Queue 2
Queue 2
Of
Queue
Introduction
◼ A queue is a non-primitive linear data structure.
◼ It is an homogeneous collection of elements in
which new elements are added at one end called the
Rear end and the existing elements are deleted from
other end called the Front end.
◼ A queue is logically a First In First Out (FIFO)
type of list.
◼ In our everyday life we come across many situation
where we are wait for the desired service, there we
have to get into a waiting line for our turn to get
serviced.
Introduction
◼ This waiting queue can be thought of as a Queue.
◼ This Queue means a Line.
◼ For example:
◼ At the railway reservation booth, we have to get
F R
(b) One element inserted in queue 10
0 1 2 3 4
F=0 & R=0
F R
(b) Second element inserted in queue
F=0 & R=1 10 20
0 1 2 3 4
F R
Introduction
◼ The following figures show queue graphically during
deletion operation's=Front & R=Rear
(b) Second element inserted in queue
10 20
F=0 & R=1 0 1 2 3 4
F R
(b) One element inserted in queue
F=1 & R=1
20
0 1 2 3 4
F R
◼ Dynamic implementation
◼ Circular Queue
◼ Priority Queue
Simple Queue
◼ In the simple queue, the beginning of the array will
become the front for the queue and the last location
of the array will act as rear for the queue.
◼ The following relation gives the total number of
elements present in the queue, when implemented
using arrays:
Total no of elem.=front-rear+1
Simple Queue
◼ Also note that if rear<front then there will be no
element in the queue or queue is always be empty.
◼ Bellow show a figure a empty simple queue Q[5]
which can accommodate five elements.
Q[0] Q[1]
Q[4] `
Q[2]
Q[3]
◼ Output-restricted Deque.
deletion insertion
insertion deletion
Q[0] Q[1] Q[2] Q[3] Q[4]
Fig: A Deque
Double Ended Queue (Deque)
◼ There are:
◼ Input-restricted Deque: An input restricted
deletion
10 20 30 40 50 insertion
deletion
Q[0] Q[1] Q[2] Q[3] Q[4]
F R
Fig: A representation of an input-restricted Deque
Double Ended Queue (Deque)
◼ There are:
◼ Output-restricted Deque: on the contrary, an
insertion
10 20 30 40 50 insertion
deletion
Q[0] Q[1] Q[2] Q[3] Q[4]
F R
Fig: A representation of an Output-restricted Deque
Double Ended Queue (Deque)
◼ The programs for input-restricted Deque and
output-restricted Deque would be similar to the
previous program of Deque except for a small
difference.
◼ The program for the input-restricted Deque would
not contain the function addqatbeg().
◼ Similarly the program for the output-restricted
Deque would not contain the function delatbeg().
Priority Queue
◼ A 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 or the element.
◼ Following rules are applied to maintain a priority
queue.
◼ The element with a higher priority is processes