Upload
douglas-warner
View
267
Download
3
Tags:
Embed Size (px)
Citation preview
Queue
•Queue:–Like any other data structure (apart from Array and Linked List),
•Queue also can be implemented,–Either as an Array or,–As a Linked List.
–So it does not have any physical identity/existence of its own, however,
•It has its own logical identity/existence because,–It handles the data in its own unique way.
Queue
Queue in frontof a counter.
Process synchronizationin a multi-user environment.
Waiting register in a cyber café.
Type:
FIFO:
LILO:
First In First Out
Last In Last Out
Examples
Queue of vehicles.
Queue
Insert an element
30
30
10
Enqueue
10 20
20
Delete an element 30
10
Dequeue
20
RearFront
Delete an element
Delete an element
Working
Queue•Queue:
–Queue is,•A collection of homogeneous data elements where,
–Insertion and Deletion operations take place at,–Two different (extreme) ends.
•Hence it is also called:–FIFO: First In First Out.–LILO: Last In Last Out.
–Insertion operation is called:•Enqueue and is normally done at,
–‘Rear’ end of the Queue.
–Deletion operation is called:•Dequeue and is normally done at,
–‘Front’ end of the Queue.
Queue
•Representation of Queue:–Can be represented in 2 ways in memory:
•Using an Array:–Gets all the properties (advantages/disadvantages) of Array.
•Using a Linked List:–Gets all the properties (advantages/disadvantages) of Linked List.
QueueImplemented using an Array
30 10 20 60 40
1 2 3 4 5
Array A
Enqueue (Insert an element)
30 10 20 60 40
Dequeue (Delete an element) 30
Dequeue (Delete an element) 10
Dequeue (Delete an element) 20
Dequeue (Delete an element) 60
Dequeue (Delete an element) 40
50
Dequeue (Delete an element) Queue is Empty
Queue is Full
QueueImplemented using a Linked List
Enqueue (Insert an element)
30 10 20
Dequeue 30
Dequeue 10
Dequeue 20
Dequeue Queue is Empty
NULL
Queue_Head
NULL
30 NULL10 NULL
20 NULL
1st Solution:
Enqueue: Insert_End
Dequeue: Delete_Front
2nd Solution:
Enqueue: Insert_Front
Dequeue: Delete_End
QueueImplemented using an Array
Enqueue (Insert an element)
30 10 20 60 40
1 2 3 4 5
Array A
Dequeue 30
Dequeue 10
Dequeue 20
Dequeue 60
Dequeue 40
30 10 20 60 40
Conclusion: Logic of Enqueue/Dequeue is working however,
It is highly inefficient to find out position every time by,scanning/looking the entire array.
Queue implemented using Array
Enqueue
30 10 20 60 40
1 2 3 4 5
Array A
30 10 20 60 40
Front =
Rear =
0
0
Steps:
If(Rear >= 5)
print “Queue is full. Enqueue not possible.”
Else
If(Front < 1) AND (Rear < 1)
Front = Front + 1
Else
Rear = Rear + 1
A[Rear] =
EndIf
R
F
R R R R
Rear = Rear + 1
EndIf
30Item
Item
Dequeue
50
U
L
Queue implemented using Array
Dequeue
30 10 20 60
1 2 3 4 5
Array A
30
10
20
60
Queue is empty.Dequeue not possible.
Front =
Rear =
0
0
Steps:
If(Front < 1)
print “Queue is empty. Dequeue not possible.”
Else
If(Front == Rear)Front = 0
Else
Front = Front + 1
EndIf
EndIf
F R
Dequeue
Dequeue
Dequeue
Dequeue
Item = A[Front]
Rear = 0
A[Front] = NULL
// 30102060
Item = NULL
Return(Item)
F FF
L
L-1
Queue implemented using Array
Steps:
If(Rear >= U) print “Queue is Full. Enqueue not possible.”Else If(Front < L) AND (Rear < L) Front = Front + 1 Rear = Rear + 1 Else Rear = Rear + 1 EndIf A[Rear] = Item
EndIfStop
Steps:
Item = NULLIf(Front < L) print “Queue is Empty. Dequeue not possible.”Else Item = A[Front] A[Front] = NULL If(Front == Rear) Front = L-1 Rear = L-1 Else Front = Front + 1 EndIfEndIfReturn(Item)Stop
Enqueue Dequeue
Full/Empty
Normal Case
ExceptionFirst Element
/ Last Element
Queue implemented using Array
•Algorithm:–Enqueue
•Input:–Item: Data to be enqueued / inserted in the Queue.–Front: Pointer to the front end of the Queue.–Rear: Pointer to the rear end of the Queue.
•Output:–Item inserted at Rear end of the Queue if successful, message otherwise.
•Data Structure:–Queue implemented using array A[L…U] with Front and Rear pointers.
Algorithm: EnqueueSteps:
If(Rear >= U)print “Queue is Full. Enqueue not possible.”
Else If(Front < L) AND (Rear < L)
Front = Front + 1Rear = Rear + 1
ElseRear = Rear + 1
EndIf
A[Rear] = Item
EndIfStop
Queue implemented using Array
•Algorithm:–Dequeue
•Input:–Front: Pointer to the front end of the Queue.–Rear: Pointer to the rear end of the Queue.
•Output:–Item: Data dequeued/deleted from the Queue if successful, message otherwise.
•Data Structure:–Queue implemented using array A[L…U] with Front and Rear pointers.
Algorithm: DequeueSteps:
Item = NULLIf(Front < L)
print “Queue is Empty. Dequeue not possible.”Else
Item = A[Front]A[Front] = NULL
If(Front == Rear)
Front = L-1Rear = L-1
ElseFront = Front + 1
EndIfEndIfReturn(Item)Stop
Tracing of Enqueue & Dequeue
0 1 2 3
Array A
Front = -1
Rear = -1
Trace the following steps for the above Queue of Length/Size 4.
1) Dequeue()2) Enqueue(35)3) Dequeue()4) Enqueue(25)5) Enqueue(45)6) Enqueue(75)7) Enqueue(65)8) Enqueue(85)9) Dequeue()10)Enqueue(55) Problem
Tracing of Enqueue & Dequeue
Enqueue:
If(Rear >= U) print “Queue is Full. Enqueue not possible.”Else If(Front < L) AND (Rear < L) Front = Front + 1 Rear = Rear + 1 Else Rear = Rear + 1 EndIf A[Rear] = Item
EndIfStop
Dequeue:
Item = NULLIf(Front < L) print “Queue is Empty. Dequeue not possible.”Else Item = A[Front] A[Front] = NULL If(Front == Rear) Front = L-1 Rear = L-1 Else Front = Front + 1 EndIfEndIfReturn(Item)Stop
1 2 3
Array A
15 35
F R
1 2 3
15 35
F R
25
2 3
35
F R
25
2 3
35
R
25
Enqueue(45)
Enqueue(25)
Dequeue()
F
Queue is full.
Queue implemented using Array
25 65
1 2 3 4 5
Array A
RF
Enqueue(85):
Queue is Full. Enqueue not possible.
Problem / Disadvantage
Solutions:
1) Change/Rewrite ‘Enqueue’ algorithm to shift the elements in case ‘Front’ is not at the starting of the Queue.
2) Change/Rewrite ‘Dequeue’ algorithm to shift all the elements as soon as one element is deleted from the Queue.
3) Implement the Queue using a Linked List.
Dequeue()35 55 45Dequeue()
Enqueue(65)
RF F
35
5545 25 65
R
2555 45
Dynamic Queue, however,Both solutions require shifting of elements and hence inefficient.
Queue
25 65
1 2 3 4 5
Array A
RF
Enqueue(85): Queue is Full. Enqueue not possible.
35 55 45
Enqueue(65)
RF F
Dequeue()
Dequeue()
Enqueue(85)
Circular Queue
35
55
85
R
Enqueue(75)
R
75
Enqueue(15)Queue is Full. Enqueue not possible.
1
2
3
4
585
75
45
25
65
R
F
Dequeue() 45
F F
Dequeue() 25
Dequeue() 65
Normal Queue v/s Circular Queue
25
1 2 3 4
35 55 45F = 0
R
R = 0
F
25
1 2 3 4
35 55 45F = 0
R
R = 0
F
Normal Queue Circular Queue
Enqueue(35)
Enqueue(55)
Enqueue(45)
Dequeue()
Dequeue()
Enqueue(25)
RF RF RF RF RF RF
Enqueue(65)
Dequeue()
Dequeue()
Dequeue()
Steps:
Enqueue(35)Enqueue: First element (Exception).
Enqueue: Queue is Full.
Enqueue: Any other element (Normal).
Dequeue: Queue is Empty.Dequeue: Last element (Exception).
Dequeue: Any other element (Normal).
Dequeue()
Conditions:
Enqueue(65)
Steps:
If(Rear >= U) print “Queue is Full. Enqueue not possible.”
Else If(Front < L) AND (Rear < L) Front = Front + 1 Rear = Rear + 1 Else Rear = Rear + 1 EndIf A[Rear] = Item
EndIf
Steps:
If(Rear == U) next = LElse next = Rear + 1EndIf
If(next == Front) print “Queue is Full. Enqueue not possible.”
Else If(Front < L) AND (Rear < L) Front = Front + 1 Rear = Rear + 1 Else Rear = next EndIf A[Rear] = ItemEndIf
Normal Queue Circular QueueENQUEUE
Normal Queue Circular QueueDEQUEUE
Steps:
Item = NULLIf(Front < L) print “Queue is Empty. Dequeue not possible.”Else Item = A[Front] A[Front] = NULL If(Front == Rear) Front = L-1 Rear = L-1 Else Front = Front + 1 EndIfEndIfReturn(Item)
Steps:If(Front == U) next = LElse next = Front + 1EndIf
Item = NULLIf(Front < L) print “Queue is Empty. Dequeue not possible.”Else Item = A[Front] A[Front] = NULL If(Front == Rear) Front = L-1 Rear = L-1 Else Front = next EndIfEndIfReturn(Item)
Tracing of Enqueue & Dequeue for Circular QueueDequeue:If(Front == U) next = LElse next = Front + 1EndIf
Item = NULLIf(Front < L) print “Queue is Empty. Dequeue not possible.”Else Item = A[Front] A[Front] = NULL If(Front == Rear) Front = L-1 Rear = L-1 Else Front = next EndIfEndIfReturn(Item)
Enqueue:If(Rear == U) next = LElse next = Rear + 1EndIf
If(next == Front) print “Queue is Full. Enqueue not possible.”
Else If(Front < L) AND (Rear < L) Front = Front + 1 Rear = Rear + 1 Else Rear = next EndIf A[Rear] = ItemEndIf
Trace the following steps for a Circular Queue of length/size ‘4’ implemented using array A[-2…1].
1) Dequeue()2) Enqueue(10)3) Enqueue(30)4) Enqueue(20)5) Enqueue(40)6) Enqueue(50)7) Dequeue()8) Dequeue()9) Enqueue(50)10) Dequeue()11) Dequeue()12) Dequeue()13) Dequeue()
-2 -1 0 1
F = -3R = -3
Queue implemented using Array
•Algorithm:–Enqueue_CQ
•Input:–Item: Data to be enqueued / inserted in the Queue.–Front: Pointer to the front end of the Queue.–Rear: Pointer to the rear end of the Queue.
•Output:–Item inserted at Rear end of the Queue if successful, message otherwise.
•Data Structure:–Queue implemented using array A[L…U] with Front and Rear pointers.
Algorithm: Enqueue_CQSteps:
If(Rear == U)next = L
Elsenext = Rear + 1
EndIf
If(next == Front)print “Queue is Full. Enqueue not possible.”
Else If(Front < L) AND (Rear < L)
Front = Front + 1Rear = Rear + 1
ElseRear = next
EndIf
A[Rear] = ItemEndIfStop
Find the next position of Rear.
Enqueue the very first element.
Enqueue any other element.
Queue implemented using Array
•Algorithm:–Dequeue_CQ
•Input:–Front: Pointer to the front end of the Queue.–Rear: Pointer to the rear end of the Queue.
•Output:–Item: Data dequeued/deleted from the Queue if successful, message otherwise.
•Data Structure:–Queue implemented using array A[L…U] with Front and Rear pointers.
Algorithm: Dequeue_CQSteps:
If(Front == U)next = L
Elsenext = Front + 1
EndIf
Item = NULLIf(Front < L)
print “Queue is Empty. Dequeue not possible.”Else
Item = A[Front]A[Front] = NULL
If(Front == Rear)
Front = L-1Rear = L-1
ElseFront = next
EndIfEndIfReturn(Item)Stop
Find the next position of Front.
Dequeue the last/only element.
Dequeue any other element.
Queue
25
1 2 3 4
35 55 45
Normal Queue
20
1 2 3 4
30 50 40
Circular Queue
F R Not possible to tell.
Front, Rear pointers?
20
1 2 3 4
50 40
Circular Queue
F R
Queue using a Linked List
Enqueue(30) Dequeue() 30
Dequeue() 10
Dequeue() 20
Dequeue() Queue is Empty
NULL
Queue_Head
NULL
30 NULL10 NULL
20 NULL
1st Solution:
Enqueue: Insert_End
Dequeue: Delete_Front
2nd Solution:
Enqueue: Insert_Front
Dequeue: Delete_End
Enqueue(10)
Enqueue(20)
Front =Rear =
NULLNULL
Traversal?
Traversal?
Yes
No
Remove it?
Front RearFront Rear
Front Rear
newnew
new
ptrptr
ptrptr
Queue using a Linked List•Algorithm:
–Enqueue_LL•Input:
–Item: Data to be enqueued / inserted in the Queue.–Queue_Head: Pointer to the starting of the Queue.–Front: Pointer to the front end of the Queue.–Rear: Pointer to the rear end of the Queue.
•Output:–Item inserted at Rear end of the Queue if successful, message otherwise.
•Data Structure:–Queue implemented using Single Linked List with Front and Rear pointers.
Algorithm: Enqueue_LLSteps:
new = GetNode(NODE)If(new == NULL)
print “Memory Insufficient. Enqueue not possible.”Else
new -> Data = Itemnew->Link = NULL
If(Queue_Head->Link == NULL)Front = newRear = newQueue_Head->Link = new
ElseRear->Link = newRear = new
EndIfEndIfStop
Enqueue the very first element.
Enqueue any other element.
Queue using a Linked List•Algorithm:
–Dequeue_LL
•Input:–Queue_Head: Pointer to the starting of the Queue.–Front: Pointer to the front end of the Queue.–Rear: Pointer to the rear end of the Queue.
•Output:–Item: Data dequeued/deleted from the Queue if successful, message otherwise.
•Data Structure:–Queue implemented using Single Linked List with Front and Rear pointers.
Algorithm: Dequeue_LLSteps:
Item = NULLIf(Queue_Head->Link == NULL)
print “Queue is empty. Dequeue not possible.”Else
Item = Front->DataQueue_Head->Link = Front->LinkReturnNode(Front)
If(Queue_Head->Link == NULL)Front = NULLRear = NULL
ElseFront = Queue_Head->Link
EndIfEndIfReturn(Item)Stop
Dequeue the last/only element.
Dequeue any other element.
Queue
RearFront
Deletion
Insertion
Deletion
Insertion
Inject(Item)
Eject()
Push(Item)
Pop()
DEQUE
Double Ended Queue
Pronounced as ‘DECK’
(Not DEQUEUE)
Queue•Deque / Deck:
–Queue where,•Both insertion and deletion can be made at either end of the queue and hence,•Can represent 2 data structures:
–Stack–Queue
–Operations possible:•Push:
–Insert Item at the Front end of a Deque.•Pop:
–Remove Item from the Front end of a Deque.•Inject:
–Insert Item at the Rear end of a Deque.•Eject:
–Remove Item from the Rear end of a Deque.
QueueTypes/Variations of DEQUE
RearFront
Insertion
DeletionInsertion
Inject(Item)Push(Item)
Pop()
RearFront
Deletion
Insertion
Deletion
Inject(Item)
Eject()Pop()
RearFront
Deletion
Insertion
DeletionInsertion
Inject(Item)
Eject()
Push(Item)
Pop()Normal
DEQUE / DECK
Input-restrictedDEQUE / DECK
Output-restrictedDEQUE / DECK
Queue
RearFront
InsertionDeletion H B D V
2 6 1 2
Items/Elements
Priorities of the Item/Element
Priority Queue
Element can be inserted or deleted not only at the ends butat any position in the queue depending on the priority.
As a result, it does not follow FIFO property and hence cannotbe called a Queue. However it is still called a Queue.
Queue•Priority Queue:
–Queue where,•Every item/element is given a value called,•The ‘priority’ of the element and,
–Element can be inserted or deleted not only at the ends but,–At any position in the queue.
–As a result,•It does not strictly follow the FIFO (First-in First-out) principle of the Queue.•However, the name is now firmly associated with this data structure.
–Example of model of Priority Queue could be:•Element of higher priority is processed before any element of lower priority.•Two elements with the same priority are processed according to the order in which they were added to the Queue.
Queue
•Applications of Queue:–Simulation:
•Queue at a ticket counter.•Queue at a traffic point.
–Aspects/Features of an Operating System:•Scheduling in a:
–Multiprogramming / Multitasking environment.