Stacks Data Structures
Stacks Data Structures
STACKS
Syllabus: Stacks (Computer Engineering)
Overview of Stack:
02TOP
46
50
To understand the concept of stack very easily, let us have a
simple day-to-day life example. Suppose we went restaurant for
dinner. Waiter serves us plates for dinner. Have you seen the
arrangement of plates in the kitchen? It is actually according to
stack manner, where removal of any random plate is not
possible. We have to remove plate which is on topmost of the
plate-stack.
Stack of plates
A. STACK Operations :
- DISPLAY (): The operation prints all the values present in the
stack. We can implement this method using looping
structure of C (i.e. for loop).
The algorithm for DISPLAY operation is given below:
10 20 30 40 50
Initialize stack S
TOP = -1
PUSH (3)
3
TOP ++ ;
PUSH (6)
3 6
TOP ++ ;
POP ()
3
TOP --
POP( )
3 4 5 0
DISPLAY () { 3, 4, 5, 0 }
Stack of size n
Else
STOP
Stack of size n
Else
//perform POP or DISPLAY operation
STOP
The stack drawn below has maximum capacity =5. Top pointer
points to the 5th element. Therefore we cannot push any
element onto the stack .this is overflow condition.
Implementation of program
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10 // maximum array size is
initialized to 10
void PUSH();
void POP();
void DISPLAY();
int stack[SIZE];
int choice ,item ;
while(1)
{
printf(" *** MENU ***\n 1. PUSH\n 2. POP\n
3. DISPLAY\n 4. EXIT\n");
switch(choice)
{
case 1:PUSH();
break;
case 2:POP();
break;
case 3:DISPLAY();
break;
case 4:exit();
if(TOP==SIZE-1)
printf("*** stack overflow(full) ***\n");
else
{
printf("enter the item to be pushed into
the stack:");
scanf("%d",&item);
TOP++; // first increment the TOP and then
add
else
{
item=stack[TOP];
TOP--;
printf("the deleted item from stack is
%d\n",item);
} // end of else block
void DISPLAY()
{
int i;
if(TOP==-1)
printf("**** stack underflow****");
else
{
printf("*** stack Display ***\n");
for(i=TOP;i>=0;i--)
printf("%d at %d\n",stack[i],i);
Explanation of Program
The C implementation of a program is given below. We must
consider following points regarding to the program:
- As it is an array implementation, we must initialize our array
to maximum size (10 elements).
- The TOP pointer has to initialize to -1 ( -1 indicates that TOP
pointer initially points to nowhere)
- We must properly code main () method for our stack
implementation. Main method has switch case control
structure so that user can give his choice of operation of his
own way.
- We must code PUSH () operation of stack. PUSH operation
always checks whether stack is overflow or not. If it is in
overflow condition then it simply DISPLAYs message to the
user about overflow condition ( printf ("*** stack
overflow(full) ***\n )) otherwise it increments
TOP pointer and add the user-entered element onto the
stack. For next successive values it will add the elements
according to PUSH () function.
- In POP () function, it should check whether stack is
underflow (empty) or not. If it is empty then it simply
DISPLAYs message to the user about underflow condition
(printf("*** stack is empty ***\n”))
otherwise it simply decrements TOP pointer.
- DISPLAY () method of stack uses for loop control structure
for displaying the values of stack. Pointer i is initialized to
TOP pointer and it decrements till 0th value of an array. After
every iteration of for loop it DISPLAYs the result to the user.
We can also initialize I pointer to 0th element which
increments to its maximum TOP pointer to display the
elements of the stack. Both this implementations are
correct.
- We can also code PEEK () function that will return the
highest element of the stack. We just have to display the
current element of stack where the TOP pointer is pointed.
Output of program
output:
25 64 67 9
TOP 1 TOP 2
Implementation of program:
The program makes use of one long size array to implement two
stacks viz. stack 1 and stack 2. They have individual methods like
PUSH1 (), POP1 (), DISPLAY1 () for stack 1 and PUSH2 (), POP2 (),
DISPLAY2 () for stack 2 respectively.
#include<stdio.h>
#include<stdlib.h>
void main()
{
Int TOP1, TOP2; // stack 1 and stack 2
pointers
int n, choice=1,a,i;
int stack[100];
printf("Enter size of array you want to
use\n");
scanf("%d",&n);
TOP1=-1; // initialize stack 1 pointer to
extreme left
TOP2=n; // initialize stack 2 pointer to
extreme right
while(choice!=0)
{
printf("***** What do u want to do? *****
\n");
printf("1.PUSH element in stack 1\n");
printf("2.PUSH element in stack 2\n");
printf("3.POP element from stack 1\n");
printf("4.POP element from stack 2\n");
printf("5.DISPLAY elements of stack 1\n");
printf("6.DISPLAY elements of stack 2\n");
printf("0.EXIT from main menu \n");
scanf("%d",&choice);
switch(choice)
{
if(TOP1==(TOP2-1))
printf(" stack 1 Overflow\n");
else
{
printf("Enter the element to enter on to
the stack 1 \n");
scanf("%d",&a);
TOP1++;
stack [TOP1]=a;
} // end else block
break;
} // end case 1
else
{
printf("Enter the element to enter on to
the stack 2 \n");
scanf("%d",&a);
TOP2--
stack [TOP2]=a;
} // end of else block
break;
}// end of case 2
if(TOP1==-1)
printf("Stack 1 is empty\n");
else
{
TOP1--;
a= stack[TOP1];
printf("%d\n",a);
} //end of else block
break;
} // end of case 3
break;
} // end of case 4
if(TOP1==-1)
printf("Stack 1 is empty\n");
else
{
printf("Stack 1 is \n");
for(i=0;i<=TOP1;i++)
printf("%d ", stack[i]);
printf("\n");
} // end of else block
break;
} // end of case 4
case 6:
{
if(TOP2==n)
printf("Stack 2 is empty\n");
else
{
printf("Stack 2 is-->>\n");
for(i=(n-1);i>=TOP2;i--)
printf("%d ", stack[i]);
printf("\n");
} // end of else block
break;
} // end of case 6
case 0:
break;
} // end of switch
} // end of while
} // end of main method
Explanation of program:
output of program :
E. Applications of STACK :
Postfix Expression :
Prefix Expression :
POP the stack with remaining PUSH the operator to the stack
Operator using LIFO manner.
STEP 3: Repeat STEP 2 till all characters are scanned.
STEP 4: If the Stack TOP is not empty, then finally POP all
operators & make the stack empty.
Example:
( postfix )
( stack )
a b
( postfix )
+ *
(stack )
a b c
(postfix)
a b c *
( postfix )
We will compare the precedence of ‘– ‘with stack top operator
i.e. ‘+ ‘. Both has same precedence. Therefore our – operator
must be pushed to stack []
+ -
(stack)
a b c * d
(postfix)
a b c * d - +
(Postfix)
Implementation of program:
#include<stdio.h>
#include<stdlib.h>
void readinfix();
void compute() ;
void DISPLAY() ;
char infix[100],postfix[100],stack[100];
int n,TOP,i,j;
scanf("%d",&n);
TOP=-1;
i=-1;
j=-1;
void readinfix()
char p;
gets(infix);
i++;
void compute()
for( i=0;infix[i]!='\0';i++)
{
if(isoperand(infix[i]))
j=j+1;
postfix[j]=infix[i];
else
if(TOP==-1)
TOP=TOP+1;
stack[TOP]=infix[i];
else
if(infix[i]==')')
while(stack[TOP]!='(')
j=j+1;
postfix[i]=stack[TOP];
TOP=TOP-1;
TOP=TOP-1;
else
if(priority(infix[i])>
priority(stack[TOP]))
TOP=TOP+1;
stack[TOP]=infix[i];
else
while((priority(infix[i])<=
priority(stack[TOP])) && TOP!=-1 &&
stack[TOP]!='(' && infix[i]!='(')
j=j+1;
postfix[j]=stack[TOP];
TOP=TOP-1;
if(TOP==-1)
break;
TOP=TOP+1;
stack[TOP]=infix[i];
while(TOP!=-1)
j=j+1;
postfix[j]=stack[TOP];
TOP=TOP-1;
boolean isoperand(char x)
return true;
else
return false;
if(ch== ')')
return 4;
if((ch=='*')||(ch=='/'))
return 3;
if((ch=='+')||(ch=='-'))
return 2;
if((ch=='<')||(ch=='>'))
return 1;
return 0;
void DISPLAY()
for(i=0;postfix[i]!='\0';i++)
printf(postfix[i]);
}
void main ()
clrscr();
readinfix();
compute();
DISPLAY();
Explanation of program:
Output of program :
Enter the array Size :
10
2+3*5
POSTFIX EXPRESSION :
235*+
STEP 1 : Scan the postfix string from left to right & Initialize the
stack .
#include<stdio.h>
#include<stdlib.h>
void readpostfix();
void compute();
char postfix[100];
int stack[100];
int i,a,b,c,TOP,n;
scanf("%d",&n);
TOP=-1;
i=-1;
void readpostfix()
char p;
gets(infix);
i++;
boolean isoperand(char c)
{
if((c>='0')&&(c<='9'))
return true;
else
return false;
void compute()
for(i=0;postfix[i]!='\0';i++)
if(isoperand(postfix[i]))
TOP++;
stack[TOP]=(int)(postfix[i]-'0');
else
a=stack[TOP];
TOP--;
b=stack[TOP];
switch(postfix[i])
{
case '+' : c=b+a;
stack[TOP]=c;
break;
stack[TOP]=c;
break;
stack[TOP]=c;
break;
stack[TOP]=c;
break;
void main()
{
readpostfix();
compute();
Explanation of a program :
Output of a program :
10
235*+
VALUE OF THE EXPRESSON IS
21
int f (int x)
{
if(x == 1) return 1;
return f(x-1)*x;
}
void main()
{
int y = f(5);// main call
// y should get 120
}