queue, deque, and priority queue implementations
TRANSCRIPT
Queue, Deque, and Priority Queue Implementations
2
Specifications for the ADT Queue
Interface for a queue of objectspublic interface queueInterface {
//add a new entry to the back of the queuepublic void enqueue(Object newEntry);
//remove and return the front of the queuepublic Object dequeue();
//retrieve the front of the queuepublic Object getFront();
//determine whether the queue is emptypublic boolean isEmpty();
//remove all entries from the queuepublic void clear();
}
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();
}
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
5
A Linked Implementation of a Queue
A chain of linked nodes that implements a queue.
Front of queue Back of queue
6
A Linked Implementation of a Queue
(a) Before adding a new node to an empty chain;
(b) after adding to it.
7
A Linked Implementation of a Queue
(a) Before adding a new node to the end of a chain;
(b) after adding it.
8
A Linked Implementation of a Queue
(a) A queue of more than one entry; (b) after removing the queue's front.
9
A Linked Implementation of a Queue
(a) A queue of one entry; (b) after removing the queue's front.
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
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;
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
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 indicesenqueue: backIndex =(backIndex+1)% queue.length
dequeue: frontIndex =(frontIndex+1)% queue.length
Note: with circular arrayfrontIndex =(backIndex+1)% queue.length
both when queue is empty and when full
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;
15
A Circular Array
A circular array that represents a queue: (d) after removing all but one entry; (e) after removing remaining entry.
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
Allows us to distinguish between
empty and full queue
Note initial values…
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
18
Array-Based Implementation of a Queue
An array-base queue: (a) initially; (b) after removing its front by incrementing frontIndex;
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.)
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
21
Vector-Based Implementation of a Queue
A vector that represents a queue.
22A diagram of the classes WaitLine and Customer.
Queue Use Example
23
Classes WaitLine and Customer
A simulated waiting line … continued →
24
Classes WaitLine and Customer
A simulated waiting line (continued)
25
Algorithm simulate(duration, arrivalProbability, maxServiceTime) serviceTimeLeft = 0for (clock = 0; clock < duration; clock++) {
if (a new customer arrives) {numberOfArrivals++serviceTime = a random time that does not exceed maxServiceTimenextArrival = a new customer containing clock, serviceTime, and a customer
number that is numberOfArrivalsline.enqueue(nextArrival)
}
if (serviceTimeLeft > 0) // if a present customer is still being servedserviceTimeLeft = serviceTimeLeft – 1
else if (! line.isEmpty()) {nextCustomer = line.dequeue()serviceTimeLeft = nextCustomer.getServiceTime() – 1timeWaited = clock – nextCustomer.getArrivalTime()totalTimeWaited = totalTimeWaited + timeWaitednumberServed++
}}
//Implemented in Java class WaitLine…
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
27
A Doubly Linked Implementation of a Deque
A doubly linked chain with head and tail references
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.
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.
30
Possible Implementations of a Priority Queue
Two possible implementations of a priority queue using (a) an array; (b) a chain of linked nodes.