Queues Dr. Anwar Ghani Lecture 06: Queue Data Structures

Slides:



Advertisements
Similar presentations
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Advertisements

Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
 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.
What is a Queue? A queue is a FIFO “first in, first out” structure.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Queue Overview Queue ADT Basic operations of queue
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Stack and Queue COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Starting Out with C++, 3 rd Edition Chapter 18 Stacks and Queues.
Starting Out with C++, 3 rd Edition Chapter 18 – Stacks and Queues.
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.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
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.
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.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
CS 204 Stacks and Queues (not in any book that we are using) From Starting out with C++, 7th edition, by Gaddis, Walters and Muganda (chapter 18, pp
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Data Structures – Week #4 Queues. January 12, 2016Borahan Tümer, Ph.D.2 Outline Queues Operations on Queues Array Implementation of Queues Linked List.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
 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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Review Array Array Elements Accessing array elements
What is a Queue? Queue is a linear data structure in which the insertion and deletion operations are performed at two different ends. In a queue data structure,
Lecture No.09 Data Structures Dr. Sohail Aslam
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
CC 215 Data Structures Queue ADT
Queue data structure.
Queue.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
Stacks.
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Chapter 19: Stacks and Queues.
Queues.
(not in any book that we are using)
Memory Organization Process 1 (browser) Code Process 3 (word)
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Queues FIFO Enqueue Dequeue Peek.
Stacks and Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
CSCS-200 Data Structure and Algorithms
Data Structures – Week #4
Data Structures & Programming
Presentation transcript:

Queues Dr. Anwar Ghani Lecture 06: Queue Data Structures International Islamic University, Islamabad

Lecture 06: Queue Data Structures “A Queue is a special kind of list, where items are inserted at one end (the rear) And deleted at the other end (the front)”. Other Names First In First Out (FIFO) International Islamic University, Islamabad

Line of customers in a bank Lecture 06: Queue Data Structures Common Operations on Queues 1. 2. 3. 4. 5. MAKENULL(Q): Makes Queue Q be an empty list. FRONT(Q): Returns the first element on Queue Q. ENQUEUE(x,Q): Inserts element x at the end of Queue Q. DEQUEUE(Q): Deletes the first element of Q. EMPTY(Q): Returns true if and only if Q is an empty queue. Example: Line of customers in a bank International Islamic University, Islamabad

Applications of Queues Lecture 06: Queue Data Structures Applications of Queues Operating system multi-user/multitasking environments, where several users or task may be requesting the same resource simultaneously. Communication Software queues to hold information received over networks and dial up connections. (Information can be transmitted faster than it can be processed, so is placed in a queue waiting to be processed) Some other? International Islamic University, Islamabad

Implementation Static Dynamic Lecture 06: Queue Data Structures Implementation Static Queue is implemented by an array, and size of queue remains fix Dynamic A queue can be implemented as a linked list, and expand or shrink with each enqueue or dequeue operation. International Islamic University, Islamabad

Lecture 06: Queue Data Structures International Islamic University, Islamabad

. A pointer Implementation of Queues x y z Keep two pointers: Lecture 06: Queue Data Structures A pointer Implementation of Queues Keep two pointers: FRONT: A pointer to the first element of the queue. REAR: A pointer to the last element of the queue. . Front x y z Rear International Islamic University, Islamabad

. A pointer Implementation of Queues MAKENULL(Q) ENQUEUE(x,Q) x Lecture 06: Queue Data Structures A pointer Implementation of Queues MAKENULL(Q) NULL Q.front Q.Rear ENQUEUE(x,Q) . x Q.front Q.Rear International Islamic University, Islamabad

. . . A pointer Implementation of Queues ENQUEUE(y,Q) x y DEQUEUE(Q) y Lecture 06: Queue Data Structures A pointer Implementation of Queues ENQUEUE(y,Q) . x y Q.front Q.Rear DEQUEUE(Q) . . y Q.front Q.Rear International Islamic University, Islamabad

A class for Dynamic Queue implementation Lecture 06: Queue Data Structures A class for Dynamic Queue implementation class DynIntQueue { private: struct QueueNode int value; QueueNode *next; }; QueueNode *front; QueueNode *rear; int numItems; public: DynIntQueue(void); ~DynIntQueue(void); void enqueue(int); int dequeue(void); bool isEmpty(void); void makeNull(void); International Islamic University, Islamabad

Implemenaton Lecture 06: Queue Data Structures //************************ // Constructor * DynIntQueue::DynIntQueue(void) { front = NULL; rear = NULL; numItems = 0; } // Destructor * DynIntQueue::~DynIntQueue(void) makeNull(); International Islamic University, Islamabad

Lecture 06: Queue Data Structures //******************************************** // Function enqueue inserts the value in num * // at the rear of the queue. * void DynIntQueue::enqueue(int num) { QueueNode *newNode; newNode = new QueueNode; newNode->value = num; newNode->next = NULL; if (isEmpty()) front = newNode; rear = newNode; } else rear->next = newNode; rear = newNode; numItems++; International Islamic University, Islamabad

Lecture 06: Queue Data Structures //********************************************** // Function dequeue removes the value at the * // front of the queue, and copies it into num. * int DynIntQueue::dequeue(void) { QueueNode *temp; int num; if (isEmpty()) cout << "The queue is else { num = front->value; temp = front->next; delete front; front = temp; numItems--; } return num; empty.\n"; } International Islamic University, Islamabad

Lecture 06: Queue Data Structures //********************************************* // Function isEmpty returns true if the queue * // is empty, and false otherwise. * bool DynIntQueue::isEmpty(void) { if (numItems) return false; else return true; } International Islamic University, Islamabad

Lecture 06: Queue Data Structures //******************************************** // Function makeNull dequeues all the elements * // in the queue. * void DynIntQueue::makeNull(void) { while(!isEmpty()) dequeue(); } International Islamic University, Islamabad

value =iQueue.dequeue(); cout << value << endl; Lecture 06: Queue Data Structures Program // This program demonstrates the DynIntQeue void main(void) { DynIntQueue iQueue; class cout << "Enqueuing 5 items...\n"; // Enqueue 5 items. for (int x = 0; x < 5; x++) iQueue.enqueue(x); // Deqeue and retrieve all items in the queue cout << "The values in the queue were:\n"; while (!iQueue.isEmpty()) { int value; value =iQueue.dequeue(); cout << value << endl; } International Islamic University, Islamabad

Program Ouput Lecture 06: Queue Data Structures Enqueuing 5 items... The values in the queue 0 were: 1 2 3 4 International Islamic University, Islamabad

Array Implementation Lecture 06: Queue Data Structures When queue is empty both front and rear are set to -1 While enqueueing increment rear by 1, and while dequeueing increment front by 1 When there is only one value in the Queue, both rear and front have same index Front First Element Second Elem. ent . Rear Last Element maxlength International Islamic University, Islamabad

Array Implementation How can we insert more elements? Rear index can Lecture 06: Queue Data Structures Array Implementation 5 4 6 7 8 1 2 Front=0 Rear=6 3 4 5 6 7 8 8 7 6 1 2 Front=4 Rear=6 3 4 5 6 7 8 7 6 12 67 1 2 Front=5 Rear=8 3 4 5 6 7 8 How can we insert more elements? Rear index can not move beyond the last element…. International Islamic University, Islamabad

Drawbacks of Linear Queue

Solution: Using circular queue Lecture 06: Queue Data Structures Solution: Using circular queue Allow rear to wrap around the array. if(rear == queueSize-1) rear = 0; else rear++; Or use module arithmetic rear = (rear + 1) % queueSize; International Islamic University, Islamabad

Rear=(Rear+1) mod Queue Size = (8+1) mod 9 = 0 39 7 6 12 67 Data Structures Lecture 06: Queue 7 6 12 67 1 2 Front=5 Rear=8 3 4 5 6 7 8 Enqueue 39 Rear=(Rear+1) mod Queue Size = (8+1) mod 9 = 0 39 7 6 12 67 1 2 Front=5 Rear=0 3 4 5 6 7 8 International Islamic University, Islamabad

Lecture 06: Queue Data Structures Implementation class IntQueue { private: int *queueArray; int queueSize; int front; int rear; int numItems; public: IntQueue(int); ~IntQueue(void); void enqueue(int); int dequeue(void); bool bool void isEmpty(void); isFull(void); clear(void); }; Note, the member function clear, which clears the queue by resetting the front and rear indices, and setting the numItems to 0. International Islamic University, Islamabad

IntQueue::IntQueue(int s) //constructor Lecture 06: Queue Data Structures IntQueue::IntQueue(int s) //constructor { queueArray = new int[s]; queueSize = s; front = -1; rear = -1; numItems = 0; } IntQueue::~IntQueue(void) //destructor delete [] queueArray; International Islamic University, Islamabad

//******************************************** Lecture 06: Queue Data Structures //******************************************** // Function enqueue inserts the value in num * * // at the rear of the queue. //******************************************** void IntQueue::enqueue(int num) { if (isFull()) cout << "The else { // Calculate rear = (rear queue is full.\n"; the new rear position + 1) % queueSize; // Insert new item queueArray[rear] = num; // Update item count numItems++; } International Islamic University, Islamabad

Lecture 06: Queue Data Structures //********************************************* // Function dequeue removes the value at the * // front of the queue, and copies t into num. * int IntQueue::dequeue(void) { if (isEmpty()) cout << else { // Move front = "The queue is empty.\n"; front (front + 1) % queueSize; item // Retrieve the front int num = queueArray[front]; // Update item count numItems--; } return num; International Islamic University, Islamabad

Lecture 06: Queue Data Structures //********************************************* // Function isEmpty returns true if the queue * // is empty, and false otherwise. * bool IntQueue::isEmpty(void) { if (numItems) return false; else return true; } International Islamic University, Islamabad

//******************************************** Lecture 06: Queue Data Structures //******************************************** // Function isFull returns true if the queue * // is full, and false otherwise. * bool IntQueue::isFull(void) { if (numItems < queueSize) return false; else return true; } International Islamic University, Islamabad

//******************************************* Lecture 06: Queue Data Structures //******************************************* // Function clear resets the front and rear * // indices, and sets numItems to 0. * void IntQueue::clear(void) { front = - 1; rear = - 1; numItems = 0; } International Islamic University, Islamabad

//Program demonstrating the IntQueue class Lecture 06: Queue Data Structures //Program demonstrating the IntQueue class void main(void) { IntQueue iQueue(5); cout << "Enqueuing 5 items...\n"; // Enqueue 5 items. for (int x = 0; x < 5; x++) iQueue.enqueue(x); // Attempt to enqueue a 6th item. cout << "Now attempting to iQueue.enqueue(5); // Deqeue and retrieve all cout << "The values in the while (!iQueue.isEmpty()) { int value; enqueue again...\n"; items in the queue queue were:\n"; value=iQueue.dequeue(); cout << value << endl; } International Islamic University, Islamabad

Program Output Lecture 06: Queue Data Structures Enqueuing 5 items... Now attempting to enqueue again... The queue is full. The 0 values in the queue were: 1 2 3 4 International Islamic University, Islamabad