Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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 Queue Queue in front of a counter. Process synchronization in 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.

3 Queue Insert an element 30 10 Enqueue 1020 Delete an element 30 10 Dequeue 20 RearFront Delete an element Working

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 Queue Implemented using an Array 3010206040 12345 Array A Enqueue (Insert an element) 30 10206040 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

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

8 Queue Implemented using an Array Enqueue (Insert an element) 3010206040 12345 Array A Dequeue30 Dequeue10 Dequeue20 Dequeue60 Dequeue40 3010206040 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 Enqueue 3010206040 12345 Array A 3010206040 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 RRRR Rear = Rear + 1 EndIf 30Item Dequeue 50 U L

10 Queue implemented using Array Dequeue 30102060 12345 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 FR Dequeue Item = A[Front] Rear = 0 A[Front] = NULL // 301020 60 Item = NULL Return(Item) FF F L L-1

11 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 EndIf Stop 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 Return(Item) Stop Enqueue Dequeue Full/Empty Normal Case Exception First Element / Last Element

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 Return(Item) Stop

16 Tracing of Enqueue & Dequeue 0123 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

17 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 EndIf Stop 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 Return(Item) Stop 123 Array A 1535 FR 123 1535 FR 25 23 35 FR 25 23 35 R 25 Enqueue(45) Enqueue(25) Dequeue() F Queue is full.

18 Queue implemented using Array 25 65 12345 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() 3555 45 Dequeue() Enqueue(65) RF F 35 55 4525 65 R 25 5545 Dynamic Queue, however, Both solutions require shifting of elements and hence inefficient.

19 Queue 25 65 12345 Array A RF Enqueue(85): Queue is Full. Enqueue not possible. 3555 45 Enqueue(65) RF F 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 5 85 75 45 25 65 R F Dequeue() 45 FF Dequeue() 25 Dequeue() 65

20 Normal Queue v/s Circular Queue 25 1234 3555 45 F = 0 R R = 0 F 25 1234 3555 45 F = 0 R R = 0 F Normal QueueCircular Queue Enqueue(35) Enqueue(55) Enqueue(45) Dequeue() Enqueue(25) RFRFRFRFRFRF Enqueue(65) 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)

21 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 = 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 Normal QueueCircular Queue ENQUEUE

22 Normal QueueCircular Queue 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 Return(Item) 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 Return(Item)

23 Tracing of Enqueue & Dequeue for Circular Queue 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 Return(Item) 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 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() -201 F = -3 R = -3

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 Algorithm: Enqueue_CQ 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 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 Algorithm: Dequeue_CQ 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 Return(Item) Stop Find the next position of Front. Dequeue the last/only element. Dequeue any other element.

28 Queue 25 1234 3555 45 Normal Queue 20 1234 3050 40 Circular Queue FRNot possible to tell. Front, Rear pointers? 20 1234 50 40 Circular Queue FR

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

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 Algorithm: Enqueue_LL 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 Stop 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 Algorithm: Dequeue_LL 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 Return(Item) Stop Dequeue the last/only element. Dequeue any other element.

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

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 Queue Types/Variations of DEQUE Rear Front Insertion Deletion Insertion Inject(Item) Push(Item) Pop() Rear Front Deletion Insertion Deletion Inject(Item) Eject() Pop() Rear Front Deletion Insertion Deletion Insertion Inject(Item) Eject() Push(Item) Pop() Normal DEQUE / DECK Input-restricted DEQUE / DECK Output-restricted DEQUE / DECK

37 Queue Rear Front Insertion Deletion H BDV 2 612 Items/Elements Priorities of the Item/Element Priority Queue 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.


Download ppt "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."

Similar presentations


Ads by Google