EVALUATION OF POST FIX EXPRESSION.   

AIM: 

To Write a program to evaluate a given postfix expression using Linked Stack. Stack.h is a user defined header file created for Linked Stack. 

ALGORITHM: 
STEP 1: Start the program 
STEP 2:Create the User defined Header file with the Name Stack.h  
STEP 2:Include the Header file with the Main program 
STEP 3:Initialize the parameters 
STEP 4: Get the Postfix Expression 
STEP 5: Evaluate the Expression Using Operator Precedence 
STEP 6:Stop the program execution. 

PROGRAM 1: 
Stack1.h 
#include<iostream.h> 
#include<conio.h> #include<stdlib.h> class STK_CLASS 

private: 
typedef struct stack 

char data; 
struct stack*next; 

node; 
node*top; 
public: 
STK_CLASS(); 
void Push(char item); 
int Sempty(); 
void Pop(); 
}; 
STK_CLASS::STK_CLASS() 

top=NULL; 
void STK_CLASS::Push(char item)   { 
node *New; 
New = new node; if(New == NULL) 
cout<<"\nmemory cannot be allocated\n"; 
else{ 
New->data=item; 
New->next=top; 
top=New; 


int STK_CLASS::Sempty() 

if(top==NULL) 
return 1; 
else 
return 0; 

void STK_CLASS::Pop() 

node *temp; 
temp=top; 
top=top->next; 
delete temp; 


PROGRAM 2: 
#include<iostream.h> 
#include<conio.h> #include<stdlib.h> #include<string.h> #include<math.h> 
#include “d:\stack.h” 
#define size 80 
void main() 
char exp[size]; 
int len; 
double Result; 
double post(char exp[]); 
clrscr(); 
cout<<”Enter the postfix expression\n”; 
cin>>exp; 
len=strlen(exp);  
exp[len]=’$’; 
Result=post(exp); 
cout<<”The Value of the expression is”<<Result; 
getch(); 
exit(0); 
double post(char exp[]) 
STK_CLASS obj; 
char ch ,*type; 
double result,val,op1,op2; 
int i; 
i=0; 
ch=exp[i]; 
while(ch!=$) 
if(ch>=’0’&&ch<=’9’) 
type=”operand”; 
else if(ch==’+’||ch==’_’||ch==’*’||ch==’/’||ch==’^’) 
type=”operator”; 
if(strcmp(type,”operand”)==0) 
val=ch-48; 
obj.push(val); 
else 
if(strcmp(type,”operator”)==0) 
op2=obj.pop(); op1=obj.pop(); 
switch(ch) 
case’+’: result=op1+op2; 
break; 
case’-’: result=op1-op2; 
break; 
case’*’: result=op1*op2; 
break; 
case’/’: result=op1/op2; 
break; 
case’^’: result=pow(op1,op2); 
break; 
obj.push(result); 
i++; 
ch=exp[i]; 
result=obj.pop(); 
return(result); 

OUTPUT: 
Enter the postfix Expression 
12+3* 
The Value of the expression is 9 

RESULT: Thus the program has been compiled and executed successfully.