Join Our Email Alerts-Subscribe
Important Note:Login & Check Your Email Inbox and Activate Confirmation Link



Enter Your Email :

DataStructure-Program to convert expression in postfix form to prefix form

#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAX 50
struct postfix
{
char stack[MAX][MAX], target[MAX] ;
char temp1[2], temp2[2] ;
char str1[MAX], str2[MAX], str3[MAX] ;
int i, top ;
} ;
void initpostfix ( struct postfix * ) ;
void setexpr ( struct postfix *, char * ) ;
void push ( struct postfix *, char * ) ;
void pop ( struct postfix *, char * ) ;
void convert ( struct postfix * ) ;
void show ( struct postfix ) ;
void main( )
{
struct postfix q ;
char expr[MAX] ;
clrscr( ) ;
initpostfix ( &q ) ;
printf ( "\nEnter an expression in postfix form: " ) ;
gets ( expr ) ;
setexpr ( &q, expr ) ;
convert ( &q ) ;
printf ( "\nThe Prefix expression is: " ) ;
show ( q ) ;
getch( ) ;
}
/* initializes the elements of the structure */
void initpostfix ( struct postfix *p )
{
p -> i = 0 ;
p -> top = -1 ;
strcpy ( p -> target, "" ) ;
}
/* copies given expr. to target string */
void setexpr ( struct postfix *p, char *c )
{
strcpy ( p -> target, c ) ;
}
/* adds an operator to the stack */
void push ( struct postfix *p, char *str )
{
if ( p -> top == MAX - 1 )
printf ( "\nStack is full." ) ;
else
{
p -> top++ ;
strcpy ( p -> stack[p -> top], str ) ;
}
}
/* pops an element from the stack */
void pop ( struct postfix *p, char *a )
{
if ( p -> top == -1 )
printf ( "\nStack is empty." ) ;
else
{
strcpy ( a, p -> stack[p -> top] ) ;
p -> top-- ;
}
}
/* converts given expr. to prefix form */
void convert ( struct postfix *p )
{
while ( p -> target[p -> i] != '\0' )
{
/* skip whitespace, if any */
if ( p -> target[p -> i] == ' ')
p -> i++ ;
if( p -> target[p -> i] == '%' || p -> target[p -> i] == '*' || p -> target[p -> i] == '-' || p -> target[p -> i] == '+' || p -> target[p -> i] == '/' || p -> target[p -> i] == '$' )
{
pop ( p, p -> str2 ) ;
pop ( p, p -> str3 ) ;
p -> temp1[0] = p -> target[ p -> i] ;
p -> temp1[1] = '\0' ;
strcpy ( p -> str1, p -> temp1 ) ;
strcat ( p -> str1, p -> str3 ) ;
strcat ( p -> str1, p -> str2 ) ;
push ( p, p -> str1 ) ;
}
else
{
p -> temp1[0] = p -> target[p -> i] ;
p -> temp1[1] = '\0' ;
strcpy ( p -> temp2, p -> temp1 ) ;
push ( p, p -> temp2 ) ;
}
p -> i++ ;
}
}
/* displays the prefix form of expr. */
void show ( struct postfix p )
{
char *temp = p.stack[0] ;
while ( *temp )
{
printf ( "%c ", *temp ) ;
temp++ ;
}
}

 


Programming & Lab Manuals- ebooks & Codes PDF






Programming & Lab Manuals

Free Download

Python Programming

R Programming

Ruby Programming

Rust Programming

Scala Programming

Swift Programming

Data Structure

DBMS

JAVA

Computer Graphics

C++ Programming

Middle Ware Programming

Algorithm

C Aptitude Questions & Answers

C Programming

ASP.NET

Assembly Programming

C#

CSS

Elixir Programming

HTML

Java Script

Perl

PHP

Operating System

Web Programming

Microprocessor Manual

Survey Lab Manual

Computer Aided Building Lab Manual

Strength of Materials Lab Manual

Hydraulic/Fluid Mechanics Lab Manual

Soil Mechanics Lab Manual

Environmental Engineering Lab Manual

Concrete and Highway Manual

Computer Aided Design Manual

Manufacturing Lab Manual

Fluid Mechanics Lab Manual

Electrical Engineering Lab Manual

Thermal Engineering Lab Manual

Strength of Materials Lab Manual

Dynamics Lab Manual

Metrology Lab Manual

C.A.D. / C.A.M Manual

Design and Fabrication Project

Simulation and Analysis Lab Manual

Mechatronics Lab Manual

Analog and digital circuits Lab Manual

OOPS Lab Manual

Control System Lab Manual

Circuit & Simulation Lab Manual

Linear Integrated Circuit Lab Manual

Digital Signal Processing Manual

Communication System Lab Manual

Microprocessor & Microcontroller Manual

Computer Networks Lab Manual

VLSI Design Lab Manual

Embedded Lab Manual

Optical and Microwave Manual

Electronics Lab Manual

Linear and Digital Circuits Lab Manual

Electrical Machines Lab Manual

Control & Instrumentation Manual

Power Electronics & Drives Manual

Microcontrollers Lab Manual

Power System Lab Manual

Data Structures & C Manual

DBMS Lab Manual

Operating Systems Laboratory Manual

Software Engineering Lab Manual

Case Tools Laboratory Manual

Internet Programming Manual

Computer Graphics Lab Manual

Mobile Apps Lab Manual

Compiler Lab Manual

NETWORK SECURITY Manual

GRID & CLOUD COMPUTING Manual

DATA MINING Manual

Automotive (EE) Lab Manual

Automotive Fuels Lab Manual

Engine Emission Manual

Java Programming Manual

SOFTWARE TESTING Manual

Physics & Chemistry Manual

ELECTRIC CIRCUITS Manual

C PROGRAMMING Manual

Aircraft Structures Manual

Aerodynamics Lab Manual

Propulsion Lab Manual

Aero Engine Manual

Aircraft Design Project

Computer Aided Simulation Manual

Aircraft Systems Laboratory Manual

Flight Integration Lab Manual

Heat Transfer Lab Manual