Sei sulla pagina 1di 7

DEQUE(Double-Ended QUEue)

Linear list in which elements can be added or removed at either end


but not in the middle
DEQUE can be implemented using

Circular array

Doubly linked list

There are two variations of a deque

Input restricted deque: insertions at one end of the list but allows
deletion at both ends of the list

Output restricted deque: allows deletion at only one end of the list
but insertions at both ends of the list

Using circular array

Assume DEQUE[1..SIZE] is a circular array with two pointers LEFT


and RIGHT, which points to two ends of the queue

The condition LEFT=NULL is used to indicate that a deque is empty

Operations
1. ENQDQR: To insert an item at the right end of the queue.
2. ENQDQL: To insert an item at left end of the queue.
3. DEQDQR: To remove item from the right queue.
4. DEQDQL: To remove item from left of the queue.

Input restricted dequeue (operations 1or 2, 3, 4)

Output restricted dequeue (operations1,2,3 or 4)

ENQDQR Insertions to the right end


Input: ITEM to be inserted at the right, left and right are two pointers
Output: ITEM inserted in the queue if not full
Data Structure: DEQUEUE being the circular array representation

Steps:

If((left=1 AND right=SIZE) OR(right+1=left)


Print deque is full..No insertion
Exit
Else
If (right=0) then
Left= Right= 1
Else Right=(right)%SIZE+1
Endif
A[right]=ITEM
Endif

ENQDQL- Insertions to the left end


Input: ITEM to be inserted at left, left and right are two pointers
Output: ITEM inserted in the left side of the DEQUEUE
Data Structure: DEQUEUE being the circular array representation

Steps

If (left=1 AND right=size) OR (left-1=right)


Print(Queue overflow..NO insertion) and Exit
Else
If(left=0) then Left=Right=1
Else If (left=1) then Left=size
Else Left=left-1
EndIf
Endif
A[left]=ITEM
Endif

DEQDQL-Deletions from left end


Input: A DEQUEUE with elements
Output: The item is deleted from the left end.
Data Structure: DEQUEUE being the circular array representation

Steps:
If (left=0 AND right=0) then
Print(Dequeue Underflow)
Else If(left=right) then
ITEM=A[left]
Left=right=0
Else
ITEM=A[left]
Left=(left)%size+1
EndIf
EndIf

DEQDQR-Deletions from right end


Input: A DEQUEUE with elements
Output: The item is deleted from the right end.
Data Structure: DEQUEUE being the circular array representation
Steps:

If (left=0 AND right=0) then Print(Dequeue Underflow)


Else If(left=right) then
ITEM=A[right]
Left=right=0
Else
ITEM=A[right]
If (right=0) Right=size
Else Right=right-1
EndIf
EndIf

eg. Consider a DEQUE having 6 memory cells

LEFT=2, RIGHT=4 DEQUE: ____, A,C,D, ___, ___

F is added to the right of deque

Two letters on the right are deleted

K,L and M are added to the left of the deque

One letter on the left is deleted

R is added to the left of deque

S is added to the right of deque

T is added to the right of deque

Potrebbero piacerti anche