Queues in data structure
Transcript of Queues in data structure
-
8/9/2019 Queues in data structure
1/36
Queues
-
8/9/2019 Queues in data structure
2/36
Queue Overview Queue ADT Basic operations of queue
insert, delete etc.
Implementations of queues using array linked list
ircular queue
!riority queue
-
8/9/2019 Queues in data structure
3/36
Queue "tores a set of elements in a particular
order "tack principle# $I%"T I& $I%"T O'T ( $I$O It means# t)e *rst element inserted is t)e
*rst one to +e removed -ample
T)e *rst one in line is t)e *rst one to +eserved
-
8/9/2019 Queues in data structure
4/36
)at is a queue/ Queues are linear data structures in w)ic) we add elements
to one end and remove t)em from t)e ot)er end.
T)e *rst item to +e en0queued is t)e *rst to +e de0queued.Queue is t)erefore called a $irst In $irst Out 1$I$O2 structure.
Queue operations# nqueue 3Insert Dequeue 3 Delete mpty
-
8/9/2019 Queues in data structure
5/36
BA
BA
D
BA
D
Brearfront
rearfron
t
rear
front
rear
front
rear
front
First In First Out
A
-
8/9/2019 Queues in data structure
6/36
Queue ADTabstract typedef44eltyper5 Q'' 1eltype26
abstractempty1q2
Q''1eltype2 q6
postconditionempty(( len1q2((76
abstracteltype remove1q2
Q'' 1eltype2 q6
precondition: empty1q2 (( $A8"
postconditionremove (( *rst1q926
q(( su+1q9,:, len1q92 0:6
-
8/9/2019 Queues in data structure
7/36
Queue ADTAbstractinsert1q, elt26
Q'' 1eltype2 q6
eltype elt6
postcondition q (( q; < 4elt5 6
-
8/9/2019 Queues in data structure
8/36
Implementation of Queues Any list implementation could +e used to
implement a queues
Arrays and ircular Arrays1static# t)e si=e ofqueue is given initially2
8inked lists 1dynamic# never +ecome full2
e will e-plore implementations +ased on
array and linked list 8et;s see )ow to useanarray to
implement a queue *rst
-
8/9/2019 Queues in data structure
9/36
Array Implementation A queue in ects An array to )old elements of t)e stacks Integer to indicate position of stack top?de*ne "I@ :77
struct queue
int items"I@C6 int front, rear6
6
Earia+le of type queue can +e declared in main12 as
queue q6
q.rear (0:6 q.front (7
-
8/9/2019 Queues in data structure
10/36
-
8/9/2019 Queues in data structure
11/36
int remove(queue *q)
{ if (empty())
{
cout
-
8/9/2019 Queues in data structure
12/36
Queue F Array "tore queue as elements in array !ro+lem
Queue contents move
As result, can not add to +ack of queue,even t)oug) queue is not full
-
8/9/2019 Queues in data structure
13/36
/o,ution 45en e6er remo6e is ca,,ed mo6e a,, t5e e,ements in t5estart of "ueue2
x 1 "2items%7'for (int i17; i
-
8/9/2019 Queues in data structure
14/36
/o,ution 8T)ese pro+lems can +e avoided +yusing a circular array F an array in
w)ic) t)e location of t)e front and+ack inde- are allowed to c)angeand t)e contents to wrap around
w)en t)e last position in t)e +uGeris *lled.
-
8/9/2019 Queues in data structure
15/36
ircular Queue
H
7
:
J
K
LM
N
rearfront
Imagine t)at t)e two ends are >oined toget)er to form acircle
T)e Queue maintains two indices, frontand rear, t)at initially are
+ot) "I@ 0:
Queue implemented +y a circulararray
-
8/9/2019 Queues in data structure
16/36
7
:
J
K
L
M
N
H
+ack
front
Insertion in queue
-
rear
-
8/9/2019 Queues in data structure
17/36
front 7
:
J
K
L
M
N
H
+ack-
-
- -
-rear
Assume t)at P denotes t)e indices in t)e +uGer t)at )oldan o+>ect.
+uGer
front
Queue implemented using circular array
-
8/9/2019 Queues in data structure
18/36
front 7
:
J
K
L
M
N
H
+ack
-
- -
- rear
Assume t)at P denotes t)e indices in t)e +uGer t)at )oldan o+>ect.
+uGer
Deletion of element from queue
-
8/9/2019 Queues in data structure
19/36
ircular Array
Implementation?de*ne "I@ :77struct queue
int items"I@C6 int front, rear6
q6
q.rear ( q.front ( "I@ 0:6
-
8/9/2019 Queues in data structure
20/36
int empty("ueue 9"){
if ("#$rear 11 "#$front)return(*+) e,se
return (-A./+);!a,,in is as empty(=")int remo6e("ueue 9"){ if (empty(")) {
cout
-
8/9/2019 Queues in data structure
21/36
7
:
J
K
L
M
N
H
P
P P
P
P
PPP
P
P
front
rear
QueueOperations
)en array is full again front is equal to rearso we can not diGerentiate w)et)er array is
full or empty
-
8/9/2019 Queues in data structure
22/36
7
:
J
K
L
M
N
H
P
P P
P
P
PPP
P
front
rear
QueueOperations
"o we sacri*ce one element.
-
8/9/2019 Queues in data structure
23/36
6oid insert("ueue 9"@ int x){
if ("#$rear 11 />?+#) "#$rear 1 7; e,se
"#$rear&&;if (p"#$rear 11 p"#$front)
{ cout
-
8/9/2019 Queues in data structure
24/36
!riority Queues
!riority queue is a data structure in w)ic)
intrinsic ordering of elements t)e resultof +asic operations.
Two kinds of priority queues#
Ascending priority queue3Rin priorityQueueDescending priority queue3RAP priority
Queue
-
8/9/2019 Queues in data structure
25/36
Rin !riority Queue
ollection of elements into w)ic)elements are inserted ar+itrarily andsmallest element is removed.
ac) element )as a priority or key. "upports following operations#
ismpty insert an element into t)e priority queue get element wit) minpriority remove element wit) minpriority
-
8/9/2019 Queues in data structure
26/36
Ra- !riority Queue
ollection of elements into w)ic)elements are inserted ar+itrarily andsmallest element is removed.
ac) element )as a priority or key. "upports following operations#
ismpty insert an element into t)e priority queue get element wit)ma-priority remove element wit) ma-priority
-
8/9/2019 Queues in data structure
27/36
Applications
"orting use element key as priority put elements to +e sorted into a
priority queue e-tract elements in priority order
if a min priority queue is used, elements
are e-tracted in ascending order ofpriority
if a ma- priority queue is used, elements
are e-tracted in descending order of
-
8/9/2019 Queues in data structure
28/36
"orting -ample
"ort *ve elements w)ose keys are M, ,, K, : using a ma- priority queue. !ut t)e *ve elements into a ma- priority
queue. Do *veremovema-12 operations placing
removed elements into t)e sorted arrayfrom rig)t to left.
-
8/9/2019 Queues in data structure
29/36
After !utting Into Ra- !riorityQueue
"orted Array
M
K
:Ra-
!riority
Queue
-
8/9/2019 Queues in data structure
30/36
After $irst %emove Ra-Operation
"orted Array
M
K
:
Ra-!riority
Queue
-
8/9/2019 Queues in data structure
31/36
After "econd %emove Ra-Operation
"orted Array
K
:
M
Ra-!riority
Queue
-
8/9/2019 Queues in data structure
32/36
After T)ird %emove Ra-Operation
"orted Array
:
MK
Ra-!riority
Queue
-
8/9/2019 Queues in data structure
33/36
After $ourt) %emove Ra-Operation
"orted Array
:
MK
Ra-!riority
Queue
-
8/9/2019 Queues in data structure
34/36
After $ift) %emove Ra-Operation
"orted Array
MK:
Ra-!riority
Queue
-
8/9/2019 Queues in data structure
35/36
/uppose n e,ements of a priority "ueue are maintained inpositions 7 to n# of an array maxp" t5en p"insert(p"@x) isstri5tforward2
6oid insert("ueue 9p"@ int x)
{if ("#$rear $1 maxp"#)
{cout
-
8/9/2019 Queues in data structure
36/36
2A specia, empty() indicator can be p,acedinto a de,eted position2 >nsertion is doneas i6en before2
82Be,etion operation ,abe,s a position asempty but insertion is done at t5e Crstempty position2
D2+ac5 de,etion compact t5e array bys5iftin a,, e,ements past t5e de,etede,ement by one position anddecrementin p"2rear by 2
E2Feep t5e array sorted2
Be,etion Operation
/o,utions