Queue, Deque, and Priority Queue Implementations

Download Queue, Deque, and Priority Queue Implementations

Post on 21-Dec-2015

215 views

Category:

Documents

1 download

Embed Size (px)

TRANSCRIPT

<ul><li> Slide 1 </li> <li> Queue, Deque, and Priority Queue Implementations </li> <li> Slide 2 </li> <li> 2 Specifications for the ADT Queue Interface for a queue of objects public interface queueInterface { //add a new entry to the back of the queue public void enqueue(Object newEntry); //remove and return the front of the queue public Object dequeue(); //retrieve the front of the queue public Object getFront(); //determine whether the queue is empty public boolean isEmpty(); //remove all entries from the queue public void clear(); } </li> <li> Slide 3 </li> <li> 3 Specifications of the ADT Deque A Java interface public interface DequeInterface { public void addToFront(Object newEntry); public void addToBack(Object newEntry); public Object removeFront(); public Object removeBack(); public Object getFront(); public Object getBack(); public boolean isEmpty(); public void clear(); } </li> <li> Slide 4 </li> <li> 4 A Linked Implementation of a Queue Use chain of linked nodes for the queue Two ends at opposite ends of chain Accessing last node inefficient Could keep a reference to the tail of the chain Place front of queue at beginning of chain Place back of queue at end of chain With references to both </li> <li> Slide 5 </li> <li> 5 A Linked Implementation of a Queue A chain of linked nodes that implements a queue. Front of queue Back of queue </li> <li> Slide 6 </li> <li> 6 A Linked Implementation of a Queue (a)Before adding a new node to an empty chain; (b)after adding to it. </li> <li> Slide 7 </li> <li> 7 A Linked Implementation of a Queue (a)Before adding a new node to the end of a chain; (b)after adding it. </li> <li> Slide 8 </li> <li> 8 A Linked Implementation of a Queue (a) A queue of more than one entry; (b) after removing the queue's front. </li> <li> Slide 9 </li> <li> 9 A Linked Implementation of a Queue (a) A queue of one entry; (b) after removing the queue's front. </li> <li> Slide 10 </li> <li> 10 Array-Based Implementation of a Queue Let queue[0] be the front frontIndex, backIndex are indices of front and back If we insist queue[0] is front Must shift entries when we remove the front Instead move frontIndex Problem then is array can become full But now beginning of array could be empty and available for use </li> <li> Slide 11 </li> <li> 11 Array-Based Implementation of a Queue An array that represents a queue without shifting its entries: (a) initially; (b) after removing the front twice; </li> <li> Slide 12 </li> <li> 12 Array-Based Implementation of a Queue An array that represents a queue without shifting its entries: (c) after several more additions &amp; removals; (d) after two additions that wrap around to the beginning of the array </li> <li> Slide 13 </li> <li> 13 A Circular Array When queue reaches end of array Add subsequent entries to beginning Array behaves as though it were circular First location follows last one Use modulo arithmetic on indices enqueue: backIndex =(backIndex+1)% queue.length dequeue: frontIndex =(frontIndex+1)% queue.length Note: with circular array frontIndex =(backIndex+1)% queue.length both when queue is empty and when full </li> <li> Slide 14 </li> <li> 14 A Circular Array A circular array that represents a queue: (a) when full; (b) after removing 2 entries; (c) after removing 3 more entries; </li> <li> Slide 15 </li> <li> 15 A Circular Array A circular array that represents a queue: (d) after removing all but one entry; (e) after removing remaining entry. </li> <li> Slide 16 </li> <li> 16 A Circular Array with One Unused Location A seven-location circular array that contains at most six entries of a queue continued Note: queue is full when: frontIndex equals (backIndex + 2) % queue.length Allows us to distinguish between empty and full queue Note initial values </li> <li> Slide 17 </li> <li> 17 A Circular Array with One Unused Location (continued) A seven-location circular array that contains at most six entries of a queue. Note: queue is full when: frontIndex equals (backIndex + 2) % queue.length And: queue is empty when: frontIndex equals (backIndex + 1) % queue.length </li> <li> Slide 18 </li> <li> 18 Array-Based Implementation of a Queue An array-base queue: (a) initially; (b) after removing its front by incrementing frontIndex ; </li> <li> Slide 19 </li> <li> 19 Array-Based Implementation of a Queue An array-base queue: (c) after removing its front by setting queue[frontIndex] to null and then incrementing frontIndex. (Note: setting to null is not essential, but can be done just to be safe.) </li> <li> Slide 20 </li> <li> 20 Vector-Based Implementation of a Queue Maintain front of queue at beginning of vector Use addElement method to add entry at back Vector expands as necessary When remove front element, remaining elements move so new front is at beginning of vector Indexes at front and back not needed </li> <li> Slide 21 </li> <li> 21 Vector-Based Implementation of a Queue A vector that represents a queue. </li> <li> Slide 22 </li> <li> 22 A diagram of the classes WaitLine and Customer. Queue Use Example </li> <li> Slide 23 </li> <li> 23 Classes WaitLine and Customer A simulated waiting line continued </li> <li> Slide 24 </li> <li> 24 Classes WaitLine and Customer A simulated waiting line (continued) </li> <li> Slide 25 </li> <li> 25 Algorithm simulate(duration, arrivalProbability, maxServiceTime) serviceTimeLeft = 0 for (clock = 0; clock &lt; duration; clock++) { if (a new customer arrives) { numberOfArrivals++ serviceTime = a random time that does not exceed maxServiceTime nextArrival = a new customer containing clock, serviceTime, and a customer number that is numberOfArrivals line.enqueue(nextArrival) } if (serviceTimeLeft &gt; 0)// if a present customer is still being served serviceTimeLeft = serviceTimeLeft 1 else if (! line.isEmpty()) { nextCustomer = line.dequeue() serviceTimeLeft = nextCustomer.getServiceTime() 1 timeWaited = clock nextCustomer.getArrivalTime() totalTimeWaited = totalTimeWaited + timeWaited numberServed++ } //Implemented in Java class WaitLine </li> <li> Slide 26 </li> <li> 26 A Doubly Linked Implementation of a Deque Chain with head reference enables reference of first and then the rest of the nodes Tail reference allows reference of last node but not next-to-last We need nodes that can reference both Previous node Next node Thus the doubly linked chain </li> <li> Slide 27 </li> <li> 27 A Doubly Linked Implementation of a Deque A doubly linked chain with head and tail references </li> <li> Slide 28 </li> <li> 28 A Doubly Linked Implementation of a Deque Adding to the back of a non empty deque: (a) after the new node is allocated; (b) after the addition is complete. </li> <li> Slide 29 </li> <li> 29 A Doubly Linked Implementation of a Deque (a) a deque containing at least two entries; (b) after removing first node and obtaining reference to the deque's first entry. </li> <li> Slide 30 </li> <li> 30 Possible Implementations of a Priority Queue Two possible implementations of a priority queue using (a) an array; (b) a chain of linked nodes. </li> </ul>

Recommended

View more >