QUEUES What are Queues? Creating a Queue Enqueuing and Dequeuing Testing for an Empty Queue.

Slides:



Advertisements
Similar presentations
Singly linked lists Doubly linked lists
Advertisements

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.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Stack & Queues COP 3502.
So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
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.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
CS Data Structures II Review COSC 2006 April 14, 2017
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Stacks, Queues, and Deques
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.
Stacks, Queues, and Deques. 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.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Stacks, Queues, and Deques
13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.
Data Structures - Queues
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Object Composition Interfaces Collections Covariance Object class Programming using C# LECTURE 10.
Linked Structures See Section 3.2 of the text.. First, notice that Java allows classes to be recursive, in the sense that a class can have an element.
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,
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’
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
LINKED LISTS Linear Linked List. November 1, 2005Linked Lists2 of 48 Linked Lists Like stacks and queues, linked lists are lists of nodes that point to.
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.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
Understanding Data Types and Collections Lesson 2.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
Stacks1 of 32 STACKS What are Stacks? Creating a Stack Pushing and Popping Testing for an Empty Stack Burgers! © Pearson Education.
Question of the Day  Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
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.
Queues. Like Stacks, Queues are a special type of List for storing collections of entities. Stacks are Lists where insertions (pushes) and deletions (pops)
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
CS 367 Introduction to Data Structures Lecture 5.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
0 of 36 Andries van Dam  /05/15 Reminder: TA Hour Policies If you hit a bug, you must be able to show significant effort of fixing it on your own.
OOP Basics Classes & Methods (c) IDMS/SQL News
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
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 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Linked Data Structures
Elementary Data Structures
CC 215 Data Structures Queue ADT
Week 3 - Friday CS221.
Stacks and Queues.
Stack and Queue APURBO DATTA.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Stacks, Queues, and Deques
Queues.
Stacks, Queues, and Deques
Lesson Objectives Aims
Stacks and Queues.
Stacks, Queues, and Deques
Ordered Structures Wellesley College CS230 Lecture 10
Lecture 9: Stack and Queue
Presentation transcript:

QUEUES What are Queues? Creating a Queue Enqueuing and Dequeuing Testing for an Empty Queue

© 2006 Pearson EducationQueues2 of 32 Queues Similar to stacks, but items are removed in different order Useful in different situations: –stacks used for activation records –queues used for event processing Information retrieved in same order in which it was stored –FIFO: first in, first out Examples: –standing in line Like stacks, queues store Object s –again, use generics to restrict class types of data passed to queue October 27, 2005

© 2006 Pearson EducationQueues3 of 32 Enqueuing and Dequeuing Enqueuing: adds a node Dequeuing: removes a node BEFORE ENQUEUING AFTER ENQUEUING person removed from queue BEFORE DEQUEUING AFTER DEQUEUING head of queue tail of queue person to add head of queue tail of queue

© 2006 Pearson EducationQueues4 of 32 Syntax: The Movie Line A movie line queue may be instantiated to only accept object of type person: Right now Person only stores _myName –subclasses might store more information (phone number, favorite foods, pictures, etc.) Different subclasses of people can be stored in a queue –we use generics to only allow Person s –Men and Women could be subclasses of Person –both types of people would be able to stand in our line Number of plates on dispenser can vary from 0 to... public class Person { private String _myName; public Person (String name) { _myName = name; } public String getName() { return _myName; } } October 27, 2005

© 2006 Pearson EducationQueues5 of 32 Special Notes In a stack, everything is added and removed from top Bottom : –was never removed –handled special cases when stack was empty Queues require two special nodes: one to mark head of queue and one to mark tail Queue dequeues via head node; enqueues via tail node. Neither head nor tail can be removed Unlike bottom of stack, which had no references, tail node needs to communicate with last internal node of queue in order to give it reference to newly enqueued node Head node needs reference to next node in order to dequeue it October 27, 2005

© 2006 Pearson EducationQueues6 of 32 The Complete Queue Let’s look at a complete queue. Note that Queue class contains references to head and tail nodes –head and tail are permanent parts of queue HeadNode Node _next InternalNode ElementType _data Node _next InternalNode ElementType _data Node _next InternalNode ElementType _data Node _next Queue HeadNode _head TailNode _tail TailNode Node _prev... October 27, 2005

© 2006 Pearson EducationQueues7 of 32 Example: Enqueue (1 of 3) Let’s look at an example of enqueuing. This is our empty queue: Note that head and tail nodes point to each other HeadNode Node _next Queue HeadNode _head TailNode _tail TailNode Node _prev October 27, 2005

© 2006 Pearson EducationQueues8 of 32 Example: Enqueue (2 of 3) Here’s our queue right BEFORE one enqueue: Note: We haven’t forgotten about Queue class that contains head and tail; we just didn’t draw it for simplicity A new Internal Node is instantiated, “Mike” is the data stored in _myName References that have been newly set or changed and new instances shown in red HeadNode Node _next TailNode Node _prev InternalNode ElementType _data Node _next Person Mike October 27, 2005

© 2006 Pearson EducationQueues9 of 32 Example: Enqueue (3 of 3) Here’s our queue after the second enqueue (new node is added at end): Once again, reference in red are the only ones queue needs to change HeadNode Node _next TailNode Node _prev InternalNode ElementType _data Node _next Person Sara InternalNode ElementType _data Node _next Person Mike October 27, 2005

© 2006 Pearson EducationQueues10 of 32 Let’s Dequeue Note here’s our list after one dequeue (node closest to head node is removed): This looks simpler than enqueue of empty queue: only one reference needed to change HeadNode Node _next TailNode Node _prev InternalNode ElementType _data Node _next Person Sara October 27, 2005 InternalNode ElementType _data Node _next Person Mike Garbage Collected!

© 2006 Pearson EducationQueues11 of 32 A Tricky Dequeue Let’s dequeue the last internal node: Now there are two links that need to change: HeadNode Node _next TailNode Node _prev October 27, 2005 InternalNode ElementType _data Node _next Person Sara boundary case: dequeue will need to behave differently when removing last internal node Garbage Collected!

© 2006 Pearson EducationQueues12 of 32 Empty? We also need to be able to check if Queue representing our movie line is empty In stack, isEmpty() was easy to write -- Stack asked top node and InternalNode returned false; Bottom returned true Queue will always refer to HeadNode and TailNode only. How can we check isEmpty() ? Queue will ask HeadNode, which will in turn ask _next node. As before, InternalNode will return false and TailNode will return true Very similar to Stack, but with one extra level of delegation October 27, 2005

© 2006 Pearson EducationQueues13 of 32 Nodes Before we can build queue, we need to define smart nodes which make it up Once again, we need generic superclass from which all concrete nodes will inherit enqueue and dequeue need to reset some references. We will define set methods for all nodes to help them do this Three different types of nodes hold different references and will react very differently to these methods, but –we still want to be able to call these methods using reference to generic node –so, we have to declare their common methods in superclass - polymorphism again October 27, 2005

© 2006 Pearson EducationQueues14 of 32 Generic Nodes Code for generic node: setNext and setPrevious are empty methods which do nothing by default isEmpty will return true for tail node, false for internal nodes Node superclass declares dequeue message, but not enqueue : –in stack, Node declared pop message, but not push –every type of object in queue responds differently to dequeue message so is declared abstract in superclass –enqueue is specific to TailNode so declare it there // generic Queue Node public abstract class Node { public void setNext(Node newNextNode) {} public void setPrevious(Node newPrevNode) {} abstract public boolean isEmpty(); abstract public Node dequeue (ObjectHolder result); } October 27, 2005

© 2006 Pearson EducationQueues15 of 32 Head Node Head node is responsible for starting dequeue because queue removes things from front: HeadNode inherits setPrevious method but does not redefine it, since head node has no _previous reference; instead redefines setNext which it uses to jump around dequeued node to reference new first node We’ve left most of these methods undefined; we’ll define them later public class HeadNode extends Node { private Node _next; public HeadNode() {... } // methods redefined from superclass public Node dequeue (Holder result) {...} public boolean isEmpty() {...} public void setNext(Node newNextNode){ _next = newNextNode; } } October 27, 2005

© 2006 Pearson EducationQueues16 of 32 Tail Node Conversely, tail node handles enqueue : TailNode inherits setNext method but does not redefine it, since tail node has no _next reference; instead redefines setPrevious Since tail node responds to dequeue message, must redefine it We will define the rest of these methods later public class TailNode extends Node { private Node _prev; public TailNode(Node newPrevNode) {... } // method specific to TailNode public void enqueue(ElementType newData) {...} // methods redefined from superclass public Node dequeue (Holder result) {...} public void setPrevious(Node newPrevNode) { _prev = newPrevNode; } } October 27, 2005

© 2006 Pearson EducationQueues17 of 32 Internal Nodes Internal nodes need references to data they store and to next node in queue: Like head node, internal nodes redefine setNext method setPrevious does nothing (as in superclass), so it doesn’t need to be redefined public class InternalNode extends Node { private ElementType _data; private Node _next; public InternalNode(ElementType newData, Node newNextNode) {...} // methods redefined from superclass public Node dequeue (Holder result) {...} public void setNext(Node newNextNode) { _next = newNextNode; } } October 27, 2005

© 2006 Pearson EducationQueues18 of 32 Queue Now that we know what nodes look like, we can begin to code our queue: Queue has to keep references to both HeadNode and TailNode so it can tell _tail to enqueue and _ head to dequeue public class Queue { private Node _head; private TailNode _tail; // instantiates HeadNode and TailNode public Queue() { //note asymmetric reference setting! why? _head = new HeadNode (); //set in constructor _tail = new TailNode (_head); //set explicitly _head.setNext(_tail); } public void dequeue() { // code to follow... } public boolean isEmpty() { // code to follow... } public void enqueue(ElementType newData) { // code to follow... } } October 27, 2005

© 2006 Pearson EducationQueues19 of 32 Node Constructors So far, we’ve defined Queue to construct instances of HeadNode and TailNode. InternalNode s will be created during enqueuing as they were during pushes of stack public class HeadNode extends Node { //head doesn’t set its _next here public HeadNode() { super(); } //rest of class... } public class TailNode extends Node { //tail does set its _previous here public TailNode(Node newPrevNode){ super(); _prev = newPrevNode; } //rest of class... } public class InternalNode extends Node { public InternalNode(ElementType newData, Node newNextNode) { super(); _next = newNextNode; _data = newData; } //rest of class... } October 27, 2005

© 2006 Pearson EducationQueues20 of 32 Enqueue Queue delegates enqueue message to tail node, telling it to add new internal node before it: TailNode ’s enqueue method: –creates new internal node, passing it data to store and reference to tail which it will store in its _next –reassigns _next reference of its previous node to point to new node –resets its own _prev reference to new node Let’s see a picture.... public class Queue { public void enqueue(ElementType newData) { _tail.enqueue(newData); }... } public class TailNode extends Node{ public void enqueue(ElementType newData) { InternalNode newPrevNode = new InternalNode (newData, this); _prev.setNext(newPrevNode); _prev = newPrevNode; }... } October 27, 2005

© 2006 Pearson EducationQueues21 of 32 Example: Enqueue (1 of 2) Before enqueue : Tail creates new internal node, sending it references to data and itself; reference to new node stored in newPrevNode HeadNode Node _next TailNode Node _prev InternalNode ElementType _data Node _next Person Sara HeadNode Node _next TailNode Node _prev Person Sara newData to enqueue InternalNode newPrevNode = newPrevNode October 27, 2005 new InternalNode (newData, this);

© 2006 Pearson EducationQueues22 of 32 Example: Enqueue (2 of 2) Tail tells its previous node (head in this case) to reset its _next reference to newPrev Tail resets its own _prev reference HeadNode Node _next TailNode Node _prev InternalNode ElementType _data Node _next Person Sara HeadNode Node _next TailNode Node _prev InternalNode ElementType _data Node _next Person Sara newPrevNode October 27, 2005 _prev.setNext(newPrevNode); _prev = newPrevNode;

© 2006 Pearson EducationQueues23 of 32 Syntax: Dequeue (1 of 3) dequeue removes internal node at head of queue and returns its data Queue ’s dequeue method: –creates Holder (remember from stacks lecture - - why we use one?) –sends dequeue message to head node, passing it reference to Holder –returns data stored in Holder public class Queue { public ElementType dequeue() { Holder result = new Holder (); _head.dequeue(result); return result.getObject(); }... } October 27, 2005

© 2006 Pearson EducationQueues24 of 32 Syntax: Dequeue (2 of 3) HeadNode passes on dequeue message to next node in list, either internal or tail node: Just as with Stack ’s internal nodes, Queue ’s InternalNode s update Holder to reference their _data and return reference to their next node: public class HeadNode extends Node{ // next (internal) node assigns its data to // result and returns its next; tail “no-ops” public Node dequeue( Holder result) { _next = _next.dequeue(result); // tell next to point its previous to me. // Only for tailNode; internal node no-ops _next.setPrevious(this); return this; }... } public class InternalNode extends Node { public Node dequeue( Holder result) { result.setObject(_data); return _next; }... } October 27, 2005

© 2006 Pearson EducationQueues25 of 32 Syntax: Dequeue (3 of 3) TailNode s don’t have _data or _next reference, so they just return reference to themselves: Note similarities to Stack! Now, let’s hand simulate our code... public class TailNode extends Node{ public Node dequeue( Holder result) { return this; }... } October 27, 2005

© 2006 Pearson EducationQueues26 of 32 Example: Dequeue (1 of 3) From class Queue : result = new Holder (); HeadNode Node _next TailNode Node _prev InternalNode ElementType _data Node _next Person Sara InternalNode ElementType _data Node _next Person Mike Holder ElementType _data null result October 27, 2005

© 2006 Pearson EducationQueues27 of 32 Example: Dequeue (2 of 3) From HeadNode : _next = _next.dequeue(result); From InternalNode : result.setObject(_data); From InternalNode: return _next; From HeadNode: _next.setPrevious(this); Let’s see this simulated: HeadNode Node _next TailNode Node _prev InternalNode ElementType _data Node _next Person Sara InternalNode ElementType _data Node _next Person Mike Holder ElementType _data result Internal Node about to be garbage collected October 27, 2005

© 2006 Pearson EducationQueues28 of 32 Example: Dequeue (3 of 3) What happens if we call dequeue on empty queue? Queue will not change –Holder will contain no data after TailNode ’s dequeue method returns itself –HeadNode ’s _next reference will still point to tail because tail’s dequeue method returns reference to itself Cleaner code might be to check if queue is empty before dequeue-ing October 27, 2005

© 2006 Pearson EducationQueues29 of 32 Empty (1 of 2) As with stack, queue does not know if it is isEmpty, so it must delegate: HeadNode does not know either, so must ask _next node: public class Queue { public boolean isEmpty() { return _head.isEmpty(); }... } public class HeadNode extends Node{ public boolean isEmpty() { return _next.isEmpty(); }... } October 27, 2005

© 2006 Pearson EducationQueues30 of 32 Empty (2 of 2) If HeadNode sends isEmpty message to InternalNode, then queue has at least one element: If HeadNode sends isEmpty message to TailNode, then queue must be empty (no InternalNode s): public class InternalNode extends Node { public boolean isEmpty() { return false; }... } public class TailNode extends Node{ public boolean isEmpty() { return true; }... } October 27, 2005

© 2006 Pearson EducationQueues31 of 32 And we’re done! HAPPY QUEUING! JUST QUEUE IT! QUEUE, BABY, QUEUE Q October 27, 2005

© 2006 Pearson EducationQueues32 of 32 October 27, 2005