You are on page 1of 5

DeQueue (or) Deque (Double ended Queue) :-

DeQueue is a data structure in which elements may be


added to or deleted from the front or the rear.
Like an ordinary queue, a double-ended queue is a data structure
it supports the following operations: enq_front, enq_back,
deq_front, deq_back, and empty. Dequeue can be behave like a
queue by using only enq_front and deq_front , and behaves like a
stack by using only enq_front and deq_rear. .
The DeQueue is represented as follows.

DeQueue can be represented in two ways they are


1) Input restricted DeQueue 2) output restricted DeQueue

The out put restricted Dequeue allows deletions from only one
end and input restricted Dequeue allow insertions at only one
end.
The DeQueue can be constructed in two ways they are
2) Using array 2. using linked list
Algorithm to add an element into DeQueue :
Assumptions: pointer f,r and initial values are -1,-1
Q[] is an array max represent the size of a queue
enq_front
step1. Start
step2. Check the queue is full or not as if (f <>
step3. If false update the pointer f as f= f-1
step4. Insert the element at pointer f as Q[f] = element
step5. Stop
enq_back
step1. Start
step2. Check the queue is full or not as if (r == max-1) if yes
queue is full
step3. If false update the pointer r as r= r+1
step4. Insert the element at pointer r as Q[r] = element
step5. Stop
Algorithm to delete an element from the DeQueue
deq_front
step1. Start
step2. Check the queue is empty or not as if (f == r) if yes queue
is empty
step3. If false update pointer f as f = f+1 and delete element at
position f as element = Q[f]
step4. If ( f== r) reset pointer f and r as f=r=-1
step5. Stop
deq_back
step1. Start
step2. Check the queue is empty or not as if (f == r) if yes queue
is empty
step3. If false delete element at position r as element = Q[r]
step4. Update pointer r as r = r-1
step5. If (f == r ) reset pointer f and r as f = r= -1
step6. Stop

The Deque Abstract Data Type


The deque abstract data type is defined by the following
structure and operations. A deque is structured, as
described above, as an ordered collection of items where
items are added and removed from either end, either front
or rear. The deque operations are given below.
 Deque() creates a new deque that is empty. It needs no
parameters and returns an empty deque.
 addFront(item) adds a new item to the front of the
deque. It needs the item and returns nothing
 addRear(item) adds a new item to the rear of the deque.
It needs the item and returns nothing.
 removeFront() removes the front item from the deque. It
needs no parameters and returns the item. The deque is
modified.
 removeRear() removes the rear item from the deque. It
needs no parameters and returns the item. The deque is
modified.
 isEmpty() tests to see whether the deque is empty. It
needs no parameters and returns a boolean value.
 size() returns the number of items in the deque. It needs
no parameters and returns an integer.
As an example, if we assume that d is a deque that has
been created and is currently empty, then Table
{dequeoperations} shows the results of a sequence of
deque operations. Note that the contents in front are listed
on the right. It is very important to keep track of the front
and the rear as you move items in and out of the collection
as things can get a bit confusing.
Return
Deque Operation Deque Contents Value

d.isEmpty() [] True

d.addRear(4) [4]
Return
Deque Operation Deque Contents Value

d.addRear('dog') ['dog',4,]

d.addFront('cat') ['dog',4,'cat']

d.addFront(True) ['dog',4,'cat',True]

d.size() ['dog',4,'cat',True] 4

d.isEmpty() ['dog',4,'cat',True] False

d.addRear(8.4) [8.4,'dog',4,'cat',True]

d.removeRear() ['dog',4,'cat',True] 8.4

d.removeFront() ['dog',4,'cat'] True

Proirity QUEUE
A priority queue is different from a "normal" queue, because instead of
being a "first-in-first-out" data structure, values come out in order by
priority.

A priority queue might be used, for example, to handle the jobs


sent to the Computer Science Department's printer:
Jobs sent by the department chair should be printed first, then jobs
sent by professors, then those sent by graduate students, and finally
those sent by undergraduates. The values put into the priority queue
would be the priority of the sender (e.g., using 4 for the chair, 3 for
professors, 2 for grad students, and 1 for undergrads), and the associated
information would be the document to print. Each time the printer is
free, the job with the highest priority would be removed from the print
queue, and printed. (Note that it is OK to have multiple jobs with the
same priority; if there is more than one job with the
same highest priority when the printer is free, then any one of them can
be selected.)

The operations that need to be provided for a priority queue are shown in
the following table, assuming that just priorities (no associated
information) are to be stored in a priority queue.

OPERATION DESCRIPTION
PriorityQ() (constructor) create an empty priority queue
boolean empty() return true iff the priority queue is empty
void
add priority p to the priority queue
insert(Comparable p)
remove and return the highest priority from the
Comparable
priority queue (error if the priority queue is
removeMax()
empty)

A priority queue can be implemented using many of the data structures d


(an array, a linked list, or a binary search tree). However, those data
structures do not provide the most efficient operations. To make all of
the operations very efficient, use a new data structure called a heap.

You might also like