# queue, deque, and priority queue implementations

Post on 21-Dec-2015

216 views

Embed Size (px)

TRANSCRIPT

- Slide 1
- Queue, Deque, and Priority Queue Implementations
- Slide 2
- 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(); }
- Slide 3
- 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(); }
- Slide 4
- 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
- Slide 5
- 5 A Linked Implementation of a Queue A chain of linked nodes that implements a queue. Front of queue Back of queue
- Slide 6
- 6 A Linked Implementation of a Queue (a)Before adding a new node to an empty chain; (b)after adding to it.
- Slide 7
- 7 A Linked Implementation of a Queue (a)Before adding a new node to the end of a chain; (b)after adding it.
- Slide 8
- 8 A Linked Implementation of a Queue (a) A queue of more than one entry; (b) after removing the queue's front.
- Slide 9
- 9 A Linked Implementation of a Queue (a) A queue of one entry; (b) after removing the queue's front.
- Slide 10
- 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
- Slide 11
- 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;
- Slide 12
- 12 Array-Based Implementation of a Queue An array that represents a queue without shifting its entries: (c) after several more additions & removals; (d) after two additions that wrap around to the beginning of the array
- Slide 13
- 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
- Slide 14
- 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;
- Slide 15
- 15 A Circular Array A circular array that represents a queue: (d) after removing all but one entry; (e) after removing remaining entry.
- Slide 16
- 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
- Slide 17
- 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
- Slide 18
- 18 Array-Based Implementation of a Queue An array-base queue: (a) initially; (b) after removing its front by incrementing frontIndex ;
- Slide 19
- 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.)
- Slide 20
- 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
- Slide 21
- 21 Vector-Based Implementation of a Queue A vector that represents a queue.
- Slide 22
- 22 A diagram of the classes WaitLine and Customer. Queue Use Example
- Slide 23
- 23 Classes WaitLine and Customer A simulated waiting line continued
- Slide 24
- 24 Classes WaitLine and Customer A simulated waiting line (continued)
- Slide 25
- 25 Algorithm simulate(duration, arrivalProbability, maxServiceTime) serviceTimeLeft = 0 for (clock = 0; clock < 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 > 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
- Slide 26
- 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
- Slide 27
- 27 A Doubly Linked Implementation of a Deque A doubly linked chain with head and tail references
- Slide 28
- 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.
- Slide 29
- 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.
- Slide 30
- 30 Possible Implementations of a Priority Queue Two possible implementations of a priority queue using (a) an array; (b) a chain of linked nodes.

Recommended

View more >