Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— Presentation transcript:

1

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

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

4

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

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

7 peek Operation peek is a queue operation that returns an existing element from the front end of a queue without removing the element.

8 isEmpty Operation isEmpty is a queue operation that determines if a queue is empty.

9 Constructing a Queue – 1 New integer queue FrontBack

10 Constructing a Queue – 2 enQueue(157) 157 Front Back

11 Constructing a Queue – 3 enQueue(999) 157999 Front Back

12 Constructing a Queue – 4 enQueue(500) 157999500 Front Back

13 Constructing a Queue – 4 x = deQueue() 999500 Front Back x 157

14 Constructing a Queue – 5 enQueue(x) 999500157 Front Back x 157

15 Constructing a Queue – 6 x = deQueue() 500157 Front Back x 999

16 Constructing a Queue – 7 enQueue(x) 500157999 Front Back x 999

17 Constructing a Queue – 8 enQueue(x) 500157999 Front Back x 999

18 Constructing a Queue – 9 x = deQueue() 157999 Front Back x 500

19

20 // Java3001.java // This file is not a program, but the method headings of the 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 { }

21 // Java3002.java // This program uses a object to store students names. // It demonstrates the use of the and 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(); }

22 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 temp = new LinkedList ();

23 // Java3003.java This program introduces the and methods. // This program uses "class casting" with the 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(); }

24 // 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 students = new LinkedList (); Queue temp = new LinkedList (); 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(); }

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

26 // Java3005.java This program shows how to remove an element from a 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 students = new LinkedList (); Queue temp = new LinkedList (); 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(); }

27 Java3005 Logic Error Demo Step 1 LukeBrianMikeJamie FrontBack Front

28 Java3005 Logic Error Demo Step 2 MikeJamie LukeBrian BackFront BackFront

29 Java3005 Logic Error Demo Step 3 - Mike is deleted Jamie LukeBrian BackFront BackFront

30 Java3005 Logic Error Demo Step 4 - temp items returned out of order JamieLukeBrian FrontBack FrontBack

31 // Java3006.java // This program shows how to remove an element from a 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 students = new LinkedList (); Queue temp = new LinkedList (); 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(); }

32 Java3006 No Error Demo Step 1 LukeBrianMikeJamie FrontBack Front

33 Java3006 No Error Demo Step 2 MikeJamie LukeBrian BackFront BackFront

34 Java3005 Logic Error Demo Step 3 - Mike is deleted Jamie LukeBrian BackFront BackFront

35 Java3005 Logic Error Demo Step 4 - students queue is emptied LukeBrianJamie BackFront Back

36 Java3005 Logic Error Demo Step 5 - All temp items returned in order LukeBrianJamie FrontBack FrontBack

37 // Java3007.java // This program demonstrates how to use a primitive type like 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 numbers = new LinkedList (); 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(); }

38 // Java3008.java // This program creates a object of 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 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 students = new LinkedList (); 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();

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

40 // 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 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"); }

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

42

43 // Java3010.java // This program implements an 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(); }

44 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 public MyQueue1() // Initializes an empty array object. { data = new int[100]; back = front = 0; } public boolean isEmpty() // Returns true if data is empty, // false otherwise { return front == back; } public int peek() { return data[front]; } // Returns the front element from // the queue without removal public void add (int x) // Adds variable x to the top of the queue { data[back] = x; back++; } public int remove() // Returns and removes the front element // from the queue { int temp = data[front]; front++; return temp; }

45 // 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 integersdeQueue 51 integersenQueue 51 more integers

46 Why did the previous program crash? 157999 Front Back 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.

47 // 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 integersdeQueue 60 integersenQueue 60 more integers

48 class MyQueue3 { private int data[]; // stores queue data private int front; // keeps index of the queue front private int back; // keeps index of the queue back public MyQueue3() // Initializes an empty array object. { data = new int[100]; back = 0; front = 0; } public boolean isEmpty() // Returns true if data is empty, false otherwise { return back == front; } public void add (int x) // Adds variable x to the "back" of the queue { if (back == data.length) back = 0; data[back] = x; back++; } public int remove() // Returns and removes the "front" element // from the queue { if (front == data.length) front = 0; int temp = data[front]; front++; return temp; } public int peek() { return data[front]; } // Returns the front element from the queue // without removal }

49 Wrap-Around Logic - 1 157999 Front Back This queue is NOT full, but the back pointer is at the last index of the static array.

50 Wrap-Around Logic - 2 enQueue(500) 500157999 Front Back

51 Wrap-Around Logic - 3 enQueue(747) 500747157999 Front Back

52 Wrap-Around Logic - 4 deQueue() 500747999 Front Back

53 Wrap-Around Logic - 5 enQueue(365) 500747365999 Front Back

54 Wrap-Around Logic - 6 deQueue() 500747365999 Front Back

55 Wrap-Around Logic - 7 deQueue() 500747365 Front Back

56 Wrap-Around Logic - 8 enQueue(121) 500747365121 Front Back

57 Wrap-Around Logic - 9 enQueue(316) 500747365121316 Front Back

58 Wrap-Around Logic - 10 deQueue() 747365121316 Front Back

59 Wrap-Around Logic - 11 enQueue(441) 747365121316441 Front Back

60 Wrap-Around Logic - 12 enQueue(555) 555747365121316441 Front Back NOTE: Even with Wrap-Around Logic, a Queue implemented as a Java Static Array can still run out of space.

61

62 // Java3013.java // This program shows the implementation of a dynamic queue class. // MyQueue4 is also declared as a generic class. import java.util.ArrayList; public class Java3013 { public static void main (String args[]) { System.out.println("JAVA3013.JAVA\n"); MyQueue4 students = new MyQueue4 (); students.add("Luke Watts"); students.add("Brian Sims"); students.add("Mike Lewis"); students.add("Jamie Singbush"); 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(); while (!students.isEmpty()) { String student = (String) students.remove(); System.out.println(student); } System.out.println(); }

63 class MyQueue4 { private ArrayList data; // stores queue data private int front; // keeps index of the queue front public MyQueue4() // Initializes an empty queue object. { data = new ArrayList (); front = 0; } public boolean isEmpty() // Returns true if data is empty, false otherwise { return data.size() == 0; } public void add(E x) // Adds variable x to the back of the queue { data.add(x); } public E remove() // Returns and removes the front element from the queue { return data.remove(front); } public E peek() // Returns the front element from the queue without removal { return data.get(front); }

64 // Java3014.java // This program shows how the queue implementation with an object // works correctly without using the "wrap-around" data storage. import java.util.ArrayList; public class Java3014 { public static void main (String args[]) { System.out.println("JAVA3014.JAVA\n"); MyQueue4 queue = new MyQueue4 (); for (int k = 0; k < 90; k++) queue.add(new Integer(k)); for (int k = 0; k < 60; k++) { int number = queue.remove(); System.out.print(number + " "); } for (int k = 60; k < 120; k++) queue.add(new Integer(k)); System.out.println("\n\n"); while (!queue.isEmpty()) { int number = queue.remove(); System.out.print(number + " "); } System.out.println("\n\n"); } enQueue 90 integersdeQueue 60 integersenQueue 60 more integers

65 // Java3015.java // This program demonstrates interface capabilities that should NOT be // used in an AP course. There are methods available due to inheritance features // that do not follow queue ADT properties. In an AP course access to a queue is // strictly meant to add at the end and remove from the front. Queue methods // should be limited to using,,, only. import java.util.*; public class Java3015 { public static void main(String args[]) { System.out.println("Java3015.java\n\n"); Queue queue = new LinkedList (); for (int k = 1; k <= 10; k++) queue.add(k); System.out.println("Removing number 3"); queue.remove(3); System.out.println(queue); System.out.println("\n\n"); }

66 APCS Exam Alert Only use methods add, remove, peek and isEmpty with any Queue objects. Use of other methods like get or remove(index) violates the abstract data definition of a queue, and may result in point deductions if used on solutions with the free response segment of the exam.

67

68 // Java3016.java // Complete method so that it removes the parameter number, but // leaves the remaining queue intact. import java.util.*; public class Java3016 { public static void main(String args[]) { System.out.println("Java3016.java\n\n"); Queue queue = new LinkedList (); for (int k = 1; k <= 10; k++) queue.add(k); System.out.println(queue); delete(queue,5); System.out.println(queue); System.out.println("\n\n"); } public static void delete(Queue q, int number) { }

69 // Java3017.java // Complete method so that it reverses the queue members. import java.util.*; public class Java3017 { public static void main(String args[]) { System.out.println("Java3017.java\n\n"); Queue queue = new LinkedList (); for (int k = 1; k <= 10; k++) queue.add(k); System.out.println(queue); reverse(queue); System.out.println(queue); System.out.println("\n\n"); } public static void reverse(Queue q) { }

70 // Java3018.java // This program is intended to demonstrate how to make // a copy of an existing queue. The output appears correct. // Are you satisfied that this is correct? import java.util.*; public class Java3018 { public static void main(String args[]) { System.out.println("Java3018.java\n\n"); Queue queue1 = new LinkedList (); Queue queue2 = new LinkedList (); for (int k = 1; k <= 10; k++) queue1.add(k); queue2 = queue1; System.out.println(queue1); System.out.println(queue2); System.out.println("\n\n"); }

71 // Java3019.java // This program shows that simple assignment does not work for copying // objects. This is known as an "aliasing" problem. import java.util.*; public class Java3019 { public static void main(String args[]) { System.out.println("Java3019.java\n\n"); Queue queue1 = new LinkedList (); Queue queue2 = new LinkedList (); for (int k = 1; k <= 10; k++) queue1.add(k); queue2 = queue1; queue2.remove(); System.out.println(queue1); System.out.println(queue2); System.out.println("\n\n"); }

72 // Java3020.java // Complete method so that the second parameter becomes // a correct copy of the first parameter. import java.util.*; public class Java3020 { public static void main(String args[]) { System.out.println("Java3020.java\n\n"); Queue queue1 = new LinkedList (); Queue queue2 = new LinkedList (); for (int k = 1; k <= 10; k++) queue1.add(k); copy(queue1,queue2); queue2.remove(); System.out.println(queue1); System.out.println(queue2); System.out.println("\n\n"); } public static void copy(Queue q1, Queue q2) { }

73

74 // Java3021.java // This program demonstrates how to declare a object // and add new elements with the method. import java.util.*; public class Java3021 { public static void main(String args[]) { System.out.println("Java3021.java\n\n"); PriorityQueue pqueue = new PriorityQueue (); pqueue.add("Ann"); pqueue.add("Bob"); pqueue.add("Dee"); pqueue.add("Eve"); pqueue.add("Joe"); System.out.println(pqueue); System.out.println("\n\n"); }

75 // Java3022.java // This program demonstrates the and methods of the // class. // At this stage the class appears no different from the class. import java.util.*; public class Java3022 { public static void main(String args[]) { System.out.println("Java3022.java\n\n"); PriorityQueue pqueue = new PriorityQueue (); pqueue.add("Ann"); pqueue.add("Bob"); pqueue.add("Dee"); pqueue.add("Eve"); pqueue.add("Joe"); while(!pqueue.isEmpty()) System.out.println(pqueue.remove()); System.out.println("\n\n"); }

76 // Java3023.java // This program demonstrates the method, which displays the value at the "head" // of the queue. This priority queue appears to base priority on ascending order of values. import java.util.*; public class Java3023 { public static void main(String args[]) { System.out.println("Java3023.java\n\n"); PriorityQueue pqueue = new PriorityQueue (); pqueue.add("Bob"); System.out.println("Head value is " + pqueue.peek()); pqueue.add("Dee"); System.out.println("Head value is " + pqueue.peek()); pqueue.add("Eve"); System.out.println("Head value is " + pqueue.peek()); pqueue.add("Ann"); System.out.println("Head value is " + pqueue.peek()); pqueue.add("Joe"); System.out.println("Head value is " + pqueue.peek()); System.out.println("\n\n"); }

77 // Java3024.java // This program demonstrates that the implementation of the // is only a representation of stored values, but provides no indication about the // priority of the data. import java.util.*; public class Java3024 { public static void main(String args[]) { System.out.println("Java3024.java\n\n"); PriorityQueue pqueue = new PriorityQueue (); pqueue.add("Bob"); System.out.println(pqueue); pqueue.add("Dee"); System.out.println(pqueue); pqueue.add("Eve"); System.out.println(pqueue); pqueue.add("Ann"); System.out.println(pqueue); pqueue.add("Joe"); System.out.println(pqueue); System.out.println("\nActual priority order"); while(!pqueue.isEmpty()) System.out.print(pqueue.remove() + " "); System.out.println("\n\n"); }

78 // Java3025.java // This program demonstrates that priority ordering also applies to numeric values in // ascending order. This program also demonstrates again that using gives // no indication to any priority ordering. import java.util.*; public class Java3025 { public static void main(String args[]) { System.out.println("Java3025.java\n\n"); PriorityQueue pqueue = new PriorityQueue (); Random rnd = new Random(1234); for (int k = 1; k <= 48; k++) { int rndInt = rnd.nextInt(900) + 100; pqueue.add(rndInt); } System.out.println(pqueue); System.out.println("\n\n"); while(!pqueue.isEmpty()) System.out.print(pqueue.remove() + " "); System.out.println("\n\n"); }

79 // Java3026.java // This program investigates using a priority queue with a user-defined class. // The program compiles, but it crashes with a "ClassCastException" runtime error. import java.util.*; public class Java3026 { public static void main(String args[]) { System.out.println("Java3026.java\n\n"); PriorityQueue pqueue = new PriorityQueue(); pqueue.add(new Student("Tom",21,2.785)); pqueue.add(new Student("Ann",40,3.555)); pqueue.add(new Student("Joe",61,2.000)); pqueue.add(new Student("Bob",35,3.275)); pqueue.add(new Student("Dee",55,3.999)); while(!pqueue.isEmpty()) System.out.print(pqueue.remove() + " "); System.out.println("\n\n"); } class Student { private String name; private int age; private double gpa; public Student(String n, int a, double g) { name = n; age = a; gpa = g; } public String toString() { return name; } }

80 // Java3027.java // A object can only stores objects that have implemented. // There is no problem using a user-defined class, but it must define the method. // This program uses the priority queue ordered according to student name. import java.util.*; public class Java3027 { public static void main(String args[]) { System.out.println("Java3027.java\n\n"); PriorityQueue pqueue = new PriorityQueue (); pqueue.add(new Student("Tom",21,2.785)); pqueue.add(new Student("Ann",40,3.555)); pqueue.add(new Student("Joe",61,2.000)); pqueue.add(new Student("Bob",35,3.275)); pqueue.add(new Student("Dee",55,3.999)); while(!pqueue.isEmpty()) System.out.print(pqueue.remove() + " "); System.out.println("\n\n"); } class Student implements Comparable { private String name; private int age; private double gpa; public Student(String n, int a, double g) { name = n; age = a; gpa = g; } public String toString() { return name; } // Note how defining use the class implementation of public int compareTo (Object source) { Student temp = (Student) source; return name.compareTo(temp.name); }

81 // Java3028.java // This program is almost identical to the previous program. The implementation of // is slightly different and results in a descending order priority.import java.util.*; public class Java3028 { public static void main(String args[]) { System.out.println("Java3028.java\n\n"); PriorityQueue pqueue = new PriorityQueue (); pqueue.add(new Student("Tom",21,2.785)); pqueue.add(new Student("Ann",40,3.555)); pqueue.add(new Student("Joe",61,2.000)); pqueue.add(new Student("Bob",35,3.275)); pqueue.add(new Student("Dee",55,3.999)); while(!pqueue.isEmpty()) System.out.print(pqueue.remove() + " "); System.out.println("\n\n"); } class Student implements Comparable { private String name; private int age; private double gpa; public Student(String n, int a, double g) { name = n; age = a; gpa = g; } public String toString() { return name; } public int compareTo (Object source) { Student temp = (Student) source; return temp.name.compareTo(name); }

82 // Java3029.java // This program creates a priority queue that is ordered according to student age. // The method is implemented differently, because cannot be used // with. // The method is redefined to display age. import java.util.*; public class Java3029 { public static void main(String args[]) { System.out.println("Java3029.java\n\n"); PriorityQueue pqueue = new PriorityQueue (); pqueue.add(new Student("Tom",21,2.785)); pqueue.add(new Student("Ann",40,3.555)); pqueue.add(new Student("Joe",61,2.015)); pqueue.add(new Student("Bob",35,3.275)); pqueue.add(new Student("Dee",55,3.999)); while(!pqueue.isEmpty()) System.out.println(pqueue.remove()); System.out.println("\n\n"); }

83 class Student implements Comparable { private String name; private int age; private double gpa; public Student(String n, int a, double g) { name = n; age = a; gpa = g; } public int compareTo (Object source) { Student temp = (Student) source; if (age < temp.age) return -1; else if (age == temp.age) return 0; else return 1; } public String toString() { return String.valueOf(age); } } NOTE: return age - temp.age; will also work.

84 // Java3030.java // Complete the method below to order the priority queue from highest to lowest gpa. // Redefine the method to display student records in the format [Tom, 21, 2.785]; import java.util.*; public class Java3030 { public static void main(String args[]) { System.out.println("Java3030.java\n\n"); PriorityQueue pqueue = new PriorityQueue (); pqueue.add(new Student("Tom",21,2.785)); pqueue.add(new Student("Ann",40,3.555)); pqueue.add(new Student("Joe",61,2.015)); pqueue.add(new Student("Bob",35,3.275)); pqueue.add(new Student("Dee",55,3.999)); while(!pqueue.isEmpty()) System.out.println(pqueue.remove()); System.out.println("\n\n"); } class Student implements Comparable { private String name; private int age; private double gpa; public Student(String n, int a, double g) { name = n; age = a; gpa = g; } public int compareTo (Object source) { } public String toString() { } }


Download ppt "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,"

Similar presentations


Ads by Google