+ All Categories
Transcript
Page 1: Queue, Deque, and Priority Queue Implementations.

Queue, Deque, and Priority Queue Implementations

Page 2: 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();

}

Page 3: Queue, Deque, and Priority Queue Implementations.

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

}

Page 4: Queue, Deque, and Priority Queue Implementations.

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

Page 5: Queue, Deque, and Priority Queue Implementations.

5

A Linked Implementation of a Queue

A chain of linked nodes that implements a queue.

Front of queue Back of queue

Page 6: Queue, Deque, and Priority Queue Implementations.

6

A Linked Implementation of a Queue

(a) Before adding a new node to an empty chain;

(b) after adding to it.

Page 7: Queue, Deque, and Priority Queue Implementations.

7

A Linked Implementation of a Queue

(a) Before adding a new node to the end of a chain;

(b) after adding it.

Page 8: Queue, Deque, and Priority Queue Implementations.

8

A Linked Implementation of a Queue

(a) A queue of more than one entry; (b) after removing the queue's front.

Page 9: Queue, Deque, and Priority Queue Implementations.

9

A Linked Implementation of a Queue

(a) A queue of one entry; (b) after removing the queue's front.

Page 10: Queue, Deque, and Priority Queue Implementations.

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

Page 11: Queue, Deque, and Priority Queue Implementations.

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;

Page 12: Queue, Deque, and Priority Queue Implementations.

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

Page 13: Queue, Deque, and Priority Queue Implementations.

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

Page 14: Queue, Deque, and Priority Queue Implementations.

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;

Page 15: Queue, Deque, and Priority Queue Implementations.

15

A Circular Array

A circular array that represents a queue: (d) after removing all but one entry; (e) after removing remaining entry.

Page 16: Queue, Deque, and Priority Queue Implementations.

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…

Page 17: Queue, Deque, and Priority Queue Implementations.

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

Page 18: Queue, Deque, and Priority Queue Implementations.

18

Array-Based Implementation of a Queue

An array-base queue: (a) initially; (b) after removing its front by incrementing frontIndex;

Page 19: Queue, Deque, and Priority Queue Implementations.

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.)

Page 20: Queue, Deque, and Priority Queue Implementations.

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

Page 21: Queue, Deque, and Priority Queue Implementations.

21

Vector-Based Implementation of a Queue

A vector that represents a queue.

Page 22: Queue, Deque, and Priority Queue Implementations.

22A diagram of the classes WaitLine and Customer.

Queue Use Example

Page 23: Queue, Deque, and Priority Queue Implementations.

23

Classes WaitLine and Customer

A simulated waiting line … continued →

Page 24: Queue, Deque, and Priority Queue Implementations.

24

Classes WaitLine and Customer

A simulated waiting line (continued)

Page 25: Queue, Deque, and Priority Queue Implementations.

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…

Page 26: Queue, Deque, and Priority Queue Implementations.

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

Page 27: Queue, Deque, and Priority Queue Implementations.

27

A Doubly Linked Implementation of a Deque

A doubly linked chain with head and tail references

Page 28: Queue, Deque, and Priority Queue Implementations.

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.

Page 29: Queue, Deque, and Priority Queue Implementations.

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.

Page 30: Queue, Deque, and Priority Queue Implementations.

30

Possible Implementations of a Priority Queue

Two possible implementations of a priority queue using (a) an array; (b) a chain of linked nodes.


Top Related