840 likes | 958 Views
Exposure Java-AB 2008. Chapter 30 Slides. The Queue and Priority Queue. PowerPoint Presentation created by: Mr. John L. M. Schram. From Materials Created by Mr. Leon Schram. Queue Definition.
E N D
Exposure Java-AB 2008 Chapter 30 Slides The Queue and Priority Queue PowerPoint Presentation created by: Mr. John L. M. Schram From Materials Created by Mr. Leon Schram
Queue Definition A queue is a data structure with elements of the same type. Queue elements can only be entered at one end of the queue, called the back, and removed from the other end of the queue, called the front, in a First In First Out (FIFO) manner. "Queue" is the British word for "line".
Have you heard of "Print Queues"? A Print Queue stores print jobs until the printer is ready to print them. It gets its name from the fact that the print jobs are printed in a FIFO manner.
Queue Operations
enQueue or add Operation enQueue, or add in Java, is a queue operation that adds a new element at the back end of a queue, which is the opposite end from where queue elements are removed.
deQueue or remove Operation deQueue, or remove in Java, is a queue operation that removes an existing element from the front end of a queue, which is the opposite end from where queue elements are added.
peek Operation peek is a queue operation that returns an existing element from the front end of a queue without removing the element.
isEmpty Operation isEmpty is a queue operation that determines if a queue is empty.
Front Back Constructing a Queue – 1New integer queue
Constructing a Queue – 2enQueue(157) Front Back
Constructing a Queue – 3enQueue(999) Front Back
Constructing a Queue – 4enQueue(500) Front Back
Constructing a Queue – 4x = deQueue() Front Back
Constructing a Queue – 5enQueue(x) Front Back
Constructing a Queue – 6x = deQueue() Front Back
Constructing a Queue – 7enQueue(x) Front Back
Constructing a Queue – 8enQueue(x) Again! Front Back
Constructing a Queue – 9x = deQueue() Front Back
Using the Queue interface
// Java3001.java // This file is not a program, but the method headings of the <Queue> interface. public boolean isEmpty() // Returns true if queue is empty, false otherwise { } public boolean add (E item) // used to be called enQueue // Adds variable item to the back of the queue; returns true { } public E remove() // used to be called deQueue // Returns and removes the front element from the queue { } public E peek() // Returns the front element from the queue without removal { }
// Java3002.java // This program uses a <Queue> object to store students names. // It demonstrates the use of the <add> and <peek> methods. import java.util.*; public class Java3002 { public static void main (String args[]) { System.out.println("JAVA3002.JAVA\n"); Queue students = new LinkedList(); students.add("Luke Watts"); System.out.println("Enqueueing Luke Watts"); System.out.println("Queue front contains " + students.peek()); students.add("Brian Sims"); System.out.println("\nEnqueueing Brian Sims"); System.out.println("Queue front contains " + students.peek()); students.add("Mike Lewis"); System.out.println("\nEnqueueing Mike Lewis"); System.out.println("Queue front contains " + students.peek()); students.add("Jamie Singbush"); System.out.println("\nEnqueueing Jamie Singbush"); System.out.println("Queue front contains " + students.peek()); System.out.println(); } }
Using the Queue interface Identifier Queue is an interface and has no constructor. Objects of the Queue interface must be instantiated with an implementing class. In Java the LinkedList class and the PriorityQueue class both implement the Queue interface. In this chapter some Queue implementations will use the LinkedList class in the following "older" manner : Queue students = new LinkedList(); You will also see some Queue implementations with the same LinkedList class, along with the identifier of the class object that will be stored in the queue, using the "generic" class approach: Queue<String> temp = new LinkedList<String>();
// Java3003.java This program introduces the <remove> and <isEmpty> methods. // This program uses "class casting" with the <remove> method. import java.util.*; public class Java3003 { public static void main (String args[]) { System.out.println("JAVA3003.JAVA\n"); Queue students = new LinkedList(); students.add("Luke Watts"); System.out.println("Enqueueing Luke Watts"); students.add("Brian Sims"); System.out.println("Enqueueing Brian Sims"); students.add("Mike Lewis"); System.out.println("Enqueueing Mike Lewis"); students.add("Jamie Singbush"); System.out.println("Enqueueing Jamie Singbush"); System.out.println(); while (!students.isEmpty()) { String student = (String) students.remove(); System.out.println("Dequeueing " + student); } System.out.println(); if (students.isEmpty()) System.out.println("The queue is empty"); else System.out.println("Queue front contains " + students.peek()); System.out.println(); } }
// Java3004.java // This program shows how to display the elements in a queue with a temporary queue. // This also demonstrates how a queue accesses elements as a FIFO. // This program uses the Java 5.0 "generics" feature. import java.util.*; public class Java3004 { public static void main (String args[]) { System.out.println("JAVA3004.JAVA\n"); Queue<String> students = new LinkedList<String>(); Queue<String> temp = new LinkedList<String>(); students.add("Luke Watts"); students.add("Brian Sims"); students.add("Mike Lewis"); students.add("Jamie Singbush"); while (!students.isEmpty()) { String student = students.remove(); System.out.println("deQueueing " + student + " from student queue; enQueueing on temp queue"); temp.add(student); } System.out.println(); while (!temp.isEmpty()) { String student = temp.remove(); System.out.println("deQueueing " + student + " from temp queue"); } System.out.println(); } }
Using a Temporary Queue A queue can only be dequeued at one location. Access to any element besides the front element, requires that the front element and possibly additional elements are dequeued from the queue. A temporary queue needs to store dequeued elements so that all the necessary elements can be returned to the original queue.
// Java3005.java This program shows how to remove an element from a <Queue> object // and "supposedly" preserve the remaining object sequence. import java.util.*; public class Java3005 { public static void main (String args[]) { System.out.println("JAVA3005.JAVA\n"); Queue<String> students = new LinkedList<String>(); Queue<String> temp = new LinkedList<String>(); students.add("Luke Watts"); students.add("Brian Sims"); students.add("Mike Lewis"); students.add("Jamie Singbush"); System.out.println(students); boolean found = false; while (!students.isEmpty() && !found) { String student = students.remove(); if (student.equals("Mike Lewis")) { System.out.println(student + " is found and removed from the queue"); found = true; } else temp.add(student); } System.out.println(); while (!temp.isEmpty()) { String student = temp.remove(); students.add(student); } System.out.println(students); System.out.println(); } }
Front Back Java3005 Logic Error DemoStep 1 students Front Back temp
Java3005 Logic Error DemoStep 2 students Front Back temp Front Back
Java3005 Logic Error DemoStep 3 - Mike is deleted students Front Back temp Front Back
Front Back Java3005 Logic Error DemoStep 4 - temp items returned out of order students Front Back temp
// Java3006.java // This program shows how to remove an element from a <MyQueue> object // and preserve the remaining object sequence correctly. import java.util.*; public class Java3006 { public static void main (String args[]) { System.out.println("JAVA3006.JAVA\n"); Queue<String> students = new LinkedList<String>(); Queue<String> temp = new LinkedList<String>(); students.add("Luke Watts"); students.add("Brian Sims"); students.add("Mike Lewis"); students.add("Jamie Singbush"); System.out.println(students); while (!students.isEmpty()) { String student = students.remove(); if (student.equals("Mike Lewis")) System.out.println(student + " is found and removed from the queue"); else temp.add(student); } System.out.println(); while (!temp.isEmpty()) { String student = temp.remove(); students.add(student); } System.out.println(students); System.out.println(); } }
Front Back Java3006 No Error DemoStep 1 students Front Back temp
Java3006 No Error DemoStep 2 students Front Back temp Front Back
Java3005 Logic Error DemoStep 3 - Mike is deleted students Front Back temp Front Back
Front Back Java3005 Logic Error DemoStep 4 - students queue is emptied students temp Front Back
Front Back Java3005 Logic Error DemoStep 5 - All temp items returned in order students Front Back temp
// Java3007.java // This program demonstrates how to use a primitive type like <int> with a queue. // This program uses Java 5.0 "autoboxing" and "generics" features. import java.util.*; public class Java3007 { public static void main (String args[]) { System.out.println("JAVA3007.JAVA\n"); Queue<Integer> numbers = new LinkedList<Integer>(); for (int k = 1000; k <= 1008; k++) { System.out.println("Enqueueing " + k + " on the queue."); numbers.add(k); } System.out.println(); while (!numbers.isEmpty()) { int number = numbers.remove(); System.out.println("Dequeueing " + number + " from the queue."); } System.out.println(); } }
// Java3008.java // This program creates a <Queue> object of <Person> objects that are retrieved from an external file. // Java "generics" is not limited to existing classes. // In this program the queue object is declared to store <Person> objects. import java.io.*; import java.util.*; public class Java3008 { public static void main(String args[]) throws IOException { System.out.println("JAVA3008.JAVA\n"); BufferedReader inStream = new BufferedReader(new FileReader("Students.txt")); Person student; Queue<Person> students = new LinkedList<Person>(); String s1,s2,s3; while( ((s1 = inStream.readLine()) != null) && ((s2 = inStream.readLine()) != null) && ((s3 = inStream.readLine()) != null) ) { String name = s1; int age = Integer.parseInt(s2); double gpa = Double.parseDouble(s3); student = new Person(name,age,gpa); System.out.println("Enqueueing " + student.getName() + " \t\t" + student.getAge() + "\t\t" + student.getGPA()); students.add(student); } inStream.close(); System.out.println(); Continued on the next slide
while (!students.isEmpty()) { student = students.deQueue(); System.out.println("Dequeueing " + student.getName() + " \t\t" + student.getAge() + "\t\t" + student.getGPA()); } System.out.println(); } } class Person { private String name; private int age; private double gpa; Person(String n,int a,double g) { name = n; age = a; gpa = g; } public String getName() { return name; } public int getAge() { return age; } public double getGPA() { return gpa; } }
// Java3009.java // This program demonstrates how it is possible to stores elements of different data // types on a queue. Every queue element is technically the same <Object> type, but // in reality it is a reference to four different data type elements. // This is not a proper way to use a queue data structure. import java.util.*; public class Java3009 { public static void main (String args[]) { System.out.println("JAVA3009.JAVA\n"); Queue queueObj = new LinkedList(); int element1 = 1234; double element2 = 3.14159; String element3 = "Grace Hopper"; Person element4 = new Person("John Doe",25,3.475); queueObj.add(new Integer(element1)); System.out.println("Enqueueing 1234 on the stack"); queueObj.add(new Double(element2)); System.out.println("Enqueueing 3.14159 on the stack"); queueObj.add(element3); System.out.println("Enqueueing Grace Hopper on the stack"); queueObj.add(element4); System.out.println("Enqueueing John Doe (25 & 3.475) on the stack"); } }
Queue Data Note Every element in a queue is the same data type. If this data type is a reference, like it is was with implementation of the Queue interface used in the previous program example, then it is possible to store different types of data at the referenced memory location.
Implementing MyQueue Statically
// Java3010.java // This program implements an <int> queue data structure with a static Java array. public class Java3010 { public static void main (String args[]) { System.out.println("JAVA3010.JAVA\n"); MyQueue1 queue1 = new MyQueue1(); queue1.add(1111); queue1.add(2222); queue1.add(3333); queue1.add(4444); System.out.println(); if (queue1.isEmpty()) System.out.println("The queue is empty"); else System.out.println("Queue front contains " + queue1.peek()); System.out.println(); while (!queue1.isEmpty()) { int number = queue1.remove(); System.out.println(number); } System.out.println(); } } MyQueue1 class shown on next slide
class MyQueue1 { private int data[]; // stores queue data private int front; // keeps index of the queue front private int back; // keeps index of the queue back
// Java3011.java // This program shows that this queue implementation can have problems even if there // should be sufficient storage space. public class Java3011 { public static void main (String args[]) { System.out.println("JAVA3011.JAVA\n"); MyQueue2 queue2 = new MyQueue2(); for (int k = 10; k <= 99; k++) queue2.add(k); for (int k = 10; k <= 60; k++) { int number = queue2.remove(); System.out.print(number + " "); } for (int k = 100; k <= 150; k++) queue2.add(k); System.out.println(); } } enQueue 90 integers deQueue 51 integers enQueue 51 more integers MyQueue2 class is the same as MyQueue1 from the last program
Why did the previousprogram crash? Front In this example, the queue can hold 6 integers. Even though there are only 3 integers in the queue, any attempt to enqueue another integer will cause the program to crash. Back
// Java3012.java // This program solves the problem of insufficient storage by using // "wrap-around" logic with the static array data structure. public class Java3012 { public static void main (String args[]) { System.out.println("JAVA3012.JAVA\n"); MyQueue3 queue3 = new MyQueue3(); for (int k = 0; k < 90; k++) queue3.add(k); for (int k = 0; k < 60; k++) { int number = queue3.remove(); System.out.print(number + " "); } for (int k = 60; k < 120; k++) queue3.add(k); System.out.println("\n\n"); while (!queue3.isEmpty()) { int number = queue3.remove(); System.out.print(number + " "); } System.out.println("\n\n"); } } enQueue 90 integers deQueue 60 integers enQueue 60 more integers MyQueue3 class shown on next slide
Wrap-Around Logic - 1 Front This queue is NOT full, but the back pointer is at the last index of the static array. Back
Wrap-Around Logic - 2 enQueue(500) Front Back