+ All Categories
Home > Documents > Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing...

Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing...

Date post: 23-Dec-2015
Category:
Upload: tobias-holland
View: 223 times
Download: 0 times
Share this document with a friend
27
Chapter 14 Queues
Transcript
Page 1: Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.

Chapter 14

Queues

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

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

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

14 - 2

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

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Queues• Standard queue operations:

14 - 3

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

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

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.dequeue();• ticketLine.dequeue();• ticketLine.enqueue("Able");

14 - 4

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

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Queues in the Java Collections API

14 - 5

Throws exception Returns special value

Insert add(e) offer(e) (true/false)

Remove remove() poll() (null)

Examine element() peek()

• The Java Collections API is not consistent about its implementation of collections– http://docs.oracle.com/javase/8/docs/api/index.html

• For queues, the API provides a Queue interface, then various classes such as LinkedList implement the interface

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

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Implementing a Queue with Links• Since operations work on both ends of the

queue, we'll use both front and rear references

14 - 6

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

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

package jsjf;

import jsjf.exceptions.*;

/** * LinkedQueue represents a linked implementation of a queue. * * @author Java Foundations * @version 4.0 */public class LinkedQueue<T> implements QueueADT<T>{ private int count; private LinearNode<T> head, tail;

/** * Creates an empty queue. */ public LinkedQueue() { count = 0; head = tail = null; }

14 - 7

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

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Adds the specified element to the tail of this queue. * @param element the element to be added to the tail of the queue */ public void enqueue(T element) { LinearNode<T> node = new LinearNode<T>(element);

if (isEmpty()) head = node; else tail.setNext(node);

tail = node; count++; }

14 - 8

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

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Removes the element at the head of this queue and

returns a reference to it. * @return the element at the head of this queue * @throws 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(); }

14 - 9

// methods to be implmentedpublic boolean isEmpty() {…}public int size() { …}public String toString() {…}

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

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

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 elements14 - 10

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

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

14 - 11

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

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Implementing a Queue with an Array• A queue implemented using a circular queue:

14 - 12

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

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:

14 - 13

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

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:

14 - 14

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

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

14 - 15

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

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. * * @author Java Foundations * @version 4.0 */public class CircularArrayQueue<T> implements QueueADT<T>{ private final static int DEFAULT_CAPACITY = 100; private int front, rear, count; private T[] queue;

14 - 16

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

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Creates an empty queue using the specified capacity. * @param 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); }

14 - 17

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

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. * @param 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++; }

14 - 18

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

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; }

14 - 19

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

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

/** * Removes the element at the front of this queue and returns a * reference to it. * @return the element removed from the front of the queue * @throws 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; }

14 - 20

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

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.)

14 - 21

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

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Pre-Lab 3 #3Suppose 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?

14 - 22

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

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Pre-Lab 3 #4Suppose 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?

14 - 23

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

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Pre-Lab 3 #5 & 65. 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?

14 - 24

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

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Pre-Lab 3 #7Correct 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++;

}14 - 25

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

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Comparing queue implementations• Array Based & Linked List Based– Space comparison– Time comparison

14 - 26

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

Java Foundations, 3rd Edition, Lewis/DePasquale/Chase

Dequeue• http://

docs.oracle.com/javase/8/docs/api/java/util/Deque.html

• Linked List based implementation– Doubly-linked lists

14 - 27


Recommended