Professional Documents
Culture Documents
QUEUE
CSE Department
National Institute of Technology Rourkela
Content
▪ Queue
▪ Operations on queue,
▪ Array and linked list representation
▪ Deque
▪ Priority queue
Alternative terms may be used for the four operations on a queue, including:
▪ Requirements:
▪ All queue operations must run in Q(1) time
▪ Operations on Queues
▪ Enqueue
▪ Here, we will also use another pointer called REAR, which will
store the address of the last element in the queue.
▪ All insertions will be done at the rear end and all the deletions
will be done at the front end.
Insert 9
Delete
▪ Priority Queue
▪ Multiple Queue
▪ In this case, the array is not full and yet we cannot place
any more objects in to the array
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Circular Queues
Case 1: If front = -1 and rear = – 1, the circular queue is empty.
Front = -1 rear=-1
0 1 2 3 4 5 6 7 8 9
Case 2: If front = = rear =0, the circular queue have one element
Front = 0 rear=0
5
0 1 2 3 4 5 6 7 8 9
Case 3: If front == rear != 0, the circular queue have one element
Front = 4 rear=4
9
0 1 2 3 4 5 6 7 8 9
Case 4: If Front =Max-1, deltion can be done by making front=0
Front = 9 rear=4
4 5 6 7 9 3
0 1 2 3 4 5 6 7 8 9
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Deque
▪ Deque (Double ended queue):
▪ Uses a explicit linear ordering
▪ Allows insertions at both the front and back of the deque
▪ It is also known as a head-tail linked list because elements can be
added to or removed from either the front (head) or the back
(tail) end
▪ implemented using either a circular array or a circular doubly
linked list
▪ Two pointers are maintained, LEFT and RIGHT, which point to
either end of the deque
▪ Unsorted list: insertions are always done at the end of the list. The
element with the highest priority will be searched and removed.
▪ While a sorted list takes O(n) time to insert an element in the list, it
takes only O(1) time to delete an element.
▪ Insertion:
▪ Traverse the entire list until we find a node that has a priority
lower than that of the new element.
▪ The new node is inserted before the node with the lower
priority.
▪ if same priority element is there in the queue as the new
element, the new element is inserted after that element.