Chapter Four
Chapter Four
Chapter Four
07/07/2023 Ataklti N. 1
4.1. Stack
Stack is a linear data structure which follows a particular order in which the
operations are performed.
A stack is a list with the restriction that insertions and deletions can be
performed in only one position, namely, the end of the list, called the top.
07/07/2023 Ataklti N. 2
Operations of Stack
07/07/2023 Ataklti N. 3
…Continue
Example of A stack of cafeteria trays, stack of books, stack of computer box in
store.
Any list implementation could be used to implement a stack
– Arrays (static: the size of stack is given initially)
– Linked lists (dynamic: never become full)
07/07/2023 Ataklti N. 4
Array Implementation
When the stack is full, top will have its maximum value, i.e. size – 1.
Initially top is set to -1. It means the stack is empty.
07/07/2023 Ataklti N. 5
Linked List Implementation of Stacks
It’s very similar to the insertion operation in a dynamic singly linked list.
The only difference is that here you'll add the new element only at the end of the
list.
– In Step [3] the TOP is moved and made to point to the last element in the stack,
which is our newly added element.
Since a dynamic list is used for the stack, the Stack is also dynamic, means it has no
07/07/2023 Ataklti N. 7
4.2. Queue
Like a stack, a queue is also a list.
However, a queue, insertion is done at one end, while deletion is performed at the other end.
Queue is a data structure that has access to its data at the front and rear.
Accessing the elements of queues follows a First In, First Out (FIFO) order.
Implementation of queue
– Array
– Linked list
07/07/2023 Ataklti N. 8
Operations of Queue
07/07/2023 Ataklti N. 9
Various Queues
07/07/2023 Ataklti N. 10
Normal queue (FIFO)
dequeue enqueue
Front Rear
Enqueue(B) B
Enqueue(C) B, C
Dequeue() C
Enqueue(G) C, G
Enqueue (F) C, G, F
Dequeue() G, F
Enqueue(A) G, F, A
Dequeue() F, A
07/07/2023 Ataklti N. 11
Simple array Queue and Circular Queues
implementation of enqueue and dequeue operations
• A problem with simple arrays is we run out of space even if the queue
never reaches the size of the array.
• Thus, simulated circular arrays (in which freed spaces are re-used to store
data) can be used to solve this problem.
07/07/2023 Ataklti N. 12
Circular Queues
• When a new item is inserted at the rear, the pointer to rear moves upwards.
• Similarly, when an item is deleted from the queue the front arrow moves
downwards.
• After a few insert and delete operations the rear might reach the end of the
queue and no more items can be inserted although the items from the front
of the queue have been deleted and there is space in the queue.
• To solve this problem, queues implement wrapping around. Such queues
are called Circular Queues.
• Example: Consider a queue with MAX_SIZE = 4
07/07/2023 Ataklti N. 13
Example of Circular and Simple array
queue
Simple array Circular array
Content of the array Content of the QUEUE SIZE Message Content of the array Content of the QUEUESIZE Message
Queue queue
Operation
Enqueue(B) B 1 B B 1
B
Enqueue(C) BC 2 B C BC 2
B C
Dequeue() C 1 C C 1
C
Enqueue(G) CG 2 C G CG 2
C G
Enqueue (F) CGF 3 C G F CGF 3
C G F
Dequeue() GF 2 G F GF 2
G F
Enqueue(A) GF 2 Overflow A G F GFA 3
G F
Enqueue(D) GF 2 Overflow A D G F GFAD 4
G F
Enqueue(C) GF 2 Overflow A D G F GFAD 4 Overflow
G F
Dequeue() F 1 A D F FAD 3
F
Enqueue(H) F 1 Overflow A D H F FADH 4
F
Dequeue () Empty 0 A D H ADH 3
07/07/2023 Ataklti N. 14
Linked list implementation of enqueue
and dequeue operations
• Enqueue- is inserting a node at the end of a linked list
• Dequeue- is deleting the first node in the list
• Queue implemented using linked list will be never full
07/07/2023 Ataklti N. 15
Application Area of Queue
07/07/2023 Ataklti N. 16
4.3 Deque
• It is a double-ended queue.
• Items can be inserted and deleted from either ends.
• More versatile data structure than stack or queue.
• has the following basic operations
EnqueueFront – inserts data at the front of the list
DequeueFront – deletes data at the front of the list
EnqueueRear – inserts data at the end of the list
DequeueRear – deletes data at the end of the list
• Deque is best implemented using doubly linked list
Front Rear
DequeueFront EnqueueFront DequeueRear EnqueueRear
07/07/2023 Ataklti N. 17
4.4. Priority Queues
07/07/2023 Ataklti N. 18
Examples of Priority Queue
Abebe AlemuDequeue()-
Belay deletes
Kedir Aster
Meron Yonas
Male Male Male Male Femal Male
e
Now the queue has data having equal priority and dequeue operation deletes the front element like in the case of
ordinary queues.
Dequeue()- deletes Abebe
Alemu Belay Kedir Yonas
Male Male Male Male
07/07/2023 Ataklti N. 19
Demerging Queues
07/07/2023 Ataklti N. 20
»I Thank You
07/07/2023 Ataklti N. 21