ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

Slides:



Advertisements
Similar presentations
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
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.
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.
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.
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.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
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.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
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.
 Chapter 8 introduces the queue data type.  Several example applications of queues are given in that chapter.  This presentation describes the queue.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition 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,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
1 Queues. 2 Queue Which of the following cases use similar structures? Cars lined up at a tollgate Cars on a 4-lane highway Customers at supermarket check-out.
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.
Implementation of QUEUE For more notes and topics visit: eITnotes.com.
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.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
Data Structures – Week #4 Queues. January 12, 2016Borahan Tümer, Ph.D.2 Outline Queues Operations on Queues Array Implementation of Queues Linked List.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 7 Queues Introduction Queue applications Implementations.
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.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Queues Another Linear ADT Copyright © 2009 Curt Hill.
 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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Review Array Array Elements Accessing array elements
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
CC 215 Data Structures Queue ADT
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Stacks Stack: restricted variant of list
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Chapter 19: Stacks and Queues.
Queues.
Visit for more Learning Resources
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Queues Definition of a Queue Examples of Queues
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Data Structures & Programming
Presentation transcript:

ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue

2 Queue  Which of the following cases use similar structures?  Cars lined up at tollgate  Cars on a 4-lane highway  Customers at supermarket check-out  Books on a book shelf  Clients airport check-in counter  Pile of bath towels on linen closet shelf

3 Queue  Is a linear data structure with removal of items at one end (front) and insertion of items at the opposite end (rear)  FIFO – first-in-first-out frontrear

4 Queue Operations  Enqueue(item)  Insert item at rear  Dequeue()  Remove item at front  Front()  Return item at front  IsEmpty()  Is queue empty?  IsFull()  Is queue full?  Clear()  Make queue empty

The Queue Operations  A queue is like a line of people waiting for a bank teller. The queue has a front and a rear. $ Front Rear

The Queue Operations  New people must enter the queue at the rear. The C++ queue class calls this a push, although it is usually called an enqueue operation. $ Front Rear

The Queue Operations  When an item is taken from the queue, it always comes from the front. The C++ queue calls this a pop, although it is usually called a dequeue operation. $ Front Rear

The Queue Class  The C++ standard template library has a queue template class.  The template parameter is the type of the items that can be put in the queue. template class queue { public: queue( ); void push(const Item& entry); void pop( ); bool empty( ) const; Item front( ) const; … };

9 Array Implementation

10 Problem with Array Implementation frontrear  With frequent queueing and dequeing, end of array can be reached quickly.  Can we somehow use the empty spaces?

11 “Circular” Array

12 Queue Implementation (2)  Suppose, currently,  front = 5  rear = 9  For next values,  front = (front + 1) mod MAX_SIZE = 6  rear = (rear + 1) mod MAX_SIZE = 0 frontrear MAX_SIZE

13 queue.h (Declarations) #define MAX_SIZE 5 #define NIL -1 typedef char elemType; class Queue { public: Queue(); void enqueue(elemType item); void dequeue(); elemType front(); bool isEmpty(); bool isFull();...

14 queue.h (Declarations)... private: elemType data[MAX_SIZE]; int front; int rear; int count; // to simply full, empty }; // Note ; ‘;’

15 Queue() (Constructor) Queue::Queue(){ front = 0; rear = -1; count = 0; }

16 Enqueue() void Queue::enqueue(elemType item){ if (!full()){ rear = (rear + 1) % MAX_SIZE; data[rear] = item; count++; }

Efficiency of enqueue() operation  If it takes t seconds to enqueue an element into a queue of 1000 elements, how long does it take to enqueue one into a queue of 2000 elements?  The same time.  Thus, O(n) = 1. 17

18 Dequeue() void Queue::dequeue(){ if (!empty()){ result = data[front]; front = (front + 1) % MAX_SIZE; count--; }

Efficiency of dequeue() operation  If it takes t seconds to dequeue an element from a queue of 1000 elements, how long does it take to dequeue one from a queue of 2000 elements?  The same time.  Thus, O(n) = 1. 19

Your Turn  Suppose you want to add another queue operation: void clear(); // Postcondition: queue is made empty.  Write the implemenation code for the clear() operation.

21 Front() elemTypeQueue::front(){ if (!empty()){ return data[front]; else return NIL; }

22 empty() bool Queue::empty(){ return count == 0; }

Your Turn  Write the implementation code for the operation full().  bool Queue::isFull(); // Postcondition: True is returned when queue // is full; false, otherwise.

Array Implementation  Easy to implement  But it has a limited capacity with a fixed array  Or you must use a dynamic array for an unbounded capacity  Special behavior is needed when the rear reaches the end of the array. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] size 3 first 0 last 2

Linked List Implementation null 13  A queue can also be implemented with a linked list with both a head and a tail pointer. front_ptr rear_ptr

Linked List Implementation null 13  Which end do you think is the front of the queue? Why? front_ptr rear_ptr

Linked List Implementation null front_ptr 13  The head_ptr points to the front of the list.  Because it is harder to remove items from the tail of the list. rear_ptr Front Rear

Your Turn  Write the class interface—public and private sections—for the linked list version of class queue.

29 queue.h (Declarations) typedef int elemType; #define NIL -1 class Queue { public: Queue(); void enqueue(elemType item); void dequeue(); elemType front(); bool isEmpty();...

Queue Implemented by Linked List class Queue {... private: struct Node { elemType value; Node *next; Node (elemType item, Node *link){ value = item; next = link; }; } Node* front; Node* rear; int count; };

Constructor Queue Set head and rear to NULL. Set count to 0 End queue

Enqueue void enqueue (elemType item) Let temp point to new node(item, NULL). If (empty queue) Then Let front and rear point to new node Else Set rear->next pointer point to temp End If Let rear point to temp Increment count End enqueue

Enqueue void enqueue (elemType item) Node *t = new Nod(item, NULL); if (isEmpty(){ front = t; rear = t; } else { rear->next = t; } count++; }

Dequeue elemType dequeue() Set result to NIL If (queue is not empty) Then Set result to data of front node Let temp point to front node Set front to temp’s next Delete temp Decrement count End If If (front = NULL) Then Set rear to NULL Return result End dequeue

Dequeue elemType dequeue() elemType result = NIL; if (!isEmpty()){ result = front->value; Node *t = front; front = temp->next; delete t; count--; } if (front = NULL) rear = NULL; return result; }

Clear clear() Loop (while front != NULL) Set temp to front Set front to front’s next Delete temp End Loop Set rear to NULL Set count to 0 End clear

Print print() Set temp to front Loop(temp != NULL) Output temp’s data Set temp to temp’s next End Loop End print

 Like stacks, queues have many applications.  Items enter a queue at the rear and leave a queue at the front.  Queues can be implemented using an array or using a linked list. Summary

Priority Queue  Stand-by queue for a flight  One with the highest priority is removed in FIFO order—e.g., one who is attending funeral, wedding.  Print job queue  Job with the highest priority is removed in FIFO order—e.g. one with 3 pages or less  Emergency Room queue  Most serious case treated first

ADT Priority Queue  Insert item into PQ  Remove item with the highest priority frm PQ  Change priority of a particular item in PQ  Remove a particular item from PQ  Check if PQ is empty  Clear PQ

Implementation of Priority Queue queue Use an array of queues (where index doubles as priority level).

Data Structure for Priority Queue typedef int elemType; typedef int priorityType; const priorityType MAX = 5; class PriorityQueue { PriorityQueue(); void insert(elemType item, prioritType pNum); elemType removeNext();... public:... private: Queue pq[MAX + 1]; // index 0 is unused };

Constructor PriorityQueue(){ for (int i = 0; i < MAX + 1; i++){ pqp[i].clear(); } }

Insert(elemType item, priorityType pNum) insert (elemType item, priorityType pNum){ } class PriorityQueue { PriorityQueue(); void insert(elemType item, prioritType pNum); elemType removeNext();... public:... private: Queue pq[MAX + 1]; // index 0 is unused };