Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queues Cmput 115 - Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.

Similar presentations


Presentation on theme: "Queues Cmput 115 - Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from."— Presentation transcript:

1 Queues Cmput 115 - Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book: Java Structures by Duane A. Bailey or the companion structure package Revised 2/24/00

2 ©Duane Szafron 2000 2 About This Lecture In this lecture we will learn about an implementation of a linear data called a Queue.

3 ©Duane Szafron 2000 3Outline Queue in the Structure Container Hierarchy Queue Interface Maze - An example of using a Queue List Implementation of Queue Vector Implementation of Queue

4 ©Duane Szafron 2000 4 Linear Container Hierarchy In the structure package the following interface hierarchy appears. Store Linear StackQueue In the java.* packages there is no Queue as an interface or as a class.

5 ©Duane Szafron 2000 5 Structure Interface - Store public interface Store { public int size (); //post: returns the number of elements contained in // the store. public boolean isEmpty (); // post: returns the true iff store is empty. public void clear (); // post: clears the store so that it contains no // elements. } code based on Bailey pg. 18

6 ©Duane Szafron 2000 6 Structure Interface - Linear public interface Linear extends Store { public void add (Object anObject); // pre: anObject is non-null // post: the object is added to the container. The // consistent replacement policy is not specified public Object peek (); // pre: container is not empty // post: returns the next object to be removed, but does in // not remove it public Object remove (); // pre: container is not empty // post: removes an object from the container and returns it } code based on Bailey pg. 127

7 ©Duane Szafron 2000 7 Structure Interface - Queue 1 public interface Queue extends Linear { public void add (Object anObject); // post: the object is added at the tail. It will be // removed after all elements in front of it. public void enqueue (Object anObject); // post: the object is added at the tail. It will be // removed after all elements in front of it. public Object remove (); // pre: queue is not empty // post: the head of the queue is removed and returned code based on Bailey pg. 137

8 ©Duane Szafron 2000 8 Structure Interface - Queue 2 public Object dequeue (); // pre: queue is not empty // post: the head of the queue is removed and returned public Object peek (); // pre: queue is not empty // post: returns the element at the head of the queue, // but does not remove it } code based on Bailey pg. 137

9 ©Duane Szafron 2000 9 Queue Example - Maze Algorithm Like Stacks, Queues can also be used to store unsearched paths. 0 S F 1 6 10 5 2 7 11 4 3 8 9 Repeat as long as the current square is not null and is not the finish square: –“Visit” the square. –Enqueue one square on the queue for each unvisited legal move from the current square. –Dequeue the queue into the current square or bind the current square to null if the stack is empty. If the current square is the goal we are successful, otherwise there is no solution

10 ©Duane Szafron 2000 10 Queue Example - Searching The algorithm seems the same so what is the difference between using a Stack and a Queue? When a Stack is used, the search goes as deep along a single path as possible, before trying another path. When a Queue is used, the search expands a frontier of nodes equidistant from the start. 0 S F 1 6 10 5 2 7 11 4 3 8 9 0 S F 1 2 4 3 5 7 9 6 8 10 Stack Search Queue Search

11 ©Duane Szafron 2000 11 Queue Example - Searching 0 S F 1 6 10 5 2 7 11 4 3 8 9 0 S F 1 2 4 3 5 7 9 6 8 10 Stack Search Queue Search 1 3 6 5 4 2 7 8 11 10 9 1 4 3 11 7 2 5 8 9 6 10

12 ©Duane Szafron 2000 12 Queue Example - Maze Trace 1 P0 P1 0 S F 1 2 4 11 3 5 7 9 6 8 10 P1 P3 P2 0 S F 1 2 4 11 3 5 7 9 6 8 10 P2 P5 P4 0 S F 1 2 4 11 3 5 7 9 6 8 10 P3 P6 P5 0 S F 1 2 4 11 3 5 7 9 6 8 10 P4 P7 P6 0 S F 1 2 4 11 3 5 7 9 6 8 10 P5 P8 0 S F 1 2 4 11 3 5 7 9 6 8 10 P7 P6

13 ©Duane Szafron 2000 13 Queue Example - Maze Trace 2 P5 P8 P7 0 S F 1 2 4 11 3 5 7 9 6 8 10 P6 P9 P8 P7 0 S F 1 2 4 11 3 5 7 9 6 8 10 P7 P9 P8 0 S F 1 2 4 11 3 5 7 9 6 8 10 P8 P10 0 S F 1 2 4 11 3 5 7 9 6 8 10 P9 P11 0 S F 1 2 4 11 3 5 7 9 6 8 10 P10 Success! P10 P11 0 S F 1 2 4 11 3 5 7 9 6 8 10

14 ©Duane Szafron 2000 14 Maze Program Same program as in the Stack lecture, except replace the line: todo = new StackList (); // A class that implements Stack by: todo = new QueueList (); // A class that implements Queue

15 ©Duane Szafron 2000 15 List-Based Queues We can implement a Queue using a DoublyLinkedList or a CircularLinkedList. We do not use a SinglyLinkedList since operations on the tail take time proportional to the length of the list. As the Queue grows and shrinks, so does the list. The time for adding or removing a single element is always the same constant time. There are two extra link spaces required for each element. The implementation is simple, few mistakes are made.

16 ©Duane Szafron 2000 16 QueueList - State and Constructors public class QueueList implements Queue { /* An implementation of the Queue Interface that uses a DoublyLinkedList to store the Stack elements. */ // Instance Variables // protected List data; public QueueList () { // post: initialize the Stack to be empty this.data = new DoublyLinkedList(); //or CircularLinkedList } code based on Bailey pg. 140

17 ©Duane Szafron 2000 17 QueueList - Linear Interface 1 public void add (Object anObject) { // post: the object is added at the tail. It will be // removed after all elements in front of it. this.data.addToTail(anObject); } public Object remove () { // pre: queue is not empty // post: the head of the queue is removed and returned return this.data.removeFromHead(); } code based on Bailey pg. 141

18 ©Duane Szafron 2000 18 QueueList - Linear Interface 2 public Object peek () { // pre: queue is not empty // post: returns the element at the head of the queue, // but does not remove it Assert.pre(!this.isEmpty(), “Queue is not empty”); return this.data.peek(); } code based on Bailey pg. 142

19 ©Duane Szafron 2000 19 QueueList - Queue Interface public void enqueue (Object anObject) { // post: the object is added at the tail. It will be // removed after all elements in front of it. this.add(anObject); } public Object dequeue () { // pre: queue is not empty // post: the head of the queue is removed and returned return this.remove(); } code based on Bailey pg. 141

20 ©Duane Szafron 2000 20 QueueList - Store Interface public int size(); //post: returns the number of elements contained in the store. return this.data.size(); } public boolean isEmpty(); // post: returns the true iff store is empty. return this.size() == 0; } public void clear(); // post: clears the store so that it contains no elements. this.data.clear(); } code based on Bailey pg. 142

21 ©Duane Szafron 2000 21 Vector-Based Queues We can implement a Queue using a single Vector. However, this implementation is not time efficient. Adding an element to the Queue results in adding an element to the end of the Vector which takes constant time (except when the Vector must grow). However, removing an element from the Queue results in removing the zeroth element of the Vector which takes time proportional to the length of the Vector (since all elements after the zeroth must be moved to the left). Since this implementation has such poor performance, we will not use it.

22 ©Duane Szafron 2000 22 Array-Based Queues We can implement a Queue using a single Array if we know in advance the maximum size of the Queue. The naïve implementation of storing the head at index 0 and growing the queue towards the end of the Array is too inefficient, for the same reason that the Vector approach was inefficient. Instead, we maintain two indexes, the head and tail index and let them “slide” towards the end of the Array and then wrap around. "Fred""Barney""Wilma" 01234 "Fred""Barney""Wilma" headtail

23 ©Duane Szafron 2000 23 Sliding Indexes 1 01234 "Fred""Barney""Wilma" headtail add 01234 "Fred""Barney""Wilma" headtail "Betty" remove add 01234 "Pebbles""Barney""Wilma" headtail "Betty" 01234 headtail "Barney""Wilma""Betty"

24 ©Duane Szafron 2000 24 Sliding Indexes 2 remove add remove 01234 "Pebbles""Barney""Wilma" headtail "Betty" 01234 "Pebbles""Wilma" headtail "Betty" 01234 "Pebbles""Wilma" headtail "Betty""Slate" 01234 "Pebbles" headtail "Betty""Slate"

25 ©Duane Szafron 2000 25 Array-Based Queues - Empty and Full We leave one empty entry in the Queue. The condition for an empty Queue is: head == tail. The condition for a full Queue is: tail is one “behind” head.

26 ©Duane Szafron 2000 26 Some Principles from the Textbook 13. Understand the complexity of the Structure you use. principles from Bailey ch. 7


Download ppt "Queues Cmput 115 - Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from."

Similar presentations


Ads by Google