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.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Queue Definition Ordered list with property: –All insertions take place at one end (tail) –All deletions take place at other end (head) Queue: Q = (a 0,
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)
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
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.
CHAPTER 7 Queues.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
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.”
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.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
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.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
1 Queues CPS212 Gordon College. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank, food.
Queues.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
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.
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.”
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
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.
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 (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Stacks And Queues Chapter 18.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Data Structures and Algorithm Analysis Dr. Ken Cosh Stacks ‘n’ 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.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
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.
Winter 2006CISC121 - Prof. McLeod1 Stuff Solution to midterm is posted. Marking has just started… Lab for this week is not posted (yet?). Final exam (full.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
Queues Another Linear ADT Copyright © 2009 Curt Hill.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
 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.
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
Queues.
Stacks and Queues.
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Chapter 19: Stacks and Queues.
Queues.
Revised based on textbook author’s notes.
CSC 143 Queues [Chapter 7].
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CE 221 Data Structures and Algorithms
Queues.
Queues Definition of a Queue Examples of Queues
Data Structures – Week #4
Getting queues right … finally (?)
Data Structures & Programming
Presentation transcript:

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 Network packets Unlike a stack, both ends are used – One for adding new elements – One for removing elements The last element has to wait until all elements preceding it on the queue are removed – For this reason, it is a First in/First out (FIFO) structure

Queues as ADTs Data – A collection of elements of the same type Operations – clear() – isEmpty() – enqueue(el) – dequeue() – firstEl()

Queues as ADTs (cont’d)

Queues as ADT’s (cont’d) Implementation – With a doubly linked list, similar to stack, code in book

Priority Queues In many situations, FIFO scheduling must be overruled using some priority criteria – E.g., a handicapped person that arrives to a post office may have priority over others and may not have to wait in line – In a priority queue, elements are dequeued according to their priority and their current queue position

Implementation of a priority queue – Linked list implementation of a priority queue If list is ordered, adding an element is O(n), searching for highest/lowest-priority element is O(1) Can also have list of list If list is unordered, adding an element is O(1), searching for highest/lowest-priority element is O(n)

7 Implementing the Stack, Queue As an ADT there are different ways for the Stack Queue Implementation, the pros and cons? Possible Ways of Implementation. – Dynamic array based implementation. Dynamic Array (1) Circular Array (2) – Linked list implementation The implementation is like a linked list but with only the necessary methods (3) Reuse a Linked List object (book) (4) Inheritance from a Linked List

8 Queue: Dynamic Array Implementation #pragma once #include template class Queue { public: Queue(size_t capacity = 1000); ~Queue(); bool IsEmpty() const; bool IsFull() const; // Add a value to the end of the Queue. void Enqueue(const T& value); // Remove and return value at front of Queue. T Dequeue(); // Retrieve value at front of Queue without removing it. T Front() const;

9 Queue.h // Display Queue contents. void Display(std::ostream& out) const; void Clear(); private: T* data; int size; int head; int tail; };

Dynamic Array Implementation (1) When new jobs enter queue, may run out of space. The array may not really be full though, if items have been removed from queue. Possible Solution: When tail = (maxSize – 1) attempt to shift data forwards into empty spaces and then do Add.

Worst Case Queue Shift Worst Case: – Shift entire queue: Cost of O(n-1) – Do every time perform an add – Too expensive to be useful Worst case is not that unlikely, so this suggests finding an alternative implementation.

‘ Circular ’ Array Implementation (2) Basic Idea: Allow the queue to wrap-around Implement with addition mod size: tail = (tail + 1) % queueSize; N-1 N-2 J1 J2 J3 J N-1 N-2 J2 J3 J1

Linked Queues (3) class Queue { public: Queue(); ~Queue(); void enqueue(const int); int* dequeue(int&); bool isEmpty(); private: QueueNode* head; QueueNode* tail; void QueueEmpty(); } Implementation is similar to linked-list, addToTail, deleteFromHead Class QueueNode{ public: QueueNode(int d, QueueNode * l); int data; QueueNode *link; };

Queues as ADT’s (4) Reuse a doubly linked list, similar to stack, code in book class Queue { public: Queue(); ~Queue(); void enqueue(const int el) {lst.addToTail(el);} int* dequeue(int&); bool isEmpty(); private: DLList lst; }

Queue ADT Implementations No matter how queue is implemented, -different internal representation, - different implementation of the methods the users that use the Queue class will see the same functionalities However, the efficiency of different operation might be different Analyze them. What about priority queue?