You are on page 1of 25

Data Structures(Queues)

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

•UNDERFLOW: If front= –1 and rear= –1, => no


element in queue.
•OVERFLOW: rear=max –1, (max is size of
queue), index starts from 0.

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.

•Link List every element has two parts:


Stores the data
Stores the address of the next element.

•The START pointer of the linked list is used as FRONT.


REAR stores the address of the last element in the queue.

•If FRONT = REAR = NULL=> queue is empty.


7
Operations on Linked
Queues(insert)
•Insert element at the rear end of the queue.
•The new element is added as the last element.
•If front=NULL, then allocate memory for new node
make both rear and front point to it.
•Else insert the new node at the rear end and
name this new node as rear.

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.

•As rear cannot be moved further.


•We can use circular queue to solve this problem.
• Rear will move to location 0, if it will
reach at the end of queue.

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.

•As rear cannot be moved further.


•We can use circular queue to solve this problem.
• Rear will move to location 0, if it will
reach at the end of queue.

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.

2. If rear != MAX –1, then rear will be incremented


and the value will be inserted

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

If queue is not empty and front = rear, then after deleting


element at front queue becomes empty; front = rear = –
1.

If queue is not empty and front = MAX–1, after deleting


element at front, front = 0

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:

–Input restricted deque insertions can be done


only at one of the ends, while deletions can be done
from both ends.
–Output restricted deque deletions can be done
only at one of the ends, while insertions can be done
on both ends.

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

•Unsortedlist: insertions always done at end of list.


Every time when an element has to be removed
from list, element with highest priority will be
searched and removed.
O(1) time to insert an element and O(n) time to
delete an element
21
Priority Queue (Linked List)

•Insertion based on priority


• Before


• After

• Deletion –delete first node


22
8.5 Applications of
QUEUES
•Queues are widely used as waiting lists for a single
shared resource like printer, disk, CPU.

•Queues are used to transfer data asynchronously (data not


necessarily received at same rate as sent) between two
processes (IO buffers), e.g., pipes, file IO, sockets.

•Queues are used as buffers on MP3 players and portable


CD players, iPod playlist.

•Queues are used in Playlist for jukebox to add songs to the


end, play from the front of the list.

•Queues are used in operating system for handling interrupts.


23
8.4.4 Multiple Queues
•In case we allocate large amount of space for
queue to avoid overflow, it will result in sheer
wastage of the memory. We can use two queues
for efficient utilization of empty space.

•A better solution to deal with this problem is to


have multiple queues or to have more than one
queue in the same array of sufficient size.

24
THANKS

25

You might also like