DataStructure - Circular, Double Ended, Priority Queues
DataStructure - Circular, Double Ended, Priority Queues
Today Topics
Problem with Simple/scalar Queues? Solution
Circular Queues What are the circular Queue
Recap
What is simple/scalar queue Operation of simple queue
49
After dequeue: 49
48
47
3 2 tail
1
head 1
48
47
CS 103
Next insertion goes into slot 0, and tail tracks it. The insertion after that goes into a lot 1, etc.
CS 103 6
tail
0
tail 2 1 0
7
Circular Queue
When elements are deleted from the front, their spaces cannot be used to store new elements. To solve this problem, circular queue is used
Q[8] Q[7] Q[6] Q[5] Q[4]
Q[1]
Q[2]
Q[3]
DeQue
Word deque is a short form of double-ended queue. Deque defines a data structure in which item can be added or deleted at either the front or rear end. But no changes can be made elsewhere in the list. Deque is a generalization of both a stack and a queue.
DeQue
There are two variations of deques. These are: Input Restricted Deque It allows insertions only at one end but allows deletions at both ends. Output Restricted Deque It allows deletions only at one end but allows insertions at both end
Representation of Deque
delete
insert
insert
delete
a[1] 34
a[2] 12
a[3] 53
a[4] 61
a[5] 9
a[6] 23
a[7] -8
a[8] 15
a[9] 24
a[10] 42
front
Deque
rear
Implementation of Deque
When an item is inserted at the Front of DEQ, then front is decreased by 1.
When an item is inserted at the Rear of DEQ, then rear is increased by 1. When an item is deleted at the Front of DEQ, then front is increased by 1. When an item is deleted at the Rear of DEQ, then rear is decreased by 1.
Implementation of Deque
In the following figure, the DEQ has some items stored in it
Front
Y
Deque
Rear
front = 2
rear = 4
The first element at the front of the deque is empty and two elements at the rear are also empty.
Implementation of Deque
If another item XX is added at the rear, then the DEQ and values of front and rear will be :
Front
X
front = 2
Y
Deque
Z
rear = 5
XX
Rear
Implementation of Deque
If two items are deleted at the front and one item is deleted at the rear, then the DEQ and values of front and rear will be:
Front Deque
Z
front = 4 rear = 4
Rear
end if 4. End
Priority Queues
Priority queue is a collection of elements where the elements are stored according to their priority levels. The order in which the elements should get added or removed is decided by the priority of the element. Following rules are applied to maintain a priority queue: The element with a higher priority is processed before any element of lower priority. If there are elements with the same priority, then the element added first in the queue would get processed.
2
3 4
A
M
B
N
C
O P
Assignment
Given a stack S and a queue Q, write a computer program in any Object Oriented Programming language that have two procedures FILLQ_WITHS which will empty the contents of the Stack S and insert them into the queue Q and FILLS_WITHQ which will fill the stack S with the elements deleted from the queue Q. Implement the procedures with S and Q having an Array representation.
Next Lecture
Pointer Review Linked List Representation of Link List Operations of Linked List Circular Linked List Double Linked List (Two-Way List) Representation of Double Linked List Operations of Double Linked List