Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.

Slides:



Advertisements
Similar presentations
Chapter 24 Lists, Stacks, and Queues
Advertisements

Chapter 15 Lists. Chapter Scope Types of list collections Using lists to solve problems Various list implementations Comparing list implementations Java.
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.
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.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
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.
1 Queues (Continued) Applications for a queue Linked queue implementation Array queue implementation Circular array queue implementation Reading L&C 3.
queues1 Queues Data structures that wait their turn.
Topic 9 The Queue ADT.
CHAPTER 7 Queues.
5/4/20151 Queues Implementations. 5/4/20152 Outline Queues Basic operations Examples of useImplementations Array-based and linked list-based.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
DATA STRUCTURE & ALGORITHMS
Chapter 14 Queues. Chapter Scope Queue processing Using queues to solve problems Various queue implementations Comparing queue implementations Java Foundations,
Queues.
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.
Chapter 13 Linked Structures - Stacks. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked implementation.
Stacks, Queues, and Deques
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
Chapter 12 Introduction to Collections - Stacks
Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract.
Topic 3 The Stack ADT.
Chapter 3 Introduction to Collections – Stacks Modified
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
COS 312 DAY 20 Tony Gauvin. Ch 1 -2 Agenda Questions? Capstone Progress reports over due Assignment 6 Posted – Due April 16 – Questions? Queues.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Stacks And Queues Chapter 18.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 5: Queues Java Software Structures: Designing and Using Data.
Linked Structures - Review Chapter 13 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
CS 367 Introduction to Data Structures Lecture 5.
Collections Chapter 12 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
COS 312 DAY 18 Tony Gauvin. Ch 1 -2 Agenda Questions? Capstone Progress reports over due Assignment 5 Corrected – 1 A, 2 B’s, 1 C, 1 D 1 F and 1 MIA –
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Linked Structures Chapter 13 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Chapter 5 The Queue ADT. 5.1 Queues Queue A structure in which elements are added to the rear and removed from the front; a “first in, first out” (FIFO)
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
The Queue ADT.
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Queues Rem Collier Room A1.02
Queues Implementations 6/17/2018.
COS 312 DAY 21 Tony Gauvin.
Chapter 21 Heaps and Priority Queues
Chapter 5 Queues.
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queue, Deque, and Priority Queue Implementations
Java Software Structures: John Lewis & Joseph Chase
Queue, Deque, and Priority Queue Implementations
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Stacks and Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
More Data Structures (Part 1)
Stacks, Queues, and Deques
Stacks and Queues.
Chapter 21 Heaps and Priority Queues
CHAPTER 3: Collections—Stacks
Presentation transcript:

Chapter 14 Queues

First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome method Various queue implementations – Linked list based – Array based Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 2

Queues Standard queue operations: Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 3

Pre Lab 3 -Q#1 Assume that ticketLine is a newly created Queue. What is the state of the Queue after the following series of statements have been executed? ticketLine.enqueue("Smith"); ticketLine.enqueue("Hu"); String name = ticketLine.dequeue(); ticketLine.enqueue("Mudahar"); ticketLine.enqueue("Bailey"); ticketLine.enqueue("Field"); ticketLine.enqueue("Davis"); ticketLine.enqueue("Matthison"); ticketLine.dequeue(); ticketLine.enqueue("Able"); Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 4

Queues in the Java Collections API Java Foundations, 3rd Edition, Lewis/DePasquale/Chase Throws exceptionReturns special value Insertadd(e)offer(e)offer(e) (true/false) Removeremove()poll()poll() (null) Examineelement()peek() The Java Collections API is not consistent about its implementation of collections – For queues, the API provides a Queue interface, then various classes such as LinkedList implement the interface

Implementing a Queue with Links Since operations work on both ends of the queue, we'll use both front and rear references Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 6

package jsjf; import jsjf.exceptions.*; /** * LinkedQueue represents a linked implementation of a queue. * Java Foundations 4.0 */ public class LinkedQueue implements QueueADT { private int count; private LinearNode head, tail; /** * Creates an empty queue. */ public LinkedQueue() { count = 0; head = tail = null; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 7

/** * Adds the specified element to the tail of this queue. element the element to be added to the tail of the queue */ public void enqueue(T element) { LinearNode node = new LinearNode (element); if (isEmpty()) head = node; else tail.setNext(node); tail = node; count++; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase14 - 8

/** * Removes the element at the head of this queue and returns a reference to it. the element at the head of this queue EmptyCollectionException if the empty */ public T dequeue() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("queue"); T result = head.getElement(); head = head.getNext(); count--; if (isEmpty()) tail = null; return result; } // Other methods public T first() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("queue"); return head.getElement(); } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase // methods to be implmented public boolean isEmpty() {…} public int size() { …} public String toString() {…}

Implementing a Queue with an Array If we implement a queue as we did a stack, one end would be fixed at index 0: The problem is that (unlike a stack) a queue operates at both ends To be efficient, we must avoid shifting elements Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Implementing a Queue with an Array A better solution is to treat the array as circular A circular array is a a regular array that is treated as if it loops back around on itself That is, the last index is thought to precede index 0 We use two integers to keep track of where the front and rear of the queue are at any given time Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Implementing a Queue with an Array A queue implemented using a circular queue: Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Implementing a Queue with an Array At some point, the elements of the queue may straddle the end of the array: Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

After A-H have been enqueued: After A-D have been dequeueed: After I, J, K, and L have been enqueued: Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Implementing a Queue with an Array Both the front and rear index values are incremented, wrapping back to 0 when necessary Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

package jsjf; import jsjf.exceptions.*; /** * CircularArrayQueue represents an array implementation of a queue in * which the indexes for the front and rear of the queue circle back to 0 * when they reach the end of the array. * Java Foundations 4.0 */ public class CircularArrayQueue implements QueueADT { private final static int DEFAULT_CAPACITY = 100; private int front, rear, count; private T[] queue; Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Creates an empty queue using the specified capacity. initialCapacity the initial size of the circular array queue */ public CircularArrayQueue (int initialCapacity) { front = rear = count = 0; queue = (T[]) (new Object[initialCapacity]); } /** * Creates an empty queue using the default capacity. */ public CircularArrayQueue() { this(DEFAULT_CAPACITY); } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Adds the specified element to the rear of this queue, expanding * the capacity of the queue array if necessary. element the element to add to the rear of the queue */ public void enqueue(T element) { if (size() == queue.length) expandCapacity(); queue[rear] = element; rear = (rear+1) % queue.length; count++; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Creates a new array to store the contents of this queue with * twice the capacity of the old one. */ private void expandCapacity() { T[] larger = (T[]) (new Object[queue.length *2]); for (int scan = 0; scan < count; scan++) { larger[scan] = queue[front]; front = (front + 1) % queue.length; } front = 0; rear = count; queue = larger; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Removes the element at the front of this queue and returns a * reference to it. the element removed from the front of the queue EmptyCollectionException if the queue is empty */ public T dequeue() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("queue"); T result = queue[front]; queue[front] = null; front = (front+1) % queue.length; count--; return result; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Pre-Lab 3 #2 Explain how a queue can be implemented using an array, where the enqueue and the dequeue operations are both constant time operations (for simplicity, assume that the capacity of the array is not expanded when the queue becomes full.) Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Pre-Lab 3 #3 Suppose there was no count variable stored for the linked list based implementation of a queue. Explain why it would not be possible to implement a constant time size() operation? Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Pre-Lab 3 #4 Suppose there was no count variable stored for the array based implementation of a queue. Explain why it would still be possible to implement a constant time size() operation? Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Pre-Lab 3 #5 & 6 5. Given a linked list based implementation of a Queue, in what case will the following code for the enqueue method fail? Assume that rear is a reference to the end of the queue. public void enqueue(T element) { LinearNode newNode = new LinearNode(element); rear.setNext(newNode); rear = newNode; count++; } 6. How can the enqueue method from the previous problem be corrected? Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Pre-Lab 3 #7 Correct the bug in the enqueue method shown below which is for a queue implemented as a circular array. public void enqueue(T element) { if(count == queue.length) expandCapacity(); queue[rear] = element; rear = rear + 1; count++; } Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Comparing queue implementations Array Based & Linked List Based – Space comparison – Time comparison Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Dequeue til/Deque.html til/Deque.html Linked List based implementation – Doubly-linked lists Java Foundations, 3rd Edition, Lewis/DePasquale/Chase