Upload
adityatheaaa
View
220
Download
0
Embed Size (px)
Citation preview
8/13/2019 Lecture 130 247 Queue
1/37
Data Structure and
Algorithm (CS 102)NITRKL
8/13/2019 Lecture 130 247 Queue
2/37
Queue
A queue is a linear list of elements inwhich deletion can take place only at one end
called Front, and
Insertion takes place at one end calledRear
Queues are also known as First-In-
First-Out (FIFO) list
8/13/2019 Lecture 130 247 Queue
3/37
Queue
Queue are represented in two-ways
Linear Array One-way Linked List
8/13/2019 Lecture 130 247 Queue
4/37
Array representation of Queue
A queue is maintained by a linear array QUEUE Two pointer variable
FRONT : Containing the location of the frontelement of the queue
REAR : Containing the location of the rearelement of the queue
FRONT == NULL indicates that thequeue is empty
8/13/2019 Lecture 130 247 Queue
5/37
Queue
AA BB CC DD
1 2 3 4 5 6 7 N
FRONT: 2
REAR: 4BB CC DD
1 2 3 4 5 6 7 N
Whenever an element is deleted from thequeue, the value of FRONT is increased by 1FRONT = FRONT + 1
FRONT: 1REAR: 4
Delete an element
8/13/2019 Lecture 130 247 Queue
6/37
Queue
BB CC DD
1 2 3 4 5 6 7 N
FRONT: 2
REAR: 4
Insert an element
BB CC DD EE
1 2 3 4 5 6 7 N
FRONT: 2REAR: 5
Whenever an element is inserted into thequeue, the value of REAR is increased by 1REAR = REAR + 1
8/13/2019 Lecture 130 247 Queue
7/37
Queue
REAR = N and Insert an element intoqueue
XX
ZZ1 2 3 4 5 6 7 N
FRONT: 7
REAR: N
Move the entire queue to the beginning of the
arrayChange the FRONT and REAR accordinglyInsert the element
This procedure is too expensive
8/13/2019 Lecture 130 247 Queue
8/37
Queue
Queue is assumed to be circular QUEUE[1] comes after QUEUE[N]
Instead of increasing REAR to N +1, we
reset REAR = 1 and then assignQUEUE[REAR] = ITEM
8/13/2019 Lecture 130 247 Queue
9/37
Queue
FRONT = N and an element of QUEUEis Deleted
XX
ZZ1 2 3 4 5 6 7 N
FRONT: N
REAR:
We reset FRONT = 1, instead of increasingFRONT to N + 1
8/13/2019 Lecture 130 247 Queue
10/37
Queue
QUEUE contain one elementFRONT = REAR NULL
XX
1 2 3 4 5 6 7 N
FRONT: 7
REAR: 7
FRONT = NULL and REAR = NULL
8/13/2019 Lecture 130 247 Queue
11/37
Algorithm to Insert in Q
[1] If FRONT = 1 andREAR = N or ifFRONT =
REAR + 1 then Print: Overflow and Exit[2] If FRONT = NULL then
Set FRONT = 1 and REAR = 1
Else If REAR = N thenSet REAR = 1Else
Set REAR = REAR + 1
[3] Set QUEUE[REAR] = ITEM[4] Exit
8/13/2019 Lecture 130 247 Queue
12/37
Queue
AA BB CC DD EE FF XX
ZZ1 2 3 4 5 6 7 N
FRONT: 7
REAR: 6
FRONT = REAR + 1 [FULL QUEUE]
8/13/2019 Lecture 130 247 Queue
13/37
Algorithm to Delete from Q
[1] If FRONT = NULL then Print: Underflow andExit
[2] Set ITEM = QUEUE[FRONT]
[3] If FRONT = REAR thenSet FRONT = NULL and REAR = NULL
Else If FRONT = N thenSet FRONT = 1
ElseSet FRONT = FRONT + 1
[4] Exit
8/13/2019 Lecture 130 247 Queue
14/37
Linked List Representation of Queue
A linked queue is a queue implementedas linked list with two pointer variable
FRONT and REAR pointing to the nodeswhich is in the FRONT and REAR of thequeue
8/13/2019 Lecture 130 247 Queue
15/37
Linked List Representation ofQueue
CC XAABB
FRONT
15
REAR
8/13/2019 Lecture 130 247 Queue
16/37
Insertion in a Queue
AA XCCBB
FRONT
16
REAR
DDNEW
8/13/2019 Lecture 130 247 Queue
17/37
Insertion in a Queue
AA XCCBB
FRONT
17
REAR
DD
8/13/2019 Lecture 130 247 Queue
18/37
Delete from a Queue
CC XAABB
FRONT
18
REAR
8/13/2019 Lecture 130 247 Queue
19/37
Delete from a Queue
XAABB
FRONT
19
REAR
8/13/2019 Lecture 130 247 Queue
20/37
Linked Queue
No need to check for overflow conditionduring insertion
No need to view it as circular forefficient management of space
8/13/2019 Lecture 130 247 Queue
21/37
Insertion
[1] NEW -> INFO = ITEMNEW -> LINK = NULL
[2] If (FRONT = NULL) then
FRONT = REAR = NULLelse
Set REAR -> LINK = NEW
REAR = NEW
[3] Exit
8/13/2019 Lecture 130 247 Queue
22/37
Deletion
[1] If (FRONT = NULL) thenPrint: Underflow, and Exit
[2] FRONT = FRONT -> LINK
[3] Exit
8/13/2019 Lecture 130 247 Queue
23/37
Deque
A deque is a linear list in whichelements can be added or removed ateither end but not in the middle
Deque is implemented by a circulararray DEQUE with pointers LEFT and
RIGHT which points to the two end ofthe deque
8/13/2019 Lecture 130 247 Queue
24/37
Deque LEFT = NULL indicate deque is empty
AA BB CC DD
1 2 3 4 5 6 7 8
LEFT: 4
RIGHT: 7
YY ZZ WW XX1 2 3 4 5 6 7 8
LEFT: 2
RIGHT: 7
8/13/2019 Lecture 130 247 Queue
25/37
Variation of deque
There are two variation of deque
[1] Input-restricted queue: Deque whichallows insertions at only one end of the list
but allows deletion at both ends of the list
[2] Output-restricted queue: Deque which
allows deletion at only one end of the listbut allows insertion at both ends of thelist
8/13/2019 Lecture 130 247 Queue
26/37
Deque
A C D
1 2 3 4 5 6
LEFT: 2RIGHT: 4
F is added to the right
A C D F1 2 3 4 5 6
LEFT: 2
RIGHT: 5
8/13/2019 Lecture 130 247 Queue
27/37
Deque
A C D F
1 2 3 4 5 6
LEFT: 2RIGHT: 5
Two Letters on right is deleted
A C1 2 3 4 5 6
LEFT: 2
RIGHT: 3
8/13/2019 Lecture 130 247 Queue
28/37
Deque
A C
1 2 3 4 5 6
LEFT: 2RIGHT: 3
K, L and M are added to the Left
K A C M L1 2 3 4 5 6
LEFT: 5
RIGHT: 3
8/13/2019 Lecture 130 247 Queue
29/37
Priority Queue A priority queue is a collection of elements
such that each elements has been assigneda priority and such that the order in whichelements are deleted and processed comes
from the following rules:[1] Elements of higher priority is processed
before any elements of lower priority
[2] Two elements with the same priority areprocessed according to the order in whichthey were added to the queue
8/13/2019 Lecture 130 247 Queue
30/37
Priority Queue
There are different ways a priorityqueue can be represented such as
[1] One-way List
[2] Multiple queue
8/13/2019 Lecture 130 247 Queue
31/37
One-Way List Representation ofa Priority Queue
[1] Each node in the list will contain threeitems of information: an informationfield INFO, a priority number PRN,
and a link number LINK
[2] A node X precedes a node Y in the list
(a) when X has higher priority than Y or(b) when both have same priority but Xwas added to the list before Y
8/13/2019 Lecture 130 247 Queue
32/37
Queue
32
1A 2B 2F 3D
Head
8/13/2019 Lecture 130 247 Queue
33/37
Insertion and Deletion
Deletion : Delete the first node in thelist.
Insertion: Find the location of Insertion
Add an ITEM with priority number N[a] Traverse the list until finding a node X
whose priority exceeds N. Insert ITEM
in front of node X[b] If no such node is found, insert ITEM
as the last element of the list
8/13/2019 Lecture 130 247 Queue
34/37
Array representation of PriorityQueue
Separate queue for each level ofpriority
Each queue will appear in its owncircular array and must have its ownpair of pointers, FRONT and REAR
If each queue is given the same amountspace then a 2D queue can be used
8/13/2019 Lecture 130 247 Queue
35/37
8/13/2019 Lecture 130 247 Queue
36/37
Deletion Algorithm [outline]
[1] Find the smallest K such thatFRONT[K] NULL
[2] Delete and process the front elementin row K of QUEUE
[3] Exit
8/13/2019 Lecture 130 247 Queue
37/37
Insertion Algorithm [outline]
[1] Insert ITEM as the rear element inrow M of QUEUE
[2] Exit