Queues Cmput 115 - Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.

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

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.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
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.
queues1 Queues Data structures that wait their turn.
CHAPTER 7 Queues.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
Binary Search Trees Cmput Lecture 23 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Sorting - Selection Sort Cmput Lecture 10 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.
Doubly-Linked Lists Cmput Lecture 16 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Ordered Containers Cmput Lecture 21 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Container Traversal Cmput Lecture 20 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Self-Reference - Induction Cmput Lecture 7 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
1 Lecture 25 Abstract Data Types –II Overview  A stack Interface in Java  StackEmptyException  Stack ADT(A Simple Array-Based Implementation  Queue.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
Stacks Cmput Lecture 18 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
Circularly-Linked Lists Cmput Lecture 17 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Self-Reference - Recursion Cmput Lecture 6 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
Object (Data and Algorithm) Analysis Cmput Lecture 5 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this.
Queue, Deque, and Priority Queue Implementations Chapter 24 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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.
Sorting - Insertion Sort Cmput Lecture 11 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.
Quick Sort Cmput Lecture 13 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code.
TCSS 342, Winter 2005 Lecture Notes
Cmput Lecture 15 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book:
The List Interface Cmput Lecture 14 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors.
Dictionaries and Hash Tables Cmput Lecture 24 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
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.
Ordered Containers CMPUT Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2003 Some code in this lecture is based.
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’
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
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.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
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.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
The Queue Data Structure Mugurel Ionu Andreica Spring 2012.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
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.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
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.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
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.
Implementing Queues Eric Roberts CS 106B February 13, 2013.
1 Lecture 15: Big O Notation (Wednesday) Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science Test See Web for Details Don’t be deleted!
Queues Rem Collier Room A1.02
CSE 373: Data Structures and Algorithms
Stacks and Queues.
Trees Tree nomenclature Implementation strategies Traversals
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Copyright © Aiman Hanna All rights reserved
Presentation transcript:

Queues Cmput Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book: Java Structures by Duane A. Bailey or the companion structure package Revised 2/24/00

©Duane Szafron About This Lecture In this lecture we will learn about an implementation of a linear data called a Queue.

©Duane Szafron Outline Queue in the Structure Container Hierarchy Queue Interface Maze - An example of using a Queue List Implementation of Queue Vector Implementation of Queue

©Duane Szafron Linear Container Hierarchy In the structure package the following interface hierarchy appears. Store Linear StackQueue In the java.* packages there is no Queue as an interface or as a class.

©Duane Szafron Structure Interface - Store public interface Store { public int size (); //post: returns the number of elements contained in // the store. public boolean isEmpty (); // post: returns the true iff store is empty. public void clear (); // post: clears the store so that it contains no // elements. } code based on Bailey pg. 18

©Duane Szafron Structure Interface - Linear public interface Linear extends Store { public void add (Object anObject); // pre: anObject is non-null // post: the object is added to the container. The // consistent replacement policy is not specified public Object peek (); // pre: container is not empty // post: returns the next object to be removed, but does in // not remove it public Object remove (); // pre: container is not empty // post: removes an object from the container and returns it } code based on Bailey pg. 127

©Duane Szafron Structure Interface - Queue 1 public interface Queue extends Linear { public void add (Object anObject); // post: the object is added at the tail. It will be // removed after all elements in front of it. public void enqueue (Object anObject); // post: the object is added at the tail. It will be // removed after all elements in front of it. public Object remove (); // pre: queue is not empty // post: the head of the queue is removed and returned code based on Bailey pg. 137

©Duane Szafron Structure Interface - Queue 2 public Object dequeue (); // pre: queue is not empty // post: the head of the queue is removed and returned public Object peek (); // pre: queue is not empty // post: returns the element at the head of the queue, // but does not remove it } code based on Bailey pg. 137

©Duane Szafron Queue Example - Maze Algorithm Like Stacks, Queues can also be used to store unsearched paths. 0 S F Repeat as long as the current square is not null and is not the finish square: –“Visit” the square. –Enqueue one square on the queue for each unvisited legal move from the current square. –Dequeue the queue into the current square or bind the current square to null if the stack is empty. If the current square is the goal we are successful, otherwise there is no solution

©Duane Szafron Queue Example - Searching The algorithm seems the same so what is the difference between using a Stack and a Queue? When a Stack is used, the search goes as deep along a single path as possible, before trying another path. When a Queue is used, the search expands a frontier of nodes equidistant from the start. 0 S F S F Stack Search Queue Search

©Duane Szafron Queue Example - Searching 0 S F S F Stack Search Queue Search

©Duane Szafron Queue Example - Maze Trace 1 P0 P1 0 S F P1 P3 P2 0 S F P2 P5 P4 0 S F P3 P6 P5 0 S F P4 P7 P6 0 S F P5 P8 0 S F P7 P6

©Duane Szafron Queue Example - Maze Trace 2 P5 P8 P7 0 S F P6 P9 P8 P7 0 S F P7 P9 P8 0 S F P8 P10 0 S F P9 P11 0 S F P10 Success! P10 P11 0 S F

©Duane Szafron Maze Program Same program as in the Stack lecture, except replace the line: todo = new StackList (); // A class that implements Stack by: todo = new QueueList (); // A class that implements Queue

©Duane Szafron List-Based Queues We can implement a Queue using a DoublyLinkedList or a CircularLinkedList. We do not use a SinglyLinkedList since operations on the tail take time proportional to the length of the list. As the Queue grows and shrinks, so does the list. The time for adding or removing a single element is always the same constant time. There are two extra link spaces required for each element. The implementation is simple, few mistakes are made.

©Duane Szafron QueueList - State and Constructors public class QueueList implements Queue { /* An implementation of the Queue Interface that uses a DoublyLinkedList to store the Stack elements. */ // Instance Variables // protected List data; public QueueList () { // post: initialize the Stack to be empty this.data = new DoublyLinkedList(); //or CircularLinkedList } code based on Bailey pg. 140

©Duane Szafron QueueList - Linear Interface 1 public void add (Object anObject) { // post: the object is added at the tail. It will be // removed after all elements in front of it. this.data.addToTail(anObject); } public Object remove () { // pre: queue is not empty // post: the head of the queue is removed and returned return this.data.removeFromHead(); } code based on Bailey pg. 141

©Duane Szafron QueueList - Linear Interface 2 public Object peek () { // pre: queue is not empty // post: returns the element at the head of the queue, // but does not remove it Assert.pre(!this.isEmpty(), “Queue is not empty”); return this.data.peek(); } code based on Bailey pg. 142

©Duane Szafron QueueList - Queue Interface public void enqueue (Object anObject) { // post: the object is added at the tail. It will be // removed after all elements in front of it. this.add(anObject); } public Object dequeue () { // pre: queue is not empty // post: the head of the queue is removed and returned return this.remove(); } code based on Bailey pg. 141

©Duane Szafron QueueList - Store Interface public int size(); //post: returns the number of elements contained in the store. return this.data.size(); } public boolean isEmpty(); // post: returns the true iff store is empty. return this.size() == 0; } public void clear(); // post: clears the store so that it contains no elements. this.data.clear(); } code based on Bailey pg. 142

©Duane Szafron Vector-Based Queues We can implement a Queue using a single Vector. However, this implementation is not time efficient. Adding an element to the Queue results in adding an element to the end of the Vector which takes constant time (except when the Vector must grow). However, removing an element from the Queue results in removing the zeroth element of the Vector which takes time proportional to the length of the Vector (since all elements after the zeroth must be moved to the left). Since this implementation has such poor performance, we will not use it.

©Duane Szafron Array-Based Queues We can implement a Queue using a single Array if we know in advance the maximum size of the Queue. The naïve implementation of storing the head at index 0 and growing the queue towards the end of the Array is too inefficient, for the same reason that the Vector approach was inefficient. Instead, we maintain two indexes, the head and tail index and let them “slide” towards the end of the Array and then wrap around. "Fred""Barney""Wilma" "Fred""Barney""Wilma" headtail

©Duane Szafron Sliding Indexes "Fred""Barney""Wilma" headtail add "Fred""Barney""Wilma" headtail "Betty" remove add "Pebbles""Barney""Wilma" headtail "Betty" headtail "Barney""Wilma""Betty"

©Duane Szafron Sliding Indexes 2 remove add remove "Pebbles""Barney""Wilma" headtail "Betty" "Pebbles""Wilma" headtail "Betty" "Pebbles""Wilma" headtail "Betty""Slate" "Pebbles" headtail "Betty""Slate"

©Duane Szafron Array-Based Queues - Empty and Full We leave one empty entry in the Queue. The condition for an empty Queue is: head == tail. The condition for a full Queue is: tail is one “behind” head.

©Duane Szafron Some Principles from the Textbook 13. Understand the complexity of the Structure you use. principles from Bailey ch. 7