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.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Data Structure HKOI training /4/2010 So Pak Yeung.
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
Lecture - 9 On Queues. Prepared by, Jesmin Akhter, Lecturer, IIT,JU QUEUES A Queue is a linear list of elements in which deletions can take place only.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
MSc.It :- Ali Abdul Karem Habib Kufa University / mathematics & Science Of Computer.
Queue Overview Queue ADT Basic operations of queue
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Unit : Overview of Queues.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Chapter 16 Stack and Queues part2
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CHP-4 QUEUE.
Queue 1 Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Queues by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Stack Any Other Data Structure Array Linked List
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
Queues CSCI 3333 Data Structures. Acknowledgement  Dr. Bun Yue  Mr. Charles Moen  Dr. Wei Ding  Ms. Krishani Abeysekera  Dr. Michael Goodrich  Dr.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
CHP-4 QUEUE. 1.INTRODUCTION  A queue is a linear list in which elements can be added at one end and elements can be removed only at other end.  That.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
UNIVERSAL COLLEGE OF ENGG. AND TECH. 3 RD IT. QUEUE ( data structure)
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Linked List X Header X 4000 ptr Question: Search a value 40 in the linked list. Steps: ptr = Header->Link While(ptr.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Introduction Of Queue. Introduction A queue is a non-primitive linear data structure. It is an homogeneous collection of elements in which new elements.
Computer Engineering Rabie A. Ramadan Lecture 6.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
Array 10 GB Hard Disk 2 GB 4 GB2 GB 3 GB DATA 4 GB Free Store data in the form of Array (Continuous memory locations) Solution-1: No Solution. Memory Insufficient.
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
UNIT-II Topics to be covered Singly linked list Circular linked list
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Data Structures Using C, 2e
Queues.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Program based on queue & their operations for an application
Chapter 15 Lists Objectives
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Queue data structure.
Stacks and Queues.
Queues Mohammad Asad Abbasi Lecture 5
CMSC 341 Lecture 5 Stacks, Queues
Stacks, Queues, and Deques
DATA STRUCTURE QUEUE.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Queues FIFO Enqueue Dequeue Peek.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Presentation transcript:

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

Queue Insert an element Enqueue 1020 Delete an element Dequeue 20 RearFront 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.

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

Queue Implemented using a Linked List Enqueue (Insert an element) 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

Queue Implemented using an Array Enqueue (Insert an element) Array A Dequeue30 Dequeue10 Dequeue20 Dequeue60 Dequeue 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 Array A 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

Queue implemented using Array Dequeue Array A 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 // Item = NULL Return(Item) FF F 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 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

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: 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

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: 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

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

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 FR FR R 25 Enqueue(45) Enqueue(25) Dequeue() F Queue is full.

Queue implemented using Array 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() Dequeue() Enqueue(65) RF F R Dynamic Queue, however, Both solutions require shifting of elements and hence inefficient.

Queue Array A RF Enqueue(85): Queue is Full. Enqueue not possible Enqueue(65) RF F Dequeue() Enqueue(85) Circular Queue R Enqueue(75) R 75 Enqueue(15) Queue is Full. Enqueue not possible R F Dequeue() 45 FF Dequeue() 25 Dequeue() 65

Normal Queue v/s Circular Queue F = 0 R R = 0 F 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)

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

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)

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

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

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

Queue Normal Queue Circular Queue FRNot possible to tell. Front, Rear pointers? Circular Queue FR

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

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

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

Queue Rear Front 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.

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

Queue Rear Front Insertion Deletion H BDV 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.

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.