Lecture Stack
Lecture Stack
Stack is a linear list where any element is added at the top of the list and any element is deleted
(accessed) from the top of the list. So, for stack an indicator or pointer must be used to indicate or
point the top element of the stack.
Add operation for a stack is called “push” operation and deletion operation is called “pop”
operation. Stack is a LIFO (Last In First Out) structure. That means the element which was added
last will be deleted or accessed first.
The elements of a stack are added from bottom to top, means push operation is performed from
bottom to top. The elements are deleted from top to bottom, which means pop operation is
performed from top to bottom.
Stack can be implemented using two ways; using array and using linked list.
8
7
6
5 1923 top
4 1452 (top=5
3 2315 )
2 1245
1 1025
(a) Array based stack
40 48 59 67 63
(b) A link based stack
Fig. 1: Graphical representation of stack
Array Based Stack
The stack, which is implemented using array is called array based stack. To create an array based
stack, at first we have to declare an array with required size.
Push Operation
Push operation means to add an element to a stack.
Here, we shall use array based stack. So, an array will be treated as a stack. We need an indicator
or index identifier to add element to the stack and this indicator will mark the top of the stack. To
add an element we have to check whether the array is already full or not. If the array is already full
then we cannot add any element, otherwise we can.
Here, top is an indicator indicates the top element of the stack and item is an element to be added
to the stack, M is the size of the stack (array). Overflow occurs when we try to insert an element
into the stack, which is already full.
6
6
5
top 5 H top
4 D
(top=4) 4 D (top=5)
3 Q
3 Q
2 F
2 F
1 A
1 A
(a) Before push
(b) After push
Fig. 2: Pictorial view of push operation
Algorithm to add an element to a stack
1. Declare the stack and top:
stack[1…..M], top;
2, Add an item in the stack:
if(top<M)
{
top=top+1;
stack[top]=item;
}
else print “Over Flow”;
3. Output will be the updated stack.
Pop Operation
Pop operation means to delete (access) an element from a stack. Here, top is an indicator indicates
the top element of the stack, M is the size of the array and x is a variable where we access top
element of the stack.
6
5 top
6 top 4 D (top=4)
5 H (top=5) 3 Q
4 D 2 F
3 Q 1 A
2 F
1 A
Applications of stack
Checking the validity of an arithmetic expression
Checking an expression is nothing but checking
1. Whether there are equal number of right and left parenthesis.
2. Whether right parenthesis is preceded by a matching left parenthesis.
If both the above conditions are satisfied, expression is valid.
Ex:
• ((A+B) or A+B( are not valid expressions because they violate condition 1.
• )a+b(-c violate condition 2.
• (a+b)) violate both the conditions.
• (a+b) * (c+d) is a valid expression.
Stacks can be used to check this.
Exp: [ ( A + B ) - { C + D } ] - [ F + G ]
Symbol Scanned STACK
[ [
( [, (
A [, (
+ [, (
B [, (
) [
- [
{ [, {
C [, {
+ [, {
D [, {
} [
]
-
[ [
F [
+ [
G [
]
In the above example we see the stack is empty at the end, so the expression is valid.
Exp: 3 * 3 / ( 4 – 1 ) + 6 * 2
( (
A ( A
* (* A
B (* AB
- (- AB*
( (-( AB*
C (-( AB*C
- (-(- AB*C
D (-(- AB*CD
) (- AB*CD-
) AB*CD--
/ / AB*CD--
( /( AB*CD--
E /( AB*CD--E
+ /(+ AB*CD--E
F /(+ AB*CD--EF
) / AB*CD--EF+
AB*CD--EF+/
Evaluating a postfix expression
To evaluate the postfix expression we scan the expression from left to right. The steps involved
in evaluating a postfix expression are:
Exp: 5 6 2 + * 12 4 / -
Input symbol STACK Operation (B op A)
5 5
6 5, 6
2 5, 6, 2
+ 5, 8 [6+2] (A=2, B=6)
* 40 [5*8] (A=8, B=5)
12 40, 12
4 40, 12, 4
/ 40, 3 [12/4] (A=3, B=12)
- 37 [40-3] (A=3, B=40)
Exp: 4 3 2 5 * - +
push(5)
push(8)
pop
push(2)
push(5)
pop
pop
pop
push(1)
pop
Answer: 8 5 2 5 1
Explanation