� Queues
QUEUES
Queues abound in everyday life.� For example:�
Basic operations that involved in Queue:
Example :�
Figure 6-15 (a) is a schematic diagram of a queue with 4 elements, where AAA is the front element and DDD is the rear element. Observe that the front and rear elements of the queue are also, respectively, the first and last elements of the list. Suppose an element is deleted from the queue. Then it must be AAA. This yields the queue in figure 6-15(b), where BBB is now the front element. Next, suppose EEE is added to the queue and then FFF is now the rear element. Now suppose another element is deleted from the queue; then it must be BBB, to yield the queue in fig.6-15(d). And so on. Observe that in such a data structure, EEE will be deleted before FFF because it has been placed in the queue before FFF. However, EEE will have to wait until CCC and DDD are deleted.
AAA
CCC
BBB
DDD
CCC
BBB
DDD
CCC
BBB
DDD
EEE
FFF
CCC
DDD
EEE
FFF
(a)
(d)
(b)
(c)
Fig. 6-15
REPRESENTATION OF QUEUE
REPRESENTATION OF QUEUE
REPRESENTATION OF QUEUE
QUEUE[REAR] := ITEM
REPRESENTATION OF QUEUE
FRONT : 1
REAR : 4
FRONT : 2
REAR : 4
FRONT : 2
REAR : 6
FRONT : 3
REAR : 6
Fig : 6.16 Array representation of a queue
QUEUE
Example
Figure 6-17 shows how a queue may be maintained by a circular array QUEUE with N = 5 memory locations. Observe that the queue always occupies consecutive locations except when it occupies locations at the beginning and at the end of the array. If the queue is viewed as a circular array, this means that is still occupies consecutive locations. Also, as indicated by fig. 6-17(m), the queue will be empty only when FRONT = REAR and an elements is deleted. For this reason, NULL is assigned to FRONT and REAR in fig. 6-17(m).
REAR : 0
| | | | |
1 2 3 4 5
QUEUE
(b) A, B and then C inserted: FRONT : 1
REAR : 3
(C) A deleted : FRONT : 2
REAR : 3
(d) D and then E inserted : FRONT : 2
REAR : 5
A | B | C | | |
| B | C | | |
| B | C | D | E |
Example
(e) B and C deleted : FRONT : 4
REAR : 5
| | | D | E |
1 2 3 4 5
QUEUE
(f) F inserted : FRONT : 4
REAR : 1
(g) D deleted : FRONT : 5
REAR : 1
(h) G and then H inserted : FRONT : 5
REAR : 3
F | | | D | E |
F | | | | E |
F | G | H | | E |
(i) E deleted : FRONT : 1
REAR : 3
(j) F deleted : FRONT : 2
REAR : 3
(k) K inserted : FRONT : 2
REAR : 4
F | G | H | | |
| G | G | | |
| G | H | K | |
Example
(l) G and H deleted : FRONT : 4
REAR : 4
| | | K | |
1 2 3 4 5
QUEUE
(m) K deleted, QUEUE is empty : FRONT : 0
REAR : 0
| | | | |
Fig :6.17
QINSERT
QINSERT(QUEUE,N,FRONT,REAR,ITEM)
This procedure inserts an element ITEM into a queue.
[Queue already fill]
QDELETE
ITEM.QDELETE(QUEUE,N,FRONT,REAR,ITEM)
This procedure deletes an element from the queue and assigns it to the variable ITEM.
1[Queue already empty]
If FRONT=NULL, then Write: Underflow, and Return.
Deque
Double-Ended-QUE
•There are two variations of deque
–Input-restricted deque
An input restricted deque is a deque which allows insertion at only one end of the list but allows deletion a both end of the list.
–Output-restricted deque
An output restricted deque is a deque which allows deletion at only one end of the list but allows insertion a both end of the list
Priority Queue
One way list representation
AAA
1
BBB
2
CCC
3
DDD
4
EEE
5
FFF
6
×
Start
PRN