Unit - 2
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
Sequential Linked Hierarchical Network
Organization Organization Relationship Relationship
Examples: Array Linked List Trees Graphs
Stack
Queue
Continue..
Linear Data Structure: The element are stored in sequential order. If
we know the address of first data item, then we can retrieve the
second one, next, third and so on.
Two types of Linear data structures:
1) Sequential Organization
2) Linked Organization
1. Sequential In sequential organization elements occupies
Organization:
contiguous or consecutive memory location. Example: Array, Stack, Queue.
2. Linked Organization: This is a data structure whose components are accessed in
certain sequence but they are not necessarily stored in consecutive
memory locations. It uses a pointer which show the relationship between
the elements with the help of links. Example: Linked List.
Continue..
NULL
Value
A 1000 B 2050 C 3335 D
5000 1000 2050 3335
Last Node
A linked List
• The linked allocation method of storage can result in both efficient use of
computer storage and computer time.
• A linked list is a non-sequential collection of data items.
• Each node is divided into two parts, the first part represents the information of
the element and the second part contains the address of the next mode.
• The last node of the list does not have successor node, so null value is stored as the
address.
• It is possible for a list to have no nodes at all, such a list is called empty list.
Continue..
Non-Linear Data Structure: Data structures where data elements are not arranged sequentially or
linearly are called non-linear data structures.
Two types of Non-Linear data structures:
1) Hierarchical Relationship Binary Search Tree (BST)
2) Network Relationship
1. Hierarchical Relationship: It contains data structure
which exhibits special types of behaviour(relationship)
between elements. Example: In BST(Binary Search
Tree) left child must have the value less than it’s
parents & right child must have value grater than it’s
parent value.
Continue..
2. Network Relationship: It is generalization of tree structure. Where instead of
having purely parent-to-child relationship between tree nodes, any kind of
complex relationship between nodes can be represented.
Example: Computer Network (Nodes: Workstations, Edges: Network connections)
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.
C Top = 3 (third element inserted)
B Top = 2 (second element inserted)
A Top = 1 (first element inserted)
Top = 0 (stack is empty)
Procedure : PUSH (S, TOP, X)
• This procedure inserts an element X to the top of a stack
• Stack is represented by a vector S containing N elements
• A pointer TOP represents the top element in the stack.
Stack is empty, TOP = 0,
1. [Check for stack overflow] N=3
If TOP ≥ N PUSH(S, TOP, 10)
Then write (‘STACK PUSH(S, TOP, 8)
OVERFLOW’) PUSH(S, TOP, -5)
Return PUSH(S, TOP, 6)
2. [Increment TOP] STACK OVERFLOW
TOP ← TOP + 1
3. [Insert Element]
S[TOP] ← X TOP = 3 -5
4. [Finished] TOP = 2 8
Return TOP = 1 10
Function : POP (S, TOP)
• This function removes & returns the top element from a stack.
• Stack is represented by a vector S containing N elements.
• A pointer TOP represents the top element in the stack.
POP(S, TOP)
1. [Check for stack underflow] POP(S, TOP)
If TOP = 0 POP(S, TOP)
Then write (‘STACK
POP(S, TOP)
UNDERFLOW’)
STACK UNDERFLOW
Return (0)
2. [Decrement TOP]
TOP ← TOP - 1
3. [Return former top element TOP = 3 -5
of stack] TOP = 2 8
Return(S[TOP + 1]) TOP = 1 10
TOP = 0 S
Example 1: Consider the following operations for library system on stack: (Assume the size
of stack is 5).
• Push b1, b2 1. [Check for stack overflow]
If TOP ≥ N
• Pop one book Then write (‘STACK OVERFLOW’)
Return
• Push b3,b4,b5 2. [Increment TOP]
TOP ← TOP + 1
• Pop three books 3. [Insert Element]
S[TOP] ← X
• Push b6,b7,b8,b9 4. [Finished]
Return
• Push b10
Initially
Stack is empty, TOP = 0, N = 5
PUSH(S, TOP, b1) TOP = 2 b2
PUSH(S, TOP, b2)
TOP = 1 b1
TOP = 0 S
Example 1: Consider the following operations for library system on stack: (Assume
the size of stack is 5).
• Push b1, b2 1. [Check for stack underflow]
If TOP = 0
• Pop one book Then write (‘STACK UNDERFLOW’)
Return (0)
• Push b3,b4,b5 2. [Decrement TOP]
TOP ← TOP - 1
• Pop three books 3. [Return former top element
• Push b6,b7,b8,b9 of stack]
Return(S[TOP + 1])
• Push b10
Now, TOP = 2, N = 5
POP(S, TOP)
TOP = 2 b2
TOP = 1 b1
S
Example 1: Consider the following operations for library system on stack: (Assume the size
of stack is 5).
1. [Check for stack overflow]
• Push b1, b2 If TOP ≥ N
• Pop one book Then
OVERFLOW’)
write (‘STACK
• Push b3,b4,b5 Return
2. [Increment TOP]
• Pop three books TOP ← TOP + 1
3. [Insert Element]
• Push b6,b7,b8,b9 S[TOP] ← X
4. [Finished]
• Push b10 Return
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).
• Push b1, b2 1. [Check for stack underflow]
If TOP = 0
• Pop one book Then write (‘STACK
UNDERFLOW’)
• Push b3,b4,b5 Return (0)
2. [Decrement TOP]
• Pop three books TOP ← TOP - 1
3. [Return former top element
• Push b6,b7,b8,b9 of stack]
• Push b10 Return(S[TOP + 1])
Now, TOP = 4, N = 5
TOP = 4 b5
POP(S, TOP)
POP(S, TOP) TOP = 3 b4
POP(S, TOP) TOP = 2 b3
TOP = 1 b1
S
Example 1: Consider the following operations for library system on stack: (Assume the
size of stack is 5).
• Push b1, b2 1. [Check for stack overflow]
If TOP ≥ N
• Pop one book Then write (‘STACK
OVERFLOW’)
• Push b3,b4,b5 Return
2. [Increment TOP]
• Pop three books TOP ← TOP + 1
3. [Insert Element]
• Push b6,b7,b8,b9 S[TOP] ← X
4. [Finished]
• Push b10 Return
TOP = 5 b9
Now, TOP = 1, N = 5
TOP = 4 b8
PUSH(S, TOP, b6)
PUSH(S, TOP, b7) TOP = 3 b7
PUSH(S, TOP, b8) TOP = 2 b6
PUSH(S, TOP, b9) b1
TOP = 1
S
Example 1: Consider the following operations for library system on stack: (Assume the
size of stack is 5).
• Push b1, b2 1. [Check for stack overflow]
If TOP ≥ N
• Pop one book Then write (‘STACK
OVERFLOW’)
• Push b3,b4,b5 Return
• Pop three books 2. [Increment TOP]
TOP ← TOP + 1
• Push b6,b7,b8,b9 3. [Insert Element]
S[TOP] ← X
• Push b10 4. [Finished]
Return
Now, TOP = 5, N = 5 TOP = 5 b9
PUSH(S, TOP, b10) b8
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