DS-Unit 2 - Stack
DS-Unit 2 - Stack
C E – S E – D ATA S T R U C T U R E S
DR. DEEPTI REDDY
A S S O C I AT E P R O F E S S O R
D E P T. O F C O M P U T E R E N G I N E E R I N G ,
Unit2- Stack
Stack
◦ Introduction to Stacks, Operations on Stacks, Applications of stacks -
Expression conversion and evaluation (Polish notation), Balanced
parenthesis checker, Recursion,
Stack Real Examples
Stack of books- How books are arranged and accessed?
Stack ADT
Stack is a linear data structure which stores the elements in an ordered manner.
The elements in a stack are added and removed only from one end which is called
top.
The policy is LIFO, the element that was inserted last is the first one to be taken
out.
Operations-
1. Push(element)- inserts an element at top of the stack
2. Pop() – removes the topmost element
3. Peek()- returns the topmost element without removing
4. isEmpty() – checks if stack is empty
5. isFull()- checks if stack is full
Push operation
Step 1: IF TOP == MAX-1
◦ PRINT OVERFLOW
◦ Goto step 4
[END OF IF]
Step 2: SET TOP = TOP + 1
Step 3: SET STACK[TOP] = VALUE
Step 4: END
Pop operation
Step 1: IF TOP = -1
◦ PRINT UNDERFLOW
◦ Go to step 4
[END OF IF]
Step 2: SET VAL = STACK[TOP]
Step 3: SET TOP = TOP - 1
Step 4: END
Peek operation
Step 1: IF TOP = -1
◦ PRINT UNDERFLOW
◦ Go to step 4
[END OF IF]
Step 2: SET VAL = STACK[TOP]
Step 3: SET TOP = TOP - 1
Step 4: END
Reflection
Consider an array of size 5, used as stack. Initially the stack is empty and top=-1.
For following operations, show the elements in the stack and value of top variable.
1. Push (A)
2. Push (B)
3. Push (C)
4. Pop()
5. Peek()
1. What is the top most element and value of top after
6. Push(D)
step 5 ?
7. Push(E) 2. At which step do you get overflow message?
8. Push(F) 3. What is the top most element and value of top after
step 10 ?
9. Push(G)
10. Pop()
Program-Implement Stack using array
#define MAX 10
int pop(int stack[])
int stack[MAX], top=-1; {
void push(int stack[], int val); If (top==-1) //check if stack is empty
int pop(int stack[]); {
int peek(int stack[]); printf(“\n stack empty”);
return -1;
}
void push(int stack[], int val) else //else if stack
{ not empty
if( top==MAX-1) //check if stack is full {
printf(“\n Stack Overflow); val=stack[top];
else //else if stack not full top--;
{ return val;
top++ }
Stack[top]=val;
}
}
Application of stack
Reversing the data items
Conversion from infix to postfix/prefix expressions
Evaluation of postfix/prefix expression
Function call
Recursion
Algorithm to reverse a string
reverse(char charArr[])
{
int size = sizeof(charArr) / sizeof(char);
char stack[20];
int i; for(i = 0; i < size; ++i)
{
push(charArr[i]);
}
for(i = 0; i < size; ++i)
{
charArr[i] = pop();
}
}
Stack-Well form-ness of
Parenthesis
Parenthesis check using stack
{A + (B – C)}
Stack [10]
Algorithm
valid= true;
s= the empty stack;
while ( entire string is not read)
{ 1. valid= true
symb=next symbol of the string; 2. valid=false
if (symb == ‘(‘ || symb == ‘{‘|| symb == 3. push (symb)
‘[‘) 4. pop()
___________
else if (symb == ‘)‘ || symb == ‘}‘|| symb ==
‘]‘)
{
i= pop ();
if ( i is not the matching opener of
symb)
_________
} //end else
} //end while
if (stack s not empty)
________
Activity
Simulate the parenthesis checker algorithm for each of the following
strings by showing the contents of the stack at each point-
1. (A+B})
2. {[A+B] – [(C-D)]
3. (A+B) – {C+D}- [F+G]
4. ((H)*{([J+K])})
5. (((A))))
Polish Notations
Algebraic expressions can be written in 3 ways-
◦ Infix, e.g A+B
◦ Prefix , e.g +AB
◦ Postfix e.g AB+
GCD(10,8)=GCD(8,2)=2
Activity- Fill in the blanks
int GCD(int x, int y)
{
int rem;
rem = x%y;
if(rem==0)
◦ return ___;
else
◦ return (GCD(___, ___));
}
Activity
Write base case and recursive function for
1. Finding sum of n numbers, e.g. sum (4)= 1+2+3+4=10
2. Calculating Fibonacci number of the given term, e.g Fib(0)=0, Fib(1)=1,
Fib(2)= 1, Fib(3)=2, Fib(4)=3, Fib(5)= 5, Fib(6)= 8…
Tower of Hanoi
The tower of Hanoi is one of the main applications of recursion.
It says, ‘if you can solve n–1 cases, then you can easily solve the nth case’.