0% found this document useful (0 votes)
9 views

Postfix Program in C

This document provides code for implementing a postfix expression evaluator using a stack data structure in C. It includes functions to create and dispose of a stack, push and pop elements, check if the stack is empty, and evaluate a postfix expression by operating on operands popped from the stack. The code sample shows the full postfix expression evaluator program that uses these stack functions and outputs the result of evaluating the expression "abcd+e*+f+".

Uploaded by

chiraghz
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Postfix Program in C

This document provides code for implementing a postfix expression evaluator using a stack data structure in C. It includes functions to create and dispose of a stack, push and pop elements, check if the stack is empty, and evaluate a postfix expression by operating on operands popped from the stack. The code sample shows the full postfix expression evaluator program that uses these stack functions and outputs the result of evaluating the expression "abcd+e*+f+".

Uploaded by

chiraghz
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

hackingzo ne s.

co m

http://hackingzo nes.co m/?p=1619

Postfix Program in c
#include #include #include struct node { int data; struct node *next; }; typedef struct node *ptrtonode; typedef ptrtonode STACK1; typedef ptrtonode position; STACK1 CreateStack(void) { STACK1 S; S=(struct node*)malloc(sizeof (struct node)); if (S==NULL) printf (\nFATAL Error); else { S->next=NULL; printf (\nstack created); } return S; } void PushStack(int x,STACK1 S) { ptrtonode temp; temp=(struct node*)malloc(sizeof (struct node)); if (temp==NULL) printf (\nFATAL ERROR.); else { temp->data=x; temp->next=S->next; S->next=temp; } } int IsEmpty(STACK1 S) { return S->next==NULL; } int PopStack(STACK1 S) { int x; ptrtonode f irst; if (IsEmpty(S)) printf (\nT HE STACK IS EMPT Y); else

{ f irst=S->next; x=f irst->data; S->next=S->next->next; f ree(f irst); } return x; } void MakeEmpty(STACK1 S) { while(!IsEmpty(S)) PopStack(S); } void DisposeStack(STACK1 S) { MakeEmpty(S); f ree(S); } void display(STACK1 S) { S=S->next; while(S!=NULL) { printf (%d,S->data); printf (\n); S=S->next; } } Postf ix.h f ile Application of stack adt evaluating postf ix expression Programming #includePexp.h #include void Postf ixEx(char instr[]) { STACK1 S=NULL; int s=0,x,x1,x2; char ch; S=CreateStack(); while((ch=instr[s++])!=NULL) { if (tolower(ch)>=a'&&tolower(ch)<=z') { printf (\nEnter the value f or %c=,ch); scanf (%d,&x); PushStack(x,S); } else { x1=PopStack(S); x2=PopStack(S); switch(ch) { case +:

x=x1+x2; break; case *: x=x1*x2; break; case /: x=x1/x2; break; case -: x=x1-x2; break; case %: x=x1%x2; break; def ault: printf (\nExpression is wrong); } PushStack(x,S); } } x=PopStack(S); if (S->next==NULL) printf (\nAf ter evaluated..%d,x); else printf (\nError in expression); DisposeStack(S); } Postfix.c file Application of stack adt conversion of infix to postfix expression #includepostf ix.h void main() { char instr[10]; clrscr(); printf (\nEnter the expression..); scanf (%s,instr); Postf ixEx(instr); getch(); } OUT PUT Enter the expression abcd+e*+f +* Stack created Enter the value f or a=6 Enter the value f or b=5 Enter the value f or c=2 Enter the value f or d=3 Enter the value f or e=8 Enter the value f or f =3 Af ter evaluated: 288

Relat ed Post
Heap Sort program in c QUICK SORT program in C

Enter your email address:

Delivered by FeedBurner

Post Footer automatically generated by Add Post Footer Plugin f or wordpress.

You might also like