3.2 Queue
3.2 Queue
items[1] B
Front=0
items[0] A
Insert an item A
A new item (A) is inserted at the Rear of the queue
items[MAXQUEUE
-1]
. .
. .
items[3]
items[2]
items[1]
items[0] A Front=0,
Rear=0
Insert an item B
A new item (B) is inserted at the Rear of the queue
items[MAXQUEUE
-1]
. .
. .
items[3]
items[2]
items[1] B Rear=1
items[0] A Front=0
Insert an item C
A new item (C) is inserted at the Rear of the queue
items[MAXQUEUE
-1]
. .
. .
items[3]
items[2] C Rear=2
items[1] B
items[0] A Front=0
Insert an item D
A new item (D) is inserted at the Rear of the queue
items[MAXQUEUE-
1]
. .
. .
items[3] D Rear=3
items[2] C
items[1] B
items[0] A Front=0
Insert Operation(Array)
QINSERT(QUEUE, N, FRONT, REAR, ITEM)
1.If FRONT = 1 and REAR = N, or FRONT = REAR +
1, then :
write OVERFLOW, and Return.
2. If FRONT = NULL, then:
Set FRONT := 1 and REAR := 1.
Else if REAR = N, then:
Set REAR := 1.
Else :
Set REAR := REAR + 1.
3. Set QUEUE[REAR]:= ITEM.
4. Exit.
Delete A
An item (A) is deleted from the Front of the queue
items[MAXQUEUE-1]
. .
. .
items[3] D Rear=3
items[2] C
items[1] B Front=1
items[0] A
Delete B
An item (B) is deleted from the Front of the queue.
items[MAXQUEUE-1]
. .
. .
items[3] D Rear=3
items[2] C Front=2
items[1] B
items[0] A
Delete C
An item (C) is deleted from the Front of the queue
items[MAXQUEUE-1]
. .
. .
items[3] D Front=Rear=3
items[2] C
items[1] B
items[0] A
Delete D
An item (A) is deleted from the Front of the queue.
items[MAXQUEUE-1]
. .
items[3] D Front=Rear=-1
items[2] C
items[1] B
items[0] A
Delete Operation(Array)
QDELETE(QUEUE, N, FRONT, REAR, ITEM)
1.If FRONT = NULL then :
write: UNDERFLOW, and Return.
2. Set ITEM := QUEUE[FRONT].
3. If FRONT = REAR,
Set FRONT := NULL and REAR := NULL
Else if FRONT = N, then:
Set FRONT := 1.
Else :
Set FRONT := FRONT + 1.
4. Return.
Insert Operation(LL)
LINKQ_INSERT(INFO, LINK, FRONT, REAR, AVAIL,
ITEM)
1.If AVAIL = NULL, then :
write OVERFLOW, and Exit.
2. Set NEW := AVAIL and AVAIL := LINK[AVAIL]
3. Set INFO[NEW]:= ITEM and LINK[NEW]=NULL
4. If FRONT = NULL , FRONT := REAR := NEW
Else :
set LINK[REAR] := NEW and REAR := NEW
5. Exit
Delete Operation(LL)
LINKQ_DELETE(INFO, LINK, FRONT, REAR, AVAIL,
ITEM)
1.If FRONT = NULL, then :
write UNDERFLOW, and Exit.
2. Set TEMP := FRONT
3. Set ITEM :=INFO[FRONT]
4. FRONT :=LINK[FRONT]
5. LINK[TEMP] = AVAIL and AVAIL = TEMP
6. Exit
Priority Queue
More specialized data structure.
Similar to Queue, having front and rear.
Items are removed from the front.
Items are ordered by key value so that the item
with the lowest key (or highest) is always at the
front.
Items are inserted in proper position to maintain
the order.
DEQueue
It is a double-ended queue.
Items can be inserted and deleted from either
ends.
More versatile data structure than stack or
queue.
E.g. policy-based application (e.g. low priority go
to the end, high go to the front)
DEQUE
A deque(double ended queue) is one of
variation of a queue.
A deque is a linear link that allows insertion
and deletion at either end,i.e. at the front or
rear of the queue but not in the middle.
Generally the leftmost element represents
the front and the rightmost elements
represent the rear.
The four basic operations that can be
performed on deque are
insertLeft,insertRight,deleteLeft and
deleteRight.
The insertLeft and deleteLeft inserts and
deletes an element to/from the left of the
deque repectively
The insertRight and deleteRight inserts
and deletes an element to/from the right of
the deque respectively.
There are multiple ways of implementing a
deque,but most commomly used
Deque can be classified as
input restricted deque.
output restricted deque.
In input restricted deque,the insertion of
an element is restricted to one end only but
deletion can take place at both the ends.
The operations that are possible on input
restricted deque are insertRight,deleteRight
and deleteLeft.
An output restricted deque is a deque
which allows deletion at only one end of the
list but allow insertion at both ends of the list.
The operations that are possible on output
restricted deque are deleteLeft,insertRight
and insertLeft.
Thank You