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,

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
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.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
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.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
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.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
CS Data Structures II Review COSC 2006 April 14, 2017
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. 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.
CSE 143 Lecture 7 Stacks and Queues reading: Stuart Reges notes on website slides created by Marty Stepp
Stacks, Queues, and Deques
Building Java Programs
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Copyright © Texas Education Agency, Advanced Computer Programming Data Structures: Collections.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Algorithm Definition An algorithm is a step-by-step solution to a problem.
First Data Structure Definition A data structure is a data type whose components are smaller data structures and/or simple data types.
Introduction Chapter 12 introduced the array data structure. Historically, the array was the first data structure used by programming languages. With.
The HashSet, TreeSet, HashMap and TreeMap are four classes that are part of the Java AB subset, which may be tested on the AP Computer Science Examination.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
© A+ Computer Science - A queue is a group of same-type values. Values are added to the back of a queue and removed from the front.
Data structures Abstract data types Java classes for Data structures and ADTs.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Introduction to Data Structures and Algorithms
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Chapter 7 Queues Introduction Queue applications Implementations.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Introduction Chapter 12 introduced the array data structure. Historically, the array was the first data structure used by programming languages. With.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Click to edit Master text styles Stacks Data Structure.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Exposure Java 2011 APCS Edition
CSE 373: Data Structures and Algorithms
Stacks and Queues.
CMSC 341 Lecture 5 Stacks, Queues
Pointers and Linked Lists
Stacks, Queues, and Deques
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Introduction to Data Structure
Stacks, Queues, and Deques
Presentation transcript:

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.

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.

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.

Constructing a Queue – 1 New integer queue FrontBack

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

Constructing a Queue – 3 enQueue(999) Front Back

Constructing a Queue – 4 enQueue(500) Front Back

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

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

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

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

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

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

// 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 { }

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

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 ();

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

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

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

Java3005 Logic Error Demo Step 1 LukeBrianMikeJamie FrontBack Front

Java3005 Logic Error Demo Step 2 MikeJamie LukeBrian BackFront BackFront

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

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

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

Java3006 No Error Demo Step 1 LukeBrianMikeJamie FrontBack Front

Java3006 No Error Demo Step 2 MikeJamie LukeBrian BackFront BackFront

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

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

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

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

// 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();

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 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 = ; 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 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.

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

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

// 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

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

// 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

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 }

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

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

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

Wrap-Around Logic - 4 deQueue() Front Back

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

Wrap-Around Logic - 6 deQueue() Front Back

Wrap-Around Logic - 7 deQueue() Front Back

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

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

Wrap-Around Logic - 10 deQueue() Front Back

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

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

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

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

// 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

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

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.

// 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) { }

// 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) { }

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

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

// 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) { }

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

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

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

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

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

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

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

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

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

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.

// 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() { } }