Queue
Queue
Queue
Definition of Queue
• A Queue is an ordered collection of items from which items
may be deleted at one end (called the front of the queue) and
into which items may be inserted at the other end (the rear of
the queue).
• The first element inserted into the queue is the first element to
be removed. For this reason a queue is sometimes called a FIFO
(first-in first-out) list as opposed to the stack, which is a LIFO
(last-in first-out).
Representing a Queue Using an Array
A Queue is maintained by a linear array and two pointer
variables: FRONT, containing the location of front element of
the queue and REAR, containing the location of rear(last)
element of the queue. The condition FRONT=NULL indicates
that queue is empty. Whenever an element is deleted from
the queue, the value of FRONT is increased by 1. Similarly,
whenever an element is added to queue, the value of REAR
is increased by 1.
Queue as a circular queue
It can be seen that after N insertions in a Queue represented by
an array of N elements, the rear element of Queue will occupy
last part of array. This occurs even though the queue itself
may not contain many elements.
Now, if we want to insert an element ITEM into a queue, we
have to move or rearrange the elements of entire queue to
the beginning of the queue. This procedure may be very
expensive.
Another method to do so is to represent a queue as a circular
queue
i,e QUEUE[1] comes after QUEUE[N] in array. With this
assumption, we insert ITEM into queue by assigning
ITEM to QUEUE[1]. Thus instead of increasing REAR
to N+1, we reset REAR=1 and then assign
QUEUE[REAR]=ITEM
Similarly, If FRONT=N and an element of QUEUE is
deleted, we reset FRONT=1 instead of increasing
FRONT to N+1
Algorithm for Inserting in a QUEUE
• Algorithm: QINSERT(QUEUE, N, FRONT, REAR,ITEM)
This algorithm inserts an element in a linear queue
• Step 1:[Queue already filled]
If REAR=N, then:
Write: ‘OVERFLOW’ and Exit.
• Step 2: If FRONT=NULL, then: [Queue initially empty]
Set FRONT:=1 and REAR:=1
Else:
Set REAR:=REAR+1
[End of If structure]
• Step 3: Set QUEUE[REAR]:=ITEM
• Step 4: Return
• Algorithm: QDELETE(QUEUE,N,FRONT,REAR,ITEM)
This algorithm deletes an element from a queue
• Step 1: If FRONT=NULL, then:
Write: ‘UNDERFLOW’
Exit
• Step 2: Set ITEM:=QUEUE[FRONT]
• Step 3: If FRONT=REAR, then: [Empty Queue]
Set FRONT:=NULL and REAR:=NULL
Else:
Set FRONT:=FRONT+1
[End of If structure]
• Step 4: Return
Algorithm: QINSERT(QUEUE, N, FRONT, REAR,ITEM)
This algorithm inserts an element in a circular queue
• Step 1:[Queue already filled]
If FRONT=1 and REAR=N or FRONT=REAR+1, then:
Write: ‘OVERFLOW’
Exit
• Step 2: If FRONT=NULL, then: [Queue initially empty]
Set FRONT:=1 and REAR:=1
Else If REAR=N, then:
Set REAR:=1
Else:
Set REAR:=REAR+1
[End of If structure]
• Step 3: Set QUEUE[REAR]:=ITEM
• Algorithm: QDELETE(QUEUE,N,FRONT,REAR,ITEM)
This algorithm deletes an element from a circular queue
• Step 1: If FRONT=NULL, then:
Write: ‘UNDERFLOW’
Exit
• Step 2: Set ITEM:=QUEUE[FRONT]
• Step 3: If FRONT=REAR, then: [Empty Queue]
Set FRONT:=NULL and REAR:=NULL
Else If FRONT=N, then:
Set FRONT:=1
Else:
Set FRONT:=FRONT+1
[End of If structure]
• Step 4: Return
• Consider the following queue of characters where QUEUE
is a circular array which is allocated six memory cells
FRONT=2, REAR=4 QUEUE: _ A C D _ _
Describe the queue as following operations take place:
(a) F is added to queue
(b) Two letters are deleted
(c) K , L and M are added
(d) Two letters are deleted
(e) R is added to queue
(f) Two letters are deleted
(g) S is added to queue
(h) Two letters are deleted
(i) One letter is deleted
(j) One letter is deleted
• Solution:
(a) FRONT=2, REAR=5 QUEUE: _ A C D F_
(b) FRONT=4, REAR=5 QUEUE: _ _ _ D F _
(c) REAR=2, FRONT=4 QUEUE: L M _ D F K
(d) FRONT=6, REAR=2 QUEUE: L M _ _ _ K
(e) FRONT=6, REAR=3 QUEUE: L M R_ _ K
(f) FRONT=2, REAR=3 QUEUE: _M R _ _ _
(g) REAR=4, FRONT=2 QUEUE: _ M R S _ _
(h) FRONT=4, REAR=4 QUEUE: _ _ _ S _ _
(i) FRONT=REAR=0 [ As FRONT=REAR, queue is empty]
(j) Since FRONT=NULL, no deletion can take place.
Underflow occurred
• DEQUE(Double ended Queue)-
A deque is a queue in which elements can be added or
removed at either end but not in the middle. A deque is
usually maintained by a circular array DEQUE with pointers
LEFT and RIGHT, which point to two ends of deque. The
elements extend from LEFT end to RIGHT end of deque.
The term circular comes from the fact that DEQUE[1]
comes after DEQUE [N].The condition LEFT=NULL will be
used to indicate that a deque is empty.
• There are two variations of a deque
• Input-restricted deque- It is a deque which allows
insertions at only one end of list but allows
deletions at both ends of the list.
• Output-restricted deque- It is a deque which allows
deletions at only one end of list but allows
insertions at both ends of list
• Consider the following deque of characters where DEQUE is a circular
array which is allocated six memory cells.
LEFT=2, RIGHT=4 DEQUE: _ A,C,D, _ , _
• Describe deque while the following operation take place
(a) F is added to right of deque
Step 6: If PRN[PTR]>PRN[NEW]
Set LINK[SAVE]:=NEW
Set LINK[NEW]:=PTR
Else:
Set LINK[SAVE]:=NEW
Set LINK[NEW]=NULL
[End of If Structure]
Step 7: Return
• Another way to maintain a priority queue in memory is to use a
separate queue for each level of priority . Each such queue will appear
in its own circular array and must have its own pair of pointers, FRONT
and REAR.
• If each queue is allocated the same amount of space, a two
dimensional array QUEUE can be used instead of the linear arrays for
representing a priority queue.
• If K represents the row K of the queue, FRONT[K] and REAR[K] are the
front and rear indexes of the Kth row.
1 2 3 4 5 6
Priority
1 AAA
2 BBB CCC XXX
3
4 FFF DDD EEE
Algorithm: QINSERT( QUEUE,N, FRONT, REAR,ITEM,K)
This algorithm inserts an element in a priority queue in a row with priority
K. N is the size of the Kth row.
• Step 1:[Queue already filled]
If FRONT[K]=1 and REAR[K]=N or FRONT[K]=REAR[K]+1, then:
Write: ‘OVERFLOW’
Exit
• Step 2: If FRONT[K]=NULL, then: [Queue initially empty]
Set FRONT[K]:=1 and REAR[K]:=1
Else If REAR[K]=N, then:
Set REAR[K]:=1
Else:
Set REAR[K]:=REAR[K]+1
[End of If structure]
• Step 3: Set QUEUE[K][REAR[K]]:=ITEM