Professional Documents
Culture Documents
Data Structures Algorithms - Lecture 23 24 25 - Stack Queue ADT
Data Structures Algorithms - Lecture 23 24 25 - Stack Queue ADT
Data Structures Algorithms - Lecture 23 24 25 - Stack Queue ADT
ALGORITHMS
COMPILED BY: SULAMAN
AHMAD NAZ
Lecture
Stack & Queue ADT
INSTRUCTOR: Saeed
Ahmed Awan
1
STACK ADT
■ A stack is an Abstract Data Type (ADT), commonly used in
most programming languages.
■ It is named stack as it behaves like a real-world stack.
Recall Lecture # 12, 13 & 14
■ We can place or remove a card or plate from the top of the
stack only.
■ At any given time, we can only access the top element of a
stack.
2
STACK ADT
3
Implementation
■ A stack can be implemented by:
– Arrays
– Linked List
■ The stack will be full when its top reaches the maximum limit.
■ The stack will be considered empty, if there is no element in the
stack.
5
Array Based Implementation ― Push()
Algorithm Push(value)
//MAX is the maximum size of Array (Stack)
If Top == MAX Then return FALSE
Top++
Stack[Top] = value
return TRUE
End Push
6
MAX = 5
Push() Value = 10
Algorithm Push(value) 5
//MAX is the maximum size of Array (Stack) 4
If Top == MAX Then return FALSE
Top++ 3
Stack[Top] = value
2
return TRUE
End Push 1
Top 0
7
MAX = 5
Push() Value = 10
Algorithm Push(value) 5
//MAX is the maximum size of Array (Stack) 4
If Top == MAX Then return FALSE
Top++ 3
Stack[Top] = value
2
return TRUE
End Push 1
Top 0
8
MAX = 5
Push() Value = 10
Algorithm Push(value) 5
//MAX is the maximum size of Array (Stack) 4
If Top == MAX Then return FALSE
Top++ 3
Stack[Top] = value
2
return TRUE
End Push Top 1
0
9
MAX = 5
Push() Value = 10
Algorithm Push(value) 5
//MAX is the maximum size of Array (Stack) 4
If Top == MAX Then return FALSE
Top++ 3
Stack[Top] = value
2
return TRUE
End Push Top 1 10
0
10
MAX = 5
Push() Value = 10
Algorithm Push(value) 5
//MAX is the maximum size of Array (Stack) 4
If Top == MAX Then return FALSE
Top++ 3
Stack[Top] = value
2
return TRUE
End Push Top 1 10
0
11
MAX = 5
Push() Value = 5
Algorithm Push(value)
Top 5 12
Stack[Top] = value
2 4
return TRUE
End Push 1 10
0
12
MAX = 5
Push() Value = 5
Algorithm Push(value)
Top 5 12
Stack[Top] = value
2 4
return TRUE
End Push 1 10
0
13
Array Based Implementation ― Pop()
Algorithm Pop()
//MAX is the maximum size of Array (Stack)
If Top == 0 Then return NULL
value = Stack[Top]
Top ― ―
return value
End Pop
14
MAX = 5
Pop()
Algorithm Pop() 5
//MAX is the maximum size of Array (Stack) 4
If Top == 0 Then return NULL
value = Stack[Top] 3
Top ― ―
2
return value
End Pop Top 1 10
0
15
MAX = 5
Pop()
Algorithm Pop() 5
//MAX is the maximum size of Array (Stack) 4
If Top == 0 Then return NULL
value = Stack[Top] 3
Top ― ―
2
return value
End Pop Top 1 10
0
16
MAX = 5
Pop()
value = 10
Algorithm Pop() 5
//MAX is the maximum size of Array (Stack) 4
If Top == 0 Then return NULL
value = Stack[Top] 3
Top ― ―
2
return value
End Pop Top 1 10
0
17
MAX = 5
Pop()
value = 10
Algorithm Pop() 5
//MAX is the maximum size of Array (Stack) 4
If Top == 0 Then return NULL
value = Stack[Top] 3
Top ― ―
2
return value
End Pop 1 10
Top 0
18
MAX = 5
Pop()
value = 10
Algorithm Pop() 5
//MAX is the maximum size of Array (Stack) 4
If Top == 0 Then return NULL
value = Stack[Top] 3
Top ― ―
2
return value
End Pop 1 10
Top 0
19
MAX = 5
Pop()
value = 10
return 10 5
Algorithm Pop()
//MAX is the maximum size of Array (Stack) 4
If Top == 0 Then return NULL
value = Stack[Top] 3
Top ― ―
2
return value
End Pop 1 10
Top 0
20
MAX = 5
Pop()
Algorithm Pop() 5
//MAX is the maximum size of Array (Stack) 4
If Top == 0 Then return NULL
value = Stack[Top] 3
Top ― ―
2
return value
End Pop 1
Top 0
21
MAX = 5
Pop()
return NULL
Algorithm Pop() 5
//MAX is the maximum size of Array (Stack) 4
If Top == 0 Then return NULL
value = Stack[Top] 3
Top ― ―
2
return value
End Pop 1
Top 0
22
Array Based Implementation ― Peek()
Algorithm Peek() 5
//MAX is the maximum size of Array (Stack) 4
If Top == 0 Then return NULL
return Stack[Top] Top 3 5
End Peek
2 2
1 -3
0
23
Linked List Based Implementation ― Push()
Algorithm Push(value) //Initially Position is 0, when stack is empty
If Position == MAX
return FALSE
Create New Node NewNode
NewNode.data = value
If Head == NULL
Head = NewNode
Else
Top.Next = NewNode
End If
Top = NewNode
Position++
return TRUE
End Push 24
Push()
Algorithm Push(value) //Initially Size is 0, when stack is empty
If Size == MAX
return FALSE
Create New Node NewNode
Head
Top
NewNode.data = value
If Head == NULL
Head = NewNode
Else
NULL
Top.Next = NewNode 12 4 0
End If
Top = NewNode
Size++
return TRUE
End Push 25
Push()
Algorithm Push(value) //Initially Size is 0, when stack is empty
If Size == MAX NULL
return FALSE
Create New Node NewNode
Top 0
NewNode.data = value
If Head == NULL
Head = NewNode
Else 4
Top.Up = NewNode
End If
Top = NewNode Head 12
Size++
return TRUE
End Push 26
Value = 10
Push() MAX = 4
71
Applications of Stack
■ Stack data structures are used in backtracking problems.
73
QUEUE ADT
■ A queue is an Abstract Data Type (ADT).
■ Unlike stacks, a queue is open at both its ends.
■ It is named queue as it behaves like a real-world queue.
■ One end is always used to insert data (enqueue) and the
other is used to remove data (dequeue).
■ Queue follows First-In-First-Out methodology, i.e., the data
item stored first will be accessed first.
74
QUEUE ADT
75
QUEUE ADT
76
Basic Operations
77
Queue vs. Stack
■ Both does not support random access.
■ Queue is FIFO, while Stack is LIFO.
78
Representation of Queue
Rear Front
79
Implementation
■ Just like a stack, a queue can also be implemented by:
– Arrays
– Linked List
80
Enqueue()
81
Array Based Implementation ― Enqueue()
Algorithm Enqueue(value)
//MAX is the maximum size of Array (Queue)
If Rear == MAX Then return FALSE
Rear++
Queue[Rear] = value
return TRUE
End Enqueue
82
Stack
Enqueue()
Algorithm Enqueue(value) 5 5
Queue[Rear] = value
2 21
return TRUE
End Enqueue 1 12
Top 0
83
Enqueue()
Algorithm Enqueue(value)
//MAX is the maximum size of Array (Queue)
If Rear == MAX Then return FALSE Stack
Rear++
Queue[Rear] = value To
0 p
return TRUE
12 21 6 8 5
End Enqueue 1
2
84
Enqueue()
Algorithm Enqueue(value)
//MAX is the maximum size of Array (Queue)
If Rear == MAX Then return FALSE Queue
Rear++
Queue[Rear] = value Fron Rea
t 0 r
return TRUE
12 21 6 8 5
End Enqueue 1
2
85
Linked List Based Implementation
of Enqueue()
NULL Stack
Top 0
Head
Top
4
NULL
12 4 0
Head 12
86
Linked List Based Implementation
of Enqueue()
Stack Queue
Front
Head
Rear
Top
NULL
NULL
12 4 0 12 4 0
87
Dequeue()
88
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value)
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ―
return value
End Dequeue
89
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value)
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ―
Fron Rea
return value t 0 r
End Dequeue
12 21 6 8 5
1
90
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value)
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ―
Fron Rea
return value t 0 r
End Dequeue
12 21 6 8 5
1
91
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value) value = 12
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ―
Fron Rea
return value t 0 r
End Dequeue
12 21 6 8 5
1
92
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value) value = 12
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ― i i+1
Fron Rea
return value t 0 r
End Dequeue
12 21 6 8 5
1
93
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value) value = 12
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ― i i+1
Fron Rea
return value t 0 r
End Dequeue
21 21 6 8 5
1
94
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value) value = 12
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ― i i+1
Fron Rea
return value t 0 r
End Dequeue
21 21 6 8 5
1
95
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value) value = 12
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ― i i+1
Fron Rea
return value t 0 r
End Dequeue
21 6 6 8 5
1
96
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value) value = 12
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ― i i+1
Fron Rea
return value t 0 r
End Dequeue
21 6 6 8 5
1
97
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value) value = 12
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ― i i+1
Fron Rea
return value t 0 r
End Dequeue
21 6 8 8 5
1
98
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value) value = 12
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ― i i+1
Fron Rea
return value t 0 r
End Dequeue
21 6 8 8 5
1
99
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value) value = 12
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ― i i+1
Fron Rea
return value t 0 r
End Dequeue
21 6 8 5 5
1
100
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value) value = 12
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ― i i+1
Fron Rea
return value t 0 r
End Dequeue
21 6 8 5 5
1
101
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value) value = 12
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ― i i+1
Fron Rea
return value t 0 r
End Dequeue
21 6 8 5 5
1
102
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value) value = 12
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ― i i+1
Fron Rea
return value t 0 r
End Dequeue
21 6 8 5 5
1
103
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value) value = 12
return 12
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ― i i+1
Fron Rea
return value t 0 r
End Dequeue
21 6 8 5 5
1
104
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value)
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ― Rea
Fron
r
return value t 0
End Dequeue
12
1
105
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value)
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ― Rea
Fron
r
return value t 0
End Dequeue
12
1
106
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value) value = 12
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ― Rea
Fron
r
return value t 0
End Dequeue
12
1
107
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value) value = 12
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ― i i+1
Rea
Fron
r
return value t 0
End Dequeue
12
1
108
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value) value = 12
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ― i i+1
Rea
r Fron
return value t 0
End Dequeue
12
1
109
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value) value = 12
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ― i i+1
Rea
r Fron
return value t 0
End Dequeue
12
1
110
Array Based Implementation ― Dequeue()
Algorithm Dequeue(value) value = 12
return 12
If Rear == 0 Then return NULL
value = Queue[Front]
For i = Front to Rear-1
Queue[i] = Queue[i+1]
Rear ― ― i i+1
Rea
r Fron
return value t 0
End Dequeue
12
1
111
DLL Based Implementation ― Dequeue()
Front
Rear
NULL
NULL
12 4 0
112
DLL Based Implementation ― Dequeue()
Front
Rear
NULL
NULL
12 4 0
value = 12
113
DLL Based Implementation ― Dequeue()
Front
Rear
NULL
NULL
12 4 0
value = 12
114
DLL Based Implementation ― Dequeue()
Front
Rear
12
NULL
NULL
4 0
value = 12
115
DLL Based Implementation ― Dequeue()
Front
Rear
NULL
NULL
4 0
value = 12
116
DLL Based Implementation ― Dequeue()
Front
Rear
NULL
NULL
4 0
value = 12
return 12
117
DLL Based Implementation ― Dequeue()
Front
Rear
NULL
NULL
999
118
DLL Based Implementation ― Dequeue()
Front
Rear
NULL
NULL
999
value = 999
119
DLL Based Implementation ― Dequeue()
Front
Rear
NULL
NULL
999
value = 999
120
DLL Based Implementation ― Dequeue()
Front
Rear
NULL
NULL
value = 999
121
DLL Based Implementation ― Dequeue()
Front
Rear
NULL
NULL
value = 999
return 999
122
Applications of Queue
123
DEQUE
■ DEQUE stands for Double-Ended Queue.
■ In simple queue, we can insert only at REAR and read & delete only
from FRONT.
■ But, we save the priority of each value with the value. And all the values are
sorted w.r.t their priorities.
■ The value with highest priority value is placed at the FRONT while the
value at REAR has the lowest priority.
125
Priority Queue ― Array Based Implementation
Fron Rea
t 0 r
12 21 6 8
20 9 1 8 6
4 126
Priority Queue ― Array Based Implementation
Fron Rea
t 0 r
12 21 6 8
20 9 1 8 6
3 0
11
4 127
Priority Queue ― Array Based Implementation
Fron Rea
t 0 r
12 21 6 8
20 9 1 8 6
3 0
11
4 128
Priority Queue ― Array Based Implementation
Fron Rea
t 0 r
12 21 6 8 8
20 9 1 8 6 6
3 0
11
4 129
Priority Queue ― Array Based Implementation
Fron Rea
t 0 r
12 21 6 8 8
20 9 1 8 6 6
3 0
11
4 130
Priority Queue ― Array Based Implementation
Fron Rea
t 0 r
12 21 6 6 8
20 9 1 8 8 6
3 0
11
4 131
Priority Queue ― Array Based Implementation
Fron Rea
t 0 r
12 21 6 6 8
20 9 1 8 8 6
3 0
11
4 132
Priority Queue ― Array Based Implementation
Fron Rea
t 0 r
12 21 21 6 8
20 9 1 9 8 6
3 0
11
4 133
Priority Queue ― Array Based Implementation
Fron Rea
t 0 r
12 21 21 6 8
20 9 1 9 8 6
3 0
11
4 134
Priority Queue ― Array Based Implementation
Fron Rea
t 0 r
12 0 21 6 8
20 11 1 9 8 6
3 0
11
4 135
Priority Queue ― Array Based Implementation
Fron Rea
t 0 r
12 0 21 6 8
20 11 1 9 8 6
4 136
Priority Queue ― SLL Based Implementation
Front
Rear
NULL
12 4 0
20 11 9
137
Priority Queue ― SLL Based Implementation
Front
Rear
NULL
12 4 0
20 11 9
NULL
New 0
Node 11
138
Priority Queue ― SLL Based Implementation
Front
Rear
NULL
12 4 0
20 11 9
Current
Node
NULL
New 2
Node 10
139
Priority Queue ― SLL Based Implementation
Front
Rear
NULL
12 4 0
20 11 9
Current Current
Node Node.Next
NULL
New 2
Node 10
140
Priority Queue ― SLL Based Implementation
Front
Rear
NULL
12 4 0
20 11 9
Current Current
Node Node.Next
NULL
New 2
Node 10
141
Priority Queue ― SLL Based Implementation
Front
Rear
NULL
12 4 0
20 11 9
Current Current
Node Node.Next
New 2
Node 10
142
Priority Queue ― SLL Based Implementation
Front
Rear
NULL
12 4 0
20 11 9
Current
Node
New 2 Current
Node 10 Node.Next
143
Priority Queue ― SLL Based Implementation
Front
Rear
NULL
12 4 2 0
20 11 10 9
Current
Node
New Current
Node Node.Next
144
Priority Queue ― SLL Based Implementation
Front
Rear
NULL
4
11
145
Priority Queue ― SLL Based Implementation
Front
Rear
NULL
4
11
NULL
New 0
Node 22
146
Priority Queue ― SLL Based Implementation
Front
Rear
NULL
4
11
New 0
Node 22
147
Priority Queue ― SLL Based Implementation
Front
Rear
NULL
4
11
New 0
Node 22
148
Priority Queue ― SLL Based Implementation
Front
Rear
NULL
New 0 4
Node 22 11
149
Practice Question
150
End of Lecture
THANK YOU
151