Professional Documents
Culture Documents
divyashikha@dtu.ac.in
8.1 Queues
•A queue : luggage kept on conveyor belts. Bag
placed first will be first to come out at other end.
•Cars lined at a toll bridge: first car to reach
bridge will be first to leave.
•First In, First Out (FIFO) data structure: element
inserted
• first is first one taken out
•Elements in queue added at one end: rear
•Elements removed from other end: front.
•Can be implemented either using arrays or linked
lists. 2
8.2 ARRAY
REPRESENTATION OF
QUEUES
•Front and rear variables point to position where
deletions and insertions can be done, respectively.
•Initially front = 0 and rear = 0.
3
ARRAY REPRESENTATION
OF QUEUES
4
OPERATIONS ON
QUEUES (INSERT)
•Suppose front= 0 and rear= 5
•Add another element with value 45
•Rear incremented by 1 and value stored at rear.
5
OPERATIONS ON
QUEUES (DELETE)
•To delete an element from the queue, increment
value of front.
6
8.3 LINKED
REPRESENTATION OF
QUEUES
•In case size of queue not known in advance use linked
representation.
8
Operations on Linked
Queues(delete)
•The element whose element is stored in the
FRONT is deleted. If front=NULL, then the
queue is in underflow condition, an error
message is printed.
•Else we first delete the node pointed by FRONT
and then we make next node of this deleted
node as front.
9
8.4 TYPES OF QUEUES
•Circular Queue
•Deque
•Priority Queue
•Multiple queue
10
8.4.1 Circular Queue
•Suppose we have a full queue. Now if we delete
some elements from the front, even though
queue has space but new elements can not be
inserted.
11
Circular Queue (insert)
•Suppose we have a full queue. Now if we delete
some elements from the front, even though
queue has space but new elements can not be
inserted.
12
Circular Queue (insert)
• For insertion, we now have to check for the
following three conditions:
1. If front = 0 and rear = MAX –1, then the circular
queue is full.
13
3. If front != 0 and rear = MAX –1, then it means
that the queue is not full. set rear = 0 and insert
element
14
Circular Queue (insert)
15
Circular Queue (deletion)
• Three conditions
If front = –1, then no elements in queue: underflow
16
Circular Queue (Deletion)
17
8.4.2 Deque
•A deque(pronounced as ‘deck’ or ‘dequeue’) is
a list in which the
• elements can be inserted or deleted at either
end.
•It is also known as a head tail linked list.
•The elements in a deque extend from the LEFT
end to the RIGHT end and since it is circular,
Dequeue[N–1] is followed by Dequeue[0].
18
•There are two variants of a double ended queue:
19
8.4.3 Priority queues
•A priority queue is a data structure in which each
element is assigned a priorityConditions.
•The priority of the element will be used to
determine the order in which the elements will be
processed.
•The general rules of processing the elements of
a priority queue are :
–An element with higher priority is processed
before an element with a lower priority.
–Two elements with same priority are processed
on a first come first served (FCFS) basis.
20
Implementation of a
Priority Queue
•Sortedlist to store elements so that when element
has to be taken out, queue will not have to be
searched for element with highest priority.
O(n) time to insert, O(1) time to delete an element
•
• After
24
THANKS
25