Unit-1 Stack Concepts and Operations On Stacks
Unit-1 Stack Concepts and Operations On Stacks
Stack
Linear Data Structure
Prepared By:
Ms. Purvi Tandel, Chandni Naik
Topics of Unit 2
Array: Representation of arrays
One dimensional array
Two dimensional array
Applications of array
Stack: Concepts
• Operations on stacks
Applications of stacks
• Polish expression
• Reverse polish expression and their compilation
Recursion
Tower of Hanoi
Classification of Data Structure
Classification of Data Structure
Data
Structure
Non-Linear
Linear Data
Data
Structure
Structure
Tree has a constraint that node can have many children but only one
Note:
parent but graph doesn’t have any restriction.
100
Valsad Surat
60
60 35 30 30
20
Bardoli Navsari
Stack
• A linear list which allows insertion and deletion of an element at one
end only is called stack.
• The insertion operation is called as PUSH and deletion operation as POP.
• The most accessible elements in stack is known as top.
• The elements can only be removed in the opposite orders from that in
which they were added to the stack.
• Such a linear list is referred to as a LIFO (last in first out) list.
Deletion
Insertion
C
B … …
A
TOP
Stack Cont…
• A pointer TOP keeps track of the top element in the stack.
• Initially, when the stack is empty, TOP has a value of “zero”.
• Each time a new element is inserted in the stack, the pointer is
incremented by “one” before, the element is placed on the stack.
• The pointer is decremented by “one” each time a deletion is made
from the stack.
TOP = 4 b5
Now, TOP = 1, N = 5
PUSH(S, TOP, b3) TOP = 3 b4
PUSH(S, TOP, b4) TOP = 2 b3
PUSH(S, TOP, b5) TOP = 1 b1
S
Example 1: Consider the following operations for library system on stack: (Assume the
size of stack is 5).
Now, TOP = 4, N = 5
TOP = 4 b5
POP(S, TOP)
POP(S, TOP) TOP = 3 b4
TOP = 1 b1
S
Example 1: Consider the following operations for library system on stack: (Assume the
size of stack is 5).
If 5 ≥ 5 b7
Print ‘STACK
STACK OVERFLOW OVERFLOW’ and b6
return b1
S
Example 1: Summary of stack positions
TOP = 4 b5
TOP = 3 b4
TOP = 2
b2 TOP = 2 b2 TOP = 2 b3
TOP = 1
b1 TOP = 1 b1 TOP = 1 b1
TOP = 0 S S S
Push b1, b2 Pop one book Push b3, b4, b5
Now, TOP = 0, N = 5 Now, TOP = 2, N = 5 Now, TOP = 1, N = 5
TOP = 5 TOP = 5 b9
b9
TOP = 4 b5 TOP = 4 b8 b8
TOP = 3 b4 TOP = 3 b7 b7
TOP = 2 b3 TOP = 2 b6 b6
TOP = 1 b1 b1 b1
TOP = 1
S S S
Push b10
Pop three books Push b6, b7, b8, b9 Now, TOP = 5, N = 5
Now, TOP = 4, N = 5 Now, TOP = 1, N = 5 STACK OVERFLOW
Function : PEEP (S, TOP, I)
• This function returns the value of the Ith element from the TOP of the
stack. The element is not deleted by this function.
• Stack is represented by a vector S containing N elements.
PEEP (S, TOP, 2)
1. [Check for stack underflow] PEEP (S, TOP, 3)
If TOP-I+1 ≤ 0 PEEP (S, TOP, 4)
Then write (‘STACK STACK UNDERFLOW
UNDERFLOW’)
Return (0)
2. [Return Ith element from top
TOP = 3 -5
of the stack]
88
Return(S[TOP–I+1])
10
S
PROCEDURE : CHANGE (S, TOP, X, I)
• This procedure changes the value of the Ith element from the top of
the stack to X.
• Stack is represented by a vector S containing N elements.
CHANGE (S, TOP, 50, 2)
1. [Check for stack underflow] CHANGE (S, TOP, 9, 3)
If TOP-I+1 ≤ 0 CHANGE (S, TOP, 25, 8)
Then write (‘STACK STACK UNDERFLOW
UNDERFLOW’)
Return
2. [Change Ith element from top
TOP = 3 -5
of the stack] 8
50
S[TOP–I+1] ← X 9
10
3. [Finished] S
Return
Thank you