1 / 39

Queue

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,

mhammett
Download Presentation

Queue

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. Examples Queue Waiting register in a cyber café. Process synchronization in a multi-user environment. Queue in front of a counter. Type: FIFO: First In First Out LILO: Last In Last Out Queue of vehicles.

  3. Working Queue Enqueue Insert an element 30 10 20 30 10 20 Front Rear Dequeue Delete an element 30 10 Delete an element 20 Delete an element

  4. 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.

  5. 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.

  6. Implemented using an Array Queue Enqueue (Insert an element) 30 10 20 60 40 50 Queue is Full 30 10 20 60 40 1 2 3 4 5 Array A 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 Dequeue (Delete an element) Queue is Empty

  7. Implemented using a Linked List Queue Enqueue (Insert an element) 30 10 20 30 NULL Queue_Head 10 NULL NULL NULL 20 NULL 1st Solution: 2nd Solution: Dequeue 30 Enqueue: Insert_End Enqueue: Insert_Front Dequeue 10 Dequeue: Delete_Front Dequeue: Delete_End Dequeue 20 Dequeue Queue is Empty

  8. Implemented using an Array Queue Enqueue (Insert an element) Dequeue 30 Dequeue 10 30 10 20 60 40 30 10 20 60 40 Dequeue 20 1 2 3 4 5 Dequeue 60 Array A Dequeue 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.

  9. Queue implemented using Array Steps: U Enqueue If(Rear >= 5) Item 30 10 20 60 40 50 print “Queue is full. Enqueue not possible.” L Else Array A If(Front < 1) AND (Rear < 1) Front = 0 30 10 20 60 40 Front = Front + 1 Rear = 0 1 2 3 4 5 Rear = Rear + 1 Else R R R R R Rear = Rear + 1 F EndIf A[Rear] = 30 Item Dequeue EndIf

  10. Steps: Queue implemented using Array Item = NULL L Array A If(Front < 1) Front = 0 print “Queue is empty. Dequeue not possible.” 30 10 20 60 Rear = 0 1 2 3 4 5 Else // 60 30 10 20 Item = A[Front] F F F R A[Front] = NULL F If(Front == Rear) Front = 0 Dequeue 30 L-1 Rear = 0 Dequeue 10 Else Dequeue 20 Front = Front + 1 EndIf Dequeue 60 EndIf Queue is empty. Dequeue not possible. Dequeue Return(Item)

  11. Dequeue Enqueue Queue implemented using Array Steps: Item = NULL If(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 EndIf EndIf Return(Item) Stop 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 Stop Full/Empty Exception First Element / Last Element Normal Case

  12. 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.

  13. Algorithm: Enqueue 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 Stop

  14. 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.

  15. Algorithm: Dequeue Steps: Item = NULL If(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 EndIf EndIf Return(Item) Stop

  16. Tracing of Enqueue & Dequeue Array A Front = -1 Rear = -1 0 1 2 3 Trace the following steps for the above Queue of Length/Size 4. • Dequeue() • Enqueue(35) • Dequeue() • Enqueue(25) • Enqueue(45) • Enqueue(75) • Enqueue(65) • Enqueue(85) • Dequeue() • Enqueue(55) Problem

  17. Array A 15 35 1 2 3 Tracing of Enqueue & Dequeue F R Dequeue: Item = NULL If(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 EndIf EndIf Return(Item) Stop 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 EndIf Stop Enqueue(25) 15 35 25 1 2 3 F R Dequeue() 35 25 2 3 F R Enqueue(45) 35 25 2 3 R F Queue is full.

  18. Problem / Disadvantage Queue implemented using Array Array A Dequeue() 35 Enqueue(85): 45 65 35 55 55 45 25 65 45 25 25 Dequeue() 55 Queue is Full. Enqueue not possible. 1 2 3 4 5 Enqueue(65) F R F R F R Dynamic Queue, however, Both solutions require shifting of elements and hence inefficient. Solutions: 1) Change/Rewrite ‘Enqueue’ algorithm to shift the elements in case ‘Front’ is not at the starting of the Queue. 3) Implement the Queue using a Linked List. 2) Change/Rewrite ‘Dequeue’ algorithm to shift all the elements as soon as one element is deleted from the Queue.

  19. Circular Queue Queue Array A Enqueue(65) 45 35 55 25 65 85 75 Queue is Full. Enqueue not possible. Enqueue(85): 1 2 3 4 5 Dequeue() 35 Dequeue() 55 R F R R F F F F R Enqueue(85) Enqueue(75) 1 5 Queue is Full. Enqueue not possible. 85 65 Enqueue(15) Dequeue() 45 4 25 75 Dequeue() 25 2 R 45 Dequeue() 65 F 3

  20. Normal Queue v/s Circular Queue Steps: Conditions: Enqueue(35) Enqueue(35) Enqueue: First element (Exception). Enqueue(65) Enqueue: Queue is Full. Enqueue(55) Dequeue() Enqueue: Any other element (Normal). Enqueue(45) Dequeue() Enqueue(25) Dequeue: Last element (Exception). Dequeue() Enqueue(65) Dequeue: Queue is Empty. Dequeue() Dequeue: Any other element (Normal). Dequeue() Dequeue() Normal Queue Circular Queue F = 0 F = 0 45 45 35 55 25 35 55 25 R = 0 R = 0 1 2 3 4 1 2 3 4 F R F R F R F R F R F R F R F R

  21. Normal Queue Circular Queue Steps: If(Rear == U) next = L Else next = Rear + 1 EndIf 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] = Item EndIf 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 ENQUEUE

  22. Normal Queue Circular Queue Steps: If(Front == U) next = L Else next = Front + 1 EndIf Item = NULL If(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 EndIf EndIf Return(Item) Steps: Item = NULL If(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 EndIf EndIf Return(Item) DEQUEUE

  23. -2 -1 0 1 Trace the following steps for a Circular Queue of length/size ‘4’ implemented using array A[-2…1]. Enqueue: If(Rear == U) next = L Else next = Rear + 1 EndIf 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] = Item EndIf Dequeue: If(Front == U) next = L Else next = Front + 1 EndIf Item = NULL If(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 EndIf EndIf Return(Item) Tracing of Enqueue & Dequeue for Circular Queue F = -3 R = -3 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()

  24. 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.

  25. Steps: If(Rear == U) next = L Else next = Rear + 1 EndIf 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] = Item EndIf Stop Algorithm: Enqueue_CQ Find the next position of Rear. Enqueue the very first element. Enqueue any other element.

  26. 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.

  27. Steps: If(Front == U) next = L Else next = Front + 1 EndIf Item = NULL If(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 EndIf EndIf Return(Item) Stop Algorithm: Dequeue_CQ Find the next position of Front. Dequeue the last/only element. Dequeue any other element.

  28. Queue Front, Rear pointers? Normal Queue Circular Queue 45 40 35 55 25 30 50 20 1 2 3 4 1 2 3 4 F R Not possible to tell. Circular Queue 40 50 20 1 2 3 4 F R

  29. 1st Solution: 2nd Solution: Queue using a Linked List Enqueue: Insert_End Traversal? Yes Remove it? Enqueue: Insert_Front Dequeue: Delete_Front Traversal? No Dequeue: Delete_End Front Rear ptr Front Rear ptr ptr 30 NULL Front Rear ptr Queue_Head 10 NULL new NULL NULL 20 NULL new new Front = NULL Rear = NULL Dequeue() 30 Enqueue(30) Dequeue() 10 Enqueue(10) Dequeue() 20 Enqueue(20) Dequeue() Queue is Empty

  30. 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.

  31. Steps: new = GetNode(NODE) If(new == NULL) print “Memory Insufficient. Enqueue not possible.” Else new -> Data = Item new->Link = NULL If(Queue_Head->Link == NULL) Front = new Rear = new Queue_Head->Link = new Else Rear->Link = new Rear = new EndIf EndIf Stop Algorithm: Enqueue_LL Enqueue the very first element. Enqueue any other element.

  32. 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.

  33. Steps: Item = NULL If(Queue_Head->Link == NULL) print “Queue is empty. Dequeue not possible.” Else Item = Front->Data Queue_Head->Link = Front->Link ReturnNode(Front) If(Queue_Head->Link == NULL) Front = NULL Rear = NULL Else Front = Queue_Head->Link EndIf EndIf Return(Item) Stop Algorithm: Dequeue_LL Dequeue the last/only element. Dequeue any other element.

  34. Queue Rear Front Eject() Pop() Deletion Deletion Insertion Insertion Push(Item) Inject(Item) DEQUE (Not DEQUEUE) Double Ended Queue Pronounced as ‘DECK’

  35. 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.

  36. Rear Front Eject() Pop() Deletion Deletion Insertion Insertion Push(Item) Inject(Item) Rear Front Eject() Pop() Deletion Deletion Insertion Inject(Item) Rear Front Pop() Deletion Insertion Insertion Push(Item) Inject(Item) Types/Variations of DEQUE Queue Normal DEQUE / DECK Input-restricted DEQUE / DECK Output-restricted DEQUE / DECK

  37. Priority Queue Queue Items/Elements Rear Front Deletion H B D V Insertion 2 6 1 2 Priorities of the Item/Element Element can be inserted or deleted not only at the ends but at any position in the queue depending on the priority. As a result, it does not follow FIFO property and hence cannot be called a Queue. However it is still called a Queue.

  38. 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.

  39. 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.

More Related