Stack
Stack
Introduction
• Data structures: A data structure is a way of
organizing data that considers not only the items
stored, but also their relationship to each other
E top
D top D D
C top C C C
B top B B B B
A top A A A A A
Last In First Out
Stack Applications
• Real life
– Pile of books
– Plate trays
• More applications related to computer
science
– Program execution stack (read more from your
text)
– Evaluating expressions
Operations on stack
Push Pop
Bottom
Push Operation
Step 4: Return
empty.
stack
Algorithms of Pop operation on stack
Pop(stack, Top, X)
Step 4: Return
POP OPERATIONS
Pop ()
Top
Pop () 4 50
Pop () 3 40
Pop () 2 30
1 20
Pop ()
0 10
Pop ()
-1 Stack is empty, so
“underflow occurs”
Insertion Procedure
(PUSH)
Procedure Insertion(a,top,item,max)
If top=max then
print ‘STACK OVERFLOW’
exit
else
top=top+1
end if
a[top]=item
Exit
Deletion Procedure
(POP)
Procedure Deletion(a,top,item)
If top=0 then
print ‘STACK UNDERFLOW’
exit
else
item=a[top]
end if
top=top-1
Exit
Read Stack
(Read)
Procedure Display(top,i,a[i])
If top=0 then
Print ‘STACK EMPTY’
Exit
Else
For i=top to 0
Print a[i] End for
exit
Applications of Stack
• Reverse String or List
• Polish Notation
• Reverse Polish Notation
• Recursion
• Tower of Hanoi
• Parsing (matching parentheses or tags)
• Browsers and editors
Reverse String or List
• We can accomplish this task by pushing each
character or member from the string/list in
the order it appears
• When the line is finished, characters are then
proposed off the stack
E G
P
O NA G N
P Na A NA
me : Na A
Na
“L” me :
me : Name : Name :
“LE” “LE “LEGN” “LEGNA”
G”
Checking for balanced braces
Step2: If you see a ”closing bracket” ( i.e. ), ], or } ), pop the stack and
Step3: When you reach the end, check the status of stack
Checking for balanced braces
Evaluation of Expressions
X=a/b-c+d*e-a*c
a = 4, b = c = 2, d = e = 3
Interpretation 1:
((4/2)-2)+(3*3)-(4*2)=0 + 8+9=1
Interpretation 2:
(4/(2-2+3))*(3-4)*2=(4/3)*(-1)*2=-2.66666…
bitwise or 6 left-to-right
logical or 4 left-to-right
?: conditional 3 right-to-left
= += -= assignment 2 right-to-left
/= *= %=
<<= >>=
&= ^=
=
, comma 1 left-to-right
Polish Notation
• The way to write arithmetic expression is
known as a notation.
A+B + AB AB +
(A - C) + B + - ACB AC – B +
A+(B*C) + A * BC ABC *+
(A+B)/(C-D) /+ AB – CD AB + CD -/
• To our surprise INFIX notations are not as simple as they seem specially
while evaluating them.
Step1
•Scan the Infix expression from left to right for tokens (Operators,
Operands & Parentheses) and perform the steps 2 to 5 for each token
in the Expression
Step2
Step3
Step4
Pop all the operators which are of higher or equal precedence then
the incoming token and append them (in the same order) to the
output Expression.
After popping out all such operators, push the new token on stack.
Algorithm
Step5
Pop all the operators from the Stack and append them to Output
Pop the left parenthesis but don’t append it to the output string
Step6
•When all tokens of Infix expression have been scanned. Pop all the
elements from the stack and append them to the Output String.
A * (B + C) – D / E
Stage 2
it is.
Example
Stage 3
Stack
Example
Stage 4
maximum.
•But when another operator is to come on the top of ‘(‘ then its precedence
is least.
Example
Stage 5
the Stack, ‘(‘. The outgoing precedence of open parenthesis is the least (refer
Stage 7
Stage 8
•Next token ), means that pop all the elements from Stack and append them
•Next, we will insert the division operator into the Stack because its
is.
Example
Stage 13
•The input Expression is complete now. So we pop the Stack and Append it
INPUT
A B C
OUTPUT
SOLUTION TO THE PROBLEM OF
TOWER OF HANOI
Step 5: Return
Tower of Hanoi
Move top disk from tower A to C
A B C
A B C
Input
Move top disk from tower A to B Move top disk from tower C to B
A B C
A B C
Tower of Hanoi
Move top disk from tower A to C Move top disk from tower B to A
A B C A B C
Move top disk from tower B to C Move top disk from tower A to C
A B C A B C
Recursion
• Recursion is a programming Technique in which a
function contains a function call to itself
factorial(int x)
{
int f;
if(x==1)
return(1);
else
f=x*factorial(x-1);
return (f);
}
Continued…
• The number entered through scanf() is 5, means x=5,
since x truns out to be 5 the condition (x==1) fails
and the statement f=x*factorial(x-1) comes in
execution
Heap
Stack
Activation Records
Static Variable
Code
Memory Organization - Example
Activation
Void main records Heap
{
int x = 10;
printf(“%d”,x);
} X = 10
Static Variable
Void main
{
int x = 10;
printf(“%d”,x);
}
Memory Organization - Example
Void ex1(int a)
Heap
{
printf(“%d”,a);
} Activation
Void main() records
X = 10
{
Static Variable
int x =10;
Void main() Void ex1(int a)
X++; { {
ex1(x); int x =10; printf(“%d”,a);
} X++; }
ex1(x);}
Memory Organization - Example
Void ex1(int a)
Heap
{
printf(“%d”,a);
} Activation
Void main() records a = 11
X = 11
{
Static Variable
int x =10;
Void main() Void ex1(int a)
X++; { {
ex1(x); int x =10; printf(“%d”,a);
} X++; }
ex1(x);}
Memory Organization - Example
Void ex1(int a)
{
if(a>0) Heap
{
printf(“%d”,a);
ex1(a-1);
} Activation
} records
X=2
Void main()
{ Static Variable
int x =2; Void main() Void ex1(int a)
X++; {
{
if(a>0)
ex1(x); int x =2;
} X++; { printf(“%d”,a);
ex1(x);} ex1(a-1);}}
Memory Organization - Example
Void ex1(int a)
{ Heap
if(a>0)
{ a=0
printf(“%d”,a); a=1
ex1(a-1); a=2
} Activation
records a=3
}
X=3
Void main()
{ Static Variable
int x =2; Void main() Void ex1(int a)
X++; {
{
if(a>0)
ex1(x); int x =2;
} X++; { printf(“%d”,a);
ex1(x);} ex1(a-1);}}