Lab 6
Lab 6
LAB 06
STACK and its Applications
Input:
An array of character to hold the infix expression
char infix_expression[100];
Stack:
An array of character to hold operations
char stack[20];
Character Variable:
To hold the individual character from infix expression
char ch;
Integer variable
To point the top of the stack
int top = -1;
if(ch=='('||ch=='*'||ch=='/'||ch=='+'||ch=='-'||ch==')')
{
if(ch=='(')
push();
if(stack[top]=='(‘)
push();
iii) If it has higher priority than the top of stack, push operator on stack.
scan character Top of Stack Action
* + push
* + push
/ - push
/ - push
if((stack[top]=='+'||stack[top]=='-')&&(ch=='*'||ch=='/’))
push();
Department of Computer Science
Algorithm for Infix to Postfix conversion
if(stack[top]=='(‘)
push();
iii) If it has higher priority than the top of stack, push operator on stack.
if((stack[top]=='+'||stack[top]=='-')&&(ch=='*'||ch=='/’))
push();
iv) Else pop the operator from the stack and output it, repeat step 4
else
{
pop();
push();
}
Department of Computer Science
Algorithm for Infix to Postfix conversion