Lecture 11
Lecture 11
A Queue is a linear list of elements in which deletions can take place only at
one end, called the front, and insertions can take place only at the other
end, called the rear. The terms and are used in describing a
linear list only when it implemented as a queue.
Queues are also called first-in first-out (FIFO) lists, since the first element in
a queue will be the first element out of the queue. In other words, the order
in which elements enter a queue is the order in which they leave. This
contrasts with stacks, which are Last-in First-out (LIFO) lists.
Fig. 6-15
Suppose we want to insert an element ITEM into a queue at the time the queue does
occupy the past part of the array,
i.e. when REAR = N. One way to do this is to simply move the entire queue to the
beginning of the array, changing FRONT and REAR accordingly, and then inserting
ITEM as above.
Array QUEUE is circular, that is, that QUEUE[1] comes after QUEUE[N] in the array.
With this assumption, we insert ITEM into the queue by assigning ITEM to QUEUE[1].
Specifically, 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.
Suppose that our queue contains only one element, i.e., suppose that
FRONT = REAR = NULL
And suppose that the element is deleted. Then we assign
FRONT := NULL and REAR := NULL
To indicate that the queue is empty.
FRONT : 1
REAR : 4
FRONT : 2
REAR : 4
FRONT : 2
REAR : 6
FRONT : 3
REAR : 6
Fig : 6.16 Array representation of a queue
Prepared by, M.Azam Khan, Paf Ket 10
3
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).
QUEUE
(a) Initially empty : FRONT : 0
REAR : 0
1 2 3 4 5
(b) A, B and then C inserted: FRONT : 1 A B C
REAR : 3
B C
(C) A deleted : FRONT : 2
REAR : 3
(d) D and then E inserted : FRONT : 2 B C D E
REAR : 5
Fig :6.17
QINSERT(QUEUE,N,FRONT,REAR,ITEM)
This procedure inserts an element ITEM into a queue.
ITEM.QDELETE(QUEUE,N,FRONT,REAR,ITEM)
This procedure deletes an element from the queue and
assigns it to the variable
[Queue already empty]
If FRONT=NULL, then Write: Underflow, and Return.
2. Set ITEM=QUEUE[FRONT]
3. [Find new value of FRONT]
If FRONT=REAR, then [Queue has only one element to
start] Set FRONT=NULL and REAR=NULL
Else if FRONT =N then
Set FRONT =1
Else Set FRONT = FRONT +1
[End of if structure]
4. RPreeptaurerdnby, M.Azam Khan, Paf Ket 14
Deque
The mathematical model of a Deque (usually pronounced like "deck") is
an irregular acronym (Operation) of double-ended queue.
Double-ended queues are a kind of sequence containers.
Elements can be efficiently added and removed from any of its ends
(either the beginning or the end of the sequence).
The model allows data to be entered and withdrawn from the front
and rear of the data structure.
Insertions and deletions can occur at either end but not in the middle
Implementation is similar to that for queues
Deques are not heavily used
You should know what a deque is, but we explore them much further
An element with higher priority will be processed before any element with
lower priority.
Two elements with the same priority will be processed in order in which
they are add to the queue.
Start
PRN
AAA 1 BBB 2 CCC 3
Property:
First node will processed first
1. Circular array
EFFICIENCY OF
Queue/Circular
Queue OPERATIONS
Insert: O(1)
Remove: O(1)
Search: O(n)