© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 5: Queues Java Software Structures: Designing and Using Data.

Slides:



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

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 11: Priority Queues and Heaps Java Software Structures: Designing.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
Queues Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Chapter 14 Queues. Chapter Scope Queue processing Using queues to solve problems Various queue implementations Comparing queue implementations Java Foundations,
1 Queues Queue Concept Queue Design Considerations Queues in Java Collections APIs Queue Applications Reading L&C , 9.3.
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.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
TCSS 342, Winter 2005 Lecture Notes
Chapter 8 Lists. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 Chapter Objectives Examine list processing and various ordering techniques.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
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
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 13: Graphs Java Software Structures: Designing and Using Data.
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
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 3: Collections Java Software Structures: Designing and Using.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
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 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Linked Structures - Review Chapter 13 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
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.
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.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
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.
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)
Collections & Stacks Collections A collection is an object that holds and organizes other objects It provides operations for accessing and managing.
The Queue ADT.
CHAPTER 4: Linked Structures
Using Queues: Coded Messages
Queues Implementations 6/17/2018.
COS 312 DAY 21 Tony Gauvin.
Queues Queue Concept Queue Design Considerations
Queue.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Chapter 5 Queues.
Queue, Deque, and Priority Queue Implementations
Java Software Structures: John Lewis & Joseph Chase
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Java Software Structures: John Lewis & Joseph Chase
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
CHAPTER 11: Priority Queues and Heaps
Copyright © Aiman Hanna All rights reserved
CHAPTER 3: Collections—Stacks
Presentation transcript:

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 5: Queues Java Software Structures: Designing and Using Data Structures Third Edition John Lewis & Joseph Chase

1-2 © 2010 Pearson Addison-Wesley. All rights reserved. 1-2 Chapter Objectives Examine queue processing Define a queue abstract data type Demonstrate how a queue can be used to solve problems Examine various queue implementations Compare queue implementations

1-3 © 2010 Pearson Addison-Wesley. All rights reserved. 1-3 Queues A queue is a collection whose elements are added on one end and removed from the other Therefore a queue is processed in a FIFO fashion: first in, first out Elements are removed in the same order they arrive Any waiting line is a queue: –the check out line at a grocery store –the cars at a stop light –an assembly line

1-4 © 2010 Pearson Addison-Wesley. All rights reserved. 1-4 Queues A queue is usually depicted horizontally One end of the queue is the rear (or tail), where elements are added The other end is the front (or head), from which elements are removed Unlike a stack, which operates on one end of the collection, a queue operates on both ends

1-5 © 2010 Pearson Addison-Wesley. All rights reserved. 1-5 A conceptual view of a queue

1-6 © 2010 Pearson Addison-Wesley. All rights reserved. 1-6 Queue Operations The term enqueue is used to refer to the process of adding an element to a queue Likewise, dequeue is the process of removing an element Like a stack, a pure queue does not allow the user to access the elements in the middle of the queue We include a toString method for convenience

1-7 © 2010 Pearson Addison-Wesley. All rights reserved. 1-7 The operations on a queue

1-8 © 2010 Pearson Addison-Wesley. All rights reserved. 1-8 The QueueADT interface in UML

1-9 © 2010 Pearson Addison-Wesley. All rights reserved. 1-9 The QueueADT interface /** * QueueADT defines the interface to a queue collection. * Dr. Lewis Dr. Chase 1.0, 8/12/08 */ package jss2; public interface QueueADT { /** * Adds one element to the rear of this queue. * element the element to be added to the rear of this queue */ public void enqueue (T element); /** * QueueADT defines the interface to a queue collection. * Dr. Lewis Dr. Chase 1.0, 8/12/08 */ package jss2; public interface QueueADT { /** * Adds one element to the rear of this queue. * element the element to be added to the rear of this queue */ public void enqueue (T element);

1-10 © 2010 Pearson Addison-Wesley. All rights reserved The QueueADT interface (continued) /** * Removes and returns the element at the front of this queue. * the element at the front of this queue */ public T dequeue(); /** * Returns without removing the element at the front of this queue. * the first element in this queue */ public T first(); /** * Returns true if this queue contains no elements. * true if this queue is empty */ public boolean isEmpty(); /** * Removes and returns the element at the front of this queue. * the element at the front of this queue */ public T dequeue(); /** * Returns without removing the element at the front of this queue. * the first element in this queue */ public T first(); /** * Returns true if this queue contains no elements. * true if this queue is empty */ public boolean isEmpty();

1-11 © 2010 Pearson Addison-Wesley. All rights reserved The QueueADT interface (continued) /** * Returns the number of elements in this queue. * the integer representation of the size of this queue */ public int size(); /** * Returns a string representation of this queue. * the string representation of this queue */ public String toString(); } /** * Returns the number of elements in this queue. * the integer representation of the size of this queue */ public int size(); /** * Returns a string representation of this queue. * the string representation of this queue */ public String toString(); }

1-12 © 2010 Pearson Addison-Wesley. All rights reserved Coded Messages Let's use a queue to help us encode and decode messages A Ceasar cipher encodes a message by shifting each letter in a message by a constant amount k If k is 5, A becomes F, B becomes G, etc. However, this is fairly easy to break An improvement can be made by changing how much a letter is shifted depending on where the letter is in the message

1-13 © 2010 Pearson Addison-Wesley. All rights reserved Coded Messages A repeating key is a series of integers that determine how much each character is shifted For example, consider the repeating key The first character in the message is shifted 3, the next 1, the next 7, and so on When the key is exhausted, we just start over at the beginning of the key

1-14 © 2010 Pearson Addison-Wesley. All rights reserved An encoded message using a repeating key

1-15 © 2010 Pearson Addison-Wesley. All rights reserved Coded Messages We'll use a queue to store the values of the key We'll dequeue a value when needed After using a key value, we then enqueue it back onto the end of the queue That way the queue represents the constantly cycling values in the key

1-16 © 2010 Pearson Addison-Wesley. All rights reserved The Codes class /** * Codes demonstrates the use of queues to encrypt and decrypt messages. * Dr. Lewis Dr. Chase 1.0, 08/12/08 */ import jss2.CircularArrayQueue; public class Codes { /** * Encode and decode a message using a key of values stored in * a queue. */ public static void main ( String[] args) { int[] key = {5, 12, -3, 8, -9, 4, 10}; Integer keyValue; String encoded = "", decoded = ""; String message = "All programmers are playwrights and all " + "computers are lousy actors."; /** * Codes demonstrates the use of queues to encrypt and decrypt messages. * Dr. Lewis Dr. Chase 1.0, 08/12/08 */ import jss2.CircularArrayQueue; public class Codes { /** * Encode and decode a message using a key of values stored in * a queue. */ public static void main ( String[] args) { int[] key = {5, 12, -3, 8, -9, 4, 10}; Integer keyValue; String encoded = "", decoded = ""; String message = "All programmers are playwrights and all " + "computers are lousy actors.";

1-17 © 2010 Pearson Addison-Wesley. All rights reserved The Codes class (continued) CircularArrayQueue keyQueue1 = new CircularArrayQueue (); CircularArrayQueue keyQueue2 = new CircularArrayQueue (); /** load key queue */ for (int scan=0; scan < key.length; scan++) { keyQueue1.enqueue (new Integer(key[scan])); keyQueue2.enqueue (new Integer(key[scan])); } /** encode message */ for (int scan=0; scan < message.length(); scan++) { keyValue = keyQueue1.dequeue(); encoded += (char) ((int)message.charAt(scan) + keyValue.intValue()); keyQueue1.enqueue (keyValue); } CircularArrayQueue keyQueue1 = new CircularArrayQueue (); CircularArrayQueue keyQueue2 = new CircularArrayQueue (); /** load key queue */ for (int scan=0; scan < key.length; scan++) { keyQueue1.enqueue (new Integer(key[scan])); keyQueue2.enqueue (new Integer(key[scan])); } /** encode message */ for (int scan=0; scan < message.length(); scan++) { keyValue = keyQueue1.dequeue(); encoded += (char) ((int)message.charAt(scan) + keyValue.intValue()); keyQueue1.enqueue (keyValue); }

1-18 © 2010 Pearson Addison-Wesley. All rights reserved The Codes class (continued) System.out.println ("Encoded Message:\n" + encoded + "\n"); /** decode message */ for (int scan=0; scan < encoded.length(); scan++) { keyValue = keyQueue2.dequeue(); decoded += (char) ((int)encoded.charAt(scan) - keyValue.intValue()); keyQueue2.enqueue (keyValue); } System.out.println ("Decoded Message:\n" + decoded); } System.out.println ("Encoded Message:\n" + encoded + "\n"); /** decode message */ for (int scan=0; scan < encoded.length(); scan++) { keyValue = keyQueue2.dequeue(); decoded += (char) ((int)encoded.charAt(scan) - keyValue.intValue()); keyQueue2.enqueue (keyValue); } System.out.println ("Decoded Message:\n" + decoded); }

1-19 © 2010 Pearson Addison-Wesley. All rights reserved UML description of the Codes program

1-20 © 2010 Pearson Addison-Wesley. All rights reserved Ticket Counter Simulation Now let's use a queue to simulate the waiting line at a movie theatre The goal is to determine how many cashiers are needed to keep the wait time below 7 minutes We'll assume: –customers arrive on average every 15 seconds –processing a request takes two minutes once a customer reaches a cashier

1-21 © 2010 Pearson Addison-Wesley. All rights reserved The Customer class /** * Customer represents a waiting customer. * Dr. Lewis Dr. Chase 1.0, 08/12/08 */ public class Customer { private int arrivalTime, departureTime; /** * Creates a new customer with the specified arrival time. * arrives the integer representation of the arrival time */ public Customer (int arrives) { arrivalTime = arrives; departureTime = 0; } /** * Customer represents a waiting customer. * Dr. Lewis Dr. Chase 1.0, 08/12/08 */ public class Customer { private int arrivalTime, departureTime; /** * Creates a new customer with the specified arrival time. * arrives the integer representation of the arrival time */ public Customer (int arrives) { arrivalTime = arrives; departureTime = 0; }

1-22 © 2010 Pearson Addison-Wesley. All rights reserved The Customer class (continued) /** * Returns the arrival time of this customer. * the integer representation of the arrival time */ public int getArrivalTime() { return arrivalTime; } /** * Sets the departure time for this customer. * departs the integer representation of the departure time **/ public void setDepartureTime (int departs) { departureTime = departs; } /** * Returns the arrival time of this customer. * the integer representation of the arrival time */ public int getArrivalTime() { return arrivalTime; } /** * Sets the departure time for this customer. * departs the integer representation of the departure time **/ public void setDepartureTime (int departs) { departureTime = departs; }

1-23 © 2010 Pearson Addison-Wesley. All rights reserved The Customer class (continued) /** * Returns the departure time of this customer. * the integer representation of the departure time */ public int getDepartureTime() { return departureTime; } /** * Computes and returns the total time spent by this customer. * the integer representation of the total customer time */ public int totalTime() { return departureTime – arrivalTime; } /** * Returns the departure time of this customer. * the integer representation of the departure time */ public int getDepartureTime() { return departureTime; } /** * Computes and returns the total time spent by this customer. * the integer representation of the total customer time */ public int totalTime() { return departureTime – arrivalTime; }

1-24 © 2010 Pearson Addison-Wesley. All rights reserved The TicketCounter class /** * TicketCounter demonstrates the use of a queue for simulating a waiting line. * Dr. Lewis Dr. Chase 1.0, 08/12/08 */ import jss2.*; public class TicketCounter { final static int PROCESS = 120; final static int MAX_CASHIERS = 10; final static int NUM_CUSTOMERS = 100; public static void main ( String[] args) { Customer customer; LinkedQueue customerQueue = new LinkedQueue (); int[] cashierTime = new int[MAX_CASHIERS]; int totalTime, averageTime, departs; /** * TicketCounter demonstrates the use of a queue for simulating a waiting line. * Dr. Lewis Dr. Chase 1.0, 08/12/08 */ import jss2.*; public class TicketCounter { final static int PROCESS = 120; final static int MAX_CASHIERS = 10; final static int NUM_CUSTOMERS = 100; public static void main ( String[] args) { Customer customer; LinkedQueue customerQueue = new LinkedQueue (); int[] cashierTime = new int[MAX_CASHIERS]; int totalTime, averageTime, departs;

1-25 © 2010 Pearson Addison-Wesley. All rights reserved The TicketCounter class (continued) /** process the simulation for various number of cashiers */ for (int cashiers=0; cashiers < MAX_CASHIERS; cashiers++) { /** set each cashiers time to zero initially*/ for (int count=0; count < cashiers; count++) cashierTime[count] = 0; /** load customer queue */ for (int count=1; count <= NUM_CUSTOMERS; count++) customerQueue.enqueue(new Customer(count*15)); totalTime = 0; /** process all customers in the queue */ while (!(customerQueue.isEmpty())) { for (int count=0; count <= cashiers; count++) { if (!(customerQueue.isEmpty())) /** process the simulation for various number of cashiers */ for (int cashiers=0; cashiers < MAX_CASHIERS; cashiers++) { /** set each cashiers time to zero initially*/ for (int count=0; count < cashiers; count++) cashierTime[count] = 0; /** load customer queue */ for (int count=1; count <= NUM_CUSTOMERS; count++) customerQueue.enqueue(new Customer(count*15)); totalTime = 0; /** process all customers in the queue */ while (!(customerQueue.isEmpty())) { for (int count=0; count <= cashiers; count++) { if (!(customerQueue.isEmpty()))

1-26 © 2010 Pearson Addison-Wesley. All rights reserved The TicketCounter class (continued) { customer = customerQueue.dequeue(); if (customer.getArrivalTime() > cashierTime[count]) departs = customer.getArrivalTime() + PROCESS; else departs = cashierTime[count] + PROCESS; customer.setDepartureTime (departs); cashierTime[count] = departs; totalTime += customer.totalTime(); } /** output results for this simulation */ averageTime = totalTime / NUM_CUSTOMERS; System.out.println ("Number of cashiers: " + (cashiers+1)); System.out.println ("Average time: " + averageTime + "\n"); } { customer = customerQueue.dequeue(); if (customer.getArrivalTime() > cashierTime[count]) departs = customer.getArrivalTime() + PROCESS; else departs = cashierTime[count] + PROCESS; customer.setDepartureTime (departs); cashierTime[count] = departs; totalTime += customer.totalTime(); } /** output results for this simulation */ averageTime = totalTime / NUM_CUSTOMERS; System.out.println ("Number of cashiers: " + (cashiers+1)); System.out.println ("Average time: " + averageTime + "\n"); }

1-27 © 2010 Pearson Addison-Wesley. All rights reserved UML description of the TicketCounter program

1-28 © 2010 Pearson Addison-Wesley. All rights reserved The results of the ticket counter simulation

1-29 © 2010 Pearson Addison-Wesley. All rights reserved The LinkedQueue Class Like a stack, a queue can be implemented using an underlying array or a linked list A linked version can use the LinearNode class yet again In addition to keeping a reference to the beginning of the list, we will keep a second reference to the end An integer count will keep track of the number of elements in the queue

1-30 © 2010 Pearson Addison-Wesley. All rights reserved A linked implementation of a queue

1-31 © 2010 Pearson Addison-Wesley. All rights reserved The LinkedQueue class /** * LinkedQueue represents a linked implementation of a queue. * Dr. Lewis Dr. Chase 1.0, 08/12/08 */ package jss2; import jss2.exceptions.*; public class LinkedQueue implements QueueADT { private int count; private LinearNode front, rear; /** * Creates an empty queue. */ public LinkedQueue() { count = 0; front = rear = null; } /** * LinkedQueue represents a linked implementation of a queue. * Dr. Lewis Dr. Chase 1.0, 08/12/08 */ package jss2; import jss2.exceptions.*; public class LinkedQueue implements QueueADT { private int count; private LinearNode front, rear; /** * Creates an empty queue. */ public LinkedQueue() { count = 0; front = rear = null; }

1-32 © 2010 Pearson Addison-Wesley. All rights reserved LinkedQueue - the enqueue Operation /** * Adds the specified element to the rear of this queue. * element the element to be added to the rear of this queue */ public void enqueue (T element) { LinearNode node = new LinearNode (element); if (isEmpty()) front = node; else rear.setNext (node); rear = node; count++; } /** * Adds the specified element to the rear of this queue. * element the element to be added to the rear of this queue */ public void enqueue (T element) { LinearNode node = new LinearNode (element); if (isEmpty()) front = node; else rear.setNext (node); rear = node; count++; }

1-33 © 2010 Pearson Addison-Wesley. All rights reserved The queue after adding element E

1-34 © 2010 Pearson Addison-Wesley. All rights reserved LinkedQueue - the dequeue Operation /** * Removes the element at the front of this queue and returns a * reference to it. Throws an EmptyCollectionException if the * queue is empty. * the element at the front of this queue EmptyCollectionException if an empty collection exception occurs */ public T dequeue() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException ("queue"); T result = front.getElement(); front = front.getNext(); count--; if (isEmpty()) rear = null; return result; } /** * Removes the element at the front of this queue and returns a * reference to it. Throws an EmptyCollectionException if the * queue is empty. * the element at the front of this queue EmptyCollectionException if an empty collection exception occurs */ public T dequeue() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException ("queue"); T result = front.getElement(); front = front.getNext(); count--; if (isEmpty()) rear = null; return result; }

1-35 © 2010 Pearson Addison-Wesley. All rights reserved The queue after a dequeue operation

1-36 © 2010 Pearson Addison-Wesley. All rights reserved LinkedQueue – other operations The remaining operations in the linked queue implementation are fairly straightforward and are similar to those in the stack collection The first operation is implemented by returning a reference to the element at the front of the queue The isEmpty operation returns true if the count of elements is 0, and false otherwise The size operation simply returns the count of elements in the queue The toString operation returns a string made up of the toString results of each individual element

1-37 © 2010 Pearson Addison-Wesley. All rights reserved Implementing Queues with arrays One strategy for implementing a queue would be to fix one end of the queue at position 0 of an array like we did with the ArrayStack Queues, operate on both ends which would force the shifting of elements either on enqueue or dequeue A better approach is to use a conceptual circular array and not fix either end of the queue

1-38 © 2010 Pearson Addison-Wesley. All rights reserved The CircularArrayQueue Class If we do not fix one end of the queue at index 0, we will not have to shift the elements A circular queue is an implementation of a queue using an array that conceptually loops around on itself That is, the last index is thought to precede index 0 We keep track of integers that indicate where the front and rear of the queue are at any given time

1-39 © 2010 Pearson Addison-Wesley. All rights reserved A circular array implementation of a queue

1-40 © 2010 Pearson Addison-Wesley. All rights reserved A queue straddling the end of a circular array

1-41 © 2010 Pearson Addison-Wesley. All rights reserved Changes in a circular array implementation of a queue

1-42 © 2010 Pearson Addison-Wesley. All rights reserved Circular Queues When an element is enqueued, the value of rear is incremented But it must take into account the need to loop back to 0: rear = (rear+1) % queue.length; Note that this array implementation can also reach capacity and may need enlarging

1-43 © 2010 Pearson Addison-Wesley. All rights reserved The CircularArrayQueue class /** * 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. * Dr. Lewis Dr. Chase /12/08 */ package jss2; import jss2.exceptions.*; import java.util.Iterator; public class CircularArrayQueue implements QueueADT { private final int DEFAULT_CAPACITY = 100; private int front, rear, count; private T[] queue; /** * 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. * Dr. Lewis Dr. Chase /12/08 */ package jss2; import jss2.exceptions.*; import java.util.Iterator; public class CircularArrayQueue implements QueueADT { private final int DEFAULT_CAPACITY = 100; private int front, rear, count; private T[] queue;

1-44 © 2010 Pearson Addison-Wesley. All rights reserved The CircularArrayQueue class /** * Creates an empty queue using the default capacity. */ public CircularArrayQueue() { front = rear = count = 0; queue = (T[]) (new Object[DEFAULT_CAPACITY]); } /** * Creates an empty queue using the specified capacity. * initialCapacity the integer representation of 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() { front = rear = count = 0; queue = (T[]) (new Object[DEFAULT_CAPACITY]); } /** * Creates an empty queue using the specified capacity. * initialCapacity the integer representation of the initial * size of the circular array queue */ public CircularArrayQueue (int initialCapacity) { front = rear = count = 0; queue = ( (T[])(new Object[initialCapacity]) ); }

1-45 © 2010 Pearson Addison-Wesley. All rights reserved CircularArrayQueue – the enqueue operation /** * 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++; } /** * 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++; }

1-46 © 2010 Pearson Addison-Wesley. All rights reserved CircularArrayQueue – the dequeue operation /** * Removes the element at the front of this queue and returns a * reference to it. Throws an EmptyCollectionException if the * queue is empty. * the reference to the element at the front * of the queue that was removed EmptyCollectionException if an empty collections exception occurs */ 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; } /** * Removes the element at the front of this queue and returns a * reference to it. Throws an EmptyCollectionException if the * queue is empty. * the reference to the element at the front * of the queue that was removed EmptyCollectionException if an empty collections exception occurs */ 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; }

1-47 © 2010 Pearson Addison-Wesley. All rights reserved CircularArrayQueue – the expandCapacity operation /** * Creates a new array to store the contents of this queue with * twice the capacity of the old one. */ public 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; } /** * Creates a new array to store the contents of this queue with * twice the capacity of the old one. */ public 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; }

1-48 © 2010 Pearson Addison-Wesley. All rights reserved Queue Implementations The enqueue operation is O(1) for both implementations The dequeue operation is O(1) for linked and circular array implementations, but O(n) for the noncircular array version due to the need to shift the elements in the queue