Stack
Stack
A stack is a LIFO (Last In First Out) or FILO (First In Last Out) data structure in which
items are inserted and deleted from only one end called Top of stack. In other
words, a stack can be defined as a container in which insertion and
deletion can be done from the one end known as the top of the stack.
A stack is used in such a situation when we want to retrieve items in the reverse
order of their storage.
PUSH POP
o Push(): When we insert an item into the stack then the operation is known
as a push. If the stack is full then the overflow error occurs.
o Pop(): When we delete an item from the stack, the operation is known as a
pop. If the stack is empty the underflow error occurs.
o isFull(): It determines whether the stack is full or not.
o isEmpty(): It determines whether the stack is empty or not.
o Peek(): It returns the top item of stack without removing it.
o Display(): It prints all the items available in the stack.
Application of Stack
Reversing a string
Undo sequence in a text editor
Balancing of symbols
Infix-to-postfix conversion
Evaluation of postfix expression
Function calling
Large number arithmetic
PUSH
Algorithm
PUSH(stack,top,item)
POP
Algorithm
POP(stack,top)
int stack[MAXSIZE];
int top=-1;
int isFULL(void)
{
if(top==MAXSIZE-1)
return 1;
return 0;
}
int isEMPTY(void)
{
if(top==-1)
return 1;
return 0;
}
int POP(void)
{
int delitem;
if(isEMPTY())
{
printf("Stack underflow error.");
return -1;
}
delitem=stack[top];
top=top-1;
return delitem;
}
int PEEK(void)
{
if(isEMPTY())
{
printf("Stack underflow error.");
return -1;
}
return stack[top];
}
void DISPLAY(void)
{
int i;
if(isEMPTY())
printf("Stack is empty.");
else
{
printf("Items of Stack are\n");
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
}
}
int main()
{
int item,choice;
while(1)
{
case 3:
item=PEEK();
if(item!=-1)
printf("Top item of stack = %d",item);
break;
case 4:
DISPLAY();
break;
case 5:
printf("\n\n\t\t\t_ _ _ Good Bye _ _ _");
exit(0);
default:
printf("\nWrong choice");
}
}
return 0;
}
Solution:
Given
A+B*C (Infix)
A+(B*C)
A+(BC*)
AD+
ABC*+ (Postfix)
Solution:
Given
(A+B)*C (Infix)
(AB+)*C
DC*
Now expanding DC*
AB+C* (Postfix)
Solution:
Given
A*B+C/D (Infix)
(A*B)+C/D
(AB*)+C/D
E+C/D Where E=(AB*)
E+(C/D)
E+(CD/)
E+F Where F=(CD/)
EF+
Now expanding EF+
AB*F+
AB*CD/+ (Postfix)
Solution:
Given
(A+B)*C/D+E^F/G (Infix)
(AB+)*C/D+E^F/G
H*C/D+E^F/G Where H=(AB+)
H*C/D+(E^F)/G
H*C/D+(EF^)/G
H*C/D+I/G Where I=(EF^)
(H*C)/D+I/G
(HC*)/D+I/G
J/D+I/G Where J=(HC*)
(J/D)+I/G
(JD/)+I/G
K+I/G Where K=(JD/)
K+(I/G)
K+(IG/)
K+L Where L=(IG/)
KL+
Now expanding KL+
JD/L+
HC*D/L+
AB+C*D/L+
AB+C*D/IG/+
AB+C*D/EF^G/+ (Postfix)
Q. Convert the infix expression A+(B*C-(D/E^F)*G)*H into postfix expression.
Solution:
Given
A+(B*C-(D/E^F)*G)*H (Infix)
A+(B*C-(D/(E^F))*G)*H
A+(B*C-(D/(EF^))*G)*H
A+(B*C-(D/I)*G)*H Where I=(EF^)
A+(B*C-(DI/)*G)*H
A+(B*C-J*G)*H Where J=(DI/)
A+((B*C)-J*G)*H
A+((BC*)-J*G)*H
A+(K-J*G)*H Where K=(BC*)
A+(K-(J*G))*H
A+(K-(JG*))*H
A+(K-L)*H Where L=(JG*)
A+(KL-)*H
A+M*H Where M=(KL-)
A+(M*H)
A+(MH*)
A+N Where N=(MH*)
AN+
Now expanding AN+
AMH*+
AKL-H*+
ABC*L-H*+
ABC*JG*-H*+
ABC*DI/G*-H*+
ABC*DEF^/G*-H*+ (Postfix)
POLISH(Q,P)
Step1: Push “(“ onto STACK and add “)” to the end of Q.
Step 2: Scan Q from left to right and repeat Steps 3 to 6 for each element of Q until
the STACK is empty:
Q: A+(B*C-(D/E^F)*G)*H)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXSIZE 20
char stack[MAXSIZE];
int top=-1;
int isFULL(void)
{
if(top==MAXSIZE-1)
return 1;
return 0;
}
int isEMPTY(void)
{
if(top==-1)
return 1;
return 0;
}
char POP(void)
{
char delitem;
if(isEMPTY())
{
printf("Stack underflow error.");
exit(1);
}
delitem=stack[top];
top=top-1;
return delitem;
}
while(top!=-1)
{
symbol=infix[i];
switch(symbol)
{
case '(':
PUSH(symbol);
break;
case ')':
temp=POP();
while(temp!='(')
{
postfix[j]=temp;
j++;
temp=POP();
}
break;
case '+':
case '-':
case '*':
case '/':
case '^':
while(PRECED(stack[top])>=PRECED(symbol))
{
temp=POP();
postfix[j]=temp;
j++;
}
PUSH(symbol);
break;
default:
postfix[j]=symbol;
j++;
}
i++;
}
postfix[j]='\0';
printf("\nPostfix expression = %s",postfix);
}
int main()
{
char infix[25];
int len;
Q. Convert the infix expression 5*(6+2)-12/4 into postfix and evaluate it.
Solution:
Given
5*(6+2)-12/4
5*(6 2 +)-12/4
5*A-12/4 where A = (6 2 +)
(5*A)-12/4
(5A*)-12/4
B-12/4 where B = (5A*)
B-(12/4)
B-(12 4 /)
B-C where C = (12 4 /)
BC-
Now expanding BC-
5A*C-
562+*C–
5 6 2 + * 12 4 / - (Postfix)
P: 5 6 2 + * 12 4 / -)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#define MAXSIZE 20
float stack[MAXSIZE];
int top=-1;
int isFULL(void)
{
if(top==MAXSIZE-1)
return 1;
return 0;
}
int isEMPTY(void)
{
if(top==-1)
return 1;
return 0;
}
float POP(void)
{
float delitem;
if(isEMPTY())
{
printf("Stack underflow error.");
exit(1);
}
delitem=stack[top];
top=top-1;
return delitem;
}
int main()
{
char postfix[25];
int len;