Queues Another Linear ADT Copyright © 2009 Curt Hill.

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

Stacks, Queues, and Linked Lists
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
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.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists.
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.
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.
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.
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 Structures and Algorithms Lecture (Queues) Instructor: Quratulain.
Data Structure Dr. Mohamed Khafagy.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Stacks, Queues, and Deques
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.”
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
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.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
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)
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Chapter 7 Queues Introduction Queue applications Implementations.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
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.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
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,
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
 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.
Review Array Array Elements Accessing array elements
Data Structures Using C++ 2E
Data Structures Using C, 2e
Queues.
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
UNIT II Queue.
Data Structure By Amee Trivedi.
CC 215 Data Structures Queue ADT
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Chapter 19: Stacks and Queues.
Queues.
CSC 143 Queues [Chapter 7].
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Queues: Implemented using Arrays
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Circular Queues: Implemented using Arrays
Queues Definition of a Queue Examples of Queues
Stacks, Queues, and Deques
Getting queues right … finally (?)
Presentation transcript:

Queues Another Linear ADT Copyright © 2009 Curt Hill

Introduction Bank and grocery store lines Unlike Stack, two places of action Adds are at one end –Rear or tail Deletes at the other –Head or front First In First Out Copyright © 2009 Curt Hill

Operations Create a queue Append a request for service Service a request Is the queue full? Is the queue empty? Queue size Copyright © 2009 Curt Hill

Queue of Integer In C++ class Queue{ public: bool enter(int); int remove(); bool remove(int &); bool isFull(); bool isEmpty(); int size(); private: … }; Copyright © 2009 Curt Hill

Potential Data Structures Straight array Circular array Files Linked list Copyright © 2009 Curt Hill

Straight array Two possibilities Move every element after a service –Inefficient for queue of any size Move head and tail –Only useful if queue lengths are short –And queue is regularly cleared Copyright © 2009 Curt Hill

Circular array Use modulo arithmetic Head or tail can wrap around from highest to lowest subscript This is classic OS buffer technique Copyright © 2009 Curt Hill

Circular array queues Declare the array to be MAX long Two indices –One points to where things enter - tail –Other where they are removed - head Each is incremented with something like this: index = ++index%MAX; –Circular increment Unless MAX is power of two it requires a division Copyright © 2009 Curt Hill

Questions What does the head and tail point at? –First unused –Last used Do we increment before using or after? Does not matter as long as consistent We also end up with the same problems Copyright © 2009 Curt Hill

Lets Assume … For the sake of presentation That head (item next to leave) points at an unused –Must be incremented first That tail (end of queue) points at an unused –Must be incremented after How should they be initialized for an empty queue Copyright © 2009 Curt Hill

Initialization Head and tail should be set to the same index –Does not matter which, so zero is fine An empty queue has the two the same How then do we signify a full queue? Two possibilities: –Waste a slot –Additional boolean Copyright © 2009 Curt Hill

The full queue If we always maintain one empty slot –Then full is (tail+1)%MAX == head IF additional boolean –Call it full and set to false initially –If tail catches head set full to true –Any call to remove sets to false –Most of the time boolean is false Linked queues do not have this problem Copyright © 2009 Curt Hill

Files Perhaps the worst unless is queue is enormous Open and close are inefficient Only really useful if queue length is very long And lots of appends followed by lots of serves resulting in clearing of queue Spooling is an example Copyright © 2009 Curt Hill

Linked list Either use a hand designed or STL list Add things on one end Remove from other For an STL list use push_back and pop_front as your pair or the reverse Copyright © 2009 Curt Hill

Linked queues The basic approach is similar to a linked stack What used to be the top of the stack is the head of the queue One additional pointer points to tail of the queue, which is the last item on the list When an item leaves the queue the code is essentially the same as pop except that the pointer has to be checked for NULL When an item enters the queue: –New item created –Pointer to last item updates the NULL in it to point to newly created item –Tail pointer then shifts down –But we must check that the tail pointer is not NULL, which we did not need to do with stack Copyright © 2009 Curt Hill

Problems with queues Queue, as described, have no notion of priority How should this be solved? Two possibilities –Multiple queues –Priority queue Copyright © 2009 Curt Hill

Multiple queues If there are few possible priorities Just use one queue for each value Always serve the first item on the highest non-empty queue An OS often has priority levels fewer than 16 and often less Copyright © 2009 Curt Hill

Priority queue The only thing needing change is the method that adds a new item to the queue It does not put it at the end but puts it just below the lowest item with same or higher priority A simple append now becomes a search for correct position Much easier for a queue made from a list Copyright © 2009 Curt Hill

Operating system example Any operating system is a good example because –There are resources –These resources are requested –The requests are queued –Some of the queues can be quite long CPU resource –Terminals waiting for service after an enter –Programs waiting for a time slice –Non-interactive programs waiting CPU time I/O subsystem –Programs waiting to do a disk read/write –Prints to be printed at one of several printers –Other output to be given (graph, punch, whatever) Copyright © 2009 Curt Hill

More OS examples Memory –Programs waiting a memory page –Memory pages waiting for use Other –Telecommunication devices waiting for service –Queue of letters to be read –Queue of system wide messages –Buffers of files are a queue Copyright © 2009 Curt Hill