Click here to load reader
Upload
robinpt
View
218
Download
0
Embed Size (px)
DESCRIPTION
DEQUE
Citation preview
7/18/2019 DEQUE
http://slidepdf.com/reader/full/deque-5696baa93a512 1/7
DEQUE(Double-Ended QUEue)
Linear list in which elements can be added or removed at either endbut not in the middle
DEQUE can be implemented using
− Circular array
− Doubly linked list
here are two variations o! a de"ue
−
#nput restricted de"ue$ insertions at one end o! the list but allowsdeletion at both ends o! the list
− %utput restricted de"ue$ allows deletion at only one end o! the listbut insertions at both ends o! the list
7/18/2019 DEQUE
http://slidepdf.com/reader/full/deque-5696baa93a512 2/7
Using circular array
&ssume DEQUE'*#+E, is a circular array with two pointers LEand .#/01 which points to two ends o! the "ueue
he condition LE23ULL is used to indicate that a de"ue is empty
Operations
E3QDQ.$ o insert an item at the right end o! the "ueue
4 E3QDQL$ o insert an item at le!t end o! the "ueue
5 DEQDQ.$ o remove item !rom the right "ueue
6 DEQDQL$ o remove item !rom le!t o! the "ueue
Input restricted dequeue (operations 1or 2, 3, 4)
Output restricted dequeue (operations1,2,3 or 4)
7/18/2019 DEQUE
http://slidepdf.com/reader/full/deque-5696baa93a512 3/7
ENQDQR – Insertions to the right end
Input: ITEM to be inserted at the right, let and right are t!o pointers
Output: ITEM inserted in the queue i not ull
"ata #tructure: "E$%E%E being the circular arra& representation
#teps:
#!((le!t2 &3D right2*#+E) %.(right72le!t)
8rint 9de"ue is !ull3o insertion:
E;it
Else
#! (right2<) then
Le!t2 .ight2
Else .ight2(right)=*#+E7
Endi!
&'right,2#E>
Endi!
7/18/2019 DEQUE
http://slidepdf.com/reader/full/deque-5696baa93a512 4/7
ENQDQL- Insertions to the left end
Input: ITEM to be inserted at let, let and right are t!o pointers
Output: ITEM inserted in the let side o the "E$%E%E
"ata #tructure: "E$%E%E being the circular arra& representation
#teps
#! (le!t2 &3D right2si?e) %. (le!t-2right)
8rint(Queue over!low3% insertion) and E;it
Else
#!(le!t2<) then Le!t2.ight2
Else #! (le!t2) then Le!t2si?e
Else Le!t2le!t-
End#!
Endi!
&'le!t,2#E>
Endi!
7/18/2019 DEQUE
http://slidepdf.com/reader/full/deque-5696baa93a512 5/7
DEQDQL-Deletions from left end
Input: ' "E$%E%E !ith eleents
Output: The ite is deleted ro the let end
"ata #tructure: "E$%E%E being the circular arra& representation
#teps:
#! (le!t2< &3D right2<) then
8rint(9De"ueue Under!low:)
Else #!(le!t2right) then
#E>2&'le!t,
Le!t2right2<
Else #E>2&'le!t,
*et+(let)si-e.1
End#!
End#!
DEQDQR D l ti f i ht d
7/18/2019 DEQUE
http://slidepdf.com/reader/full/deque-5696baa93a512 6/7
DEQDQR-Deletions from right end
Input: ' "E$%E%E !ith eleents
Output: The ite is deleted ro the right end
"ata #tructure: "E$%E%E being the circular arra& representation
#teps:
#! (le!t2< &3D right2<) then 8rint(9De"ueue Under!low:)
Else #!(le!t2right) then
#E>2&'right,
Le!t2right2<
Else
#E>2&'right,
#! (right2<) .ight2si?e
Else .ight2right-
End#!
End#!
7/18/2019 DEQUE
http://slidepdf.com/reader/full/deque-5696baa93a512 7/7
eg Consider a DEQUE having @ memory cells
LE241 .#/026 DEQUE$ AAAA1 &1C1D1 AAA1 AAA
is added to the right o! de"ue
wo letters on the right are deleted
B1L and > are added to the le!t o! the de"ue %ne letter on the le!t is deleted
. is added to the le!t o! de"ue
* is added to the right o! de"ue
is added to the right o! de"ue