Date post: | 02-Jan-2016 |

Category: | ## Documents |

View: | 35 times |

Download: | 2 times |

Share this document with a friend

Description:

Queue, Deque, and Priority Queue Implementations. Chapter 23. Chapter Contents. A Linked List Implementation of a Queue An Array-Based Implementation of a Queue A Circular Array A Circular Array with One Unused Location A Vector-Based Implementation of a Queue - PowerPoint PPT Presentation

Transcript:

Queue, Deque, and Priority Queue ImplementationsChapter 23

Chapter ContentsA Linked List Implementation of a QueueAn Array-Based Implementation of a QueueA Circular ArrayA Circular Array with One Unused LocationA Vector-Based Implementation of a QueueCircular Linked Implementations of a QueueA Two-Part Circular Linked ChainA Doubly Linked Implementation of a QueuePossible Implementations of a Priority Queue

A Linked Implementation of a QueueUse chain of linked nodes for the queueTwo ends at opposite ends of chainAccessing last node inefficientCould keep a reference to the tail of the chainPlace front of queue at beginning of chainPlace back of queue at end of chainWith references to both

A Linked Implementation of a QueueFig. 23-1 A chain of linked nodes that implements a queue.Front of queueBack of queue

A Linked Implementation of a QueueFig. 23-2 (a) Before adding a new node to an empty chain; (b) after adding to it.

A Linked Implementation of a QueueFig. 23-3 (a) Before adding a new node to the end of a chain; (b) after adding it.

A Linked Implementation of a QueueFig. 23-4 (a) A queue of more than one entry; (b) after removing the queue's front.

A Linked Implementation of a QueueFig. 23-5 (a) A queue of one entry; (b) after removing the queue's front.

Array-Based Implementation of a QueueLet queue[0] be the frontfrontIndex, backIndex are indices of front and backIf we insist queue[0] is frontMust shift entries when we remove the frontInstead move frontIndexProblem then is array can become fullBut now beginning of array could be empty and available for use

Array-Based Implementation of a QueueFig. 23-6 An array that represents a queue without shifting its entries: (a) initially; (b) after removing the front twice;

Array-Based Implementation of a QueueFig. 23-6 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

A Circular ArrayWhen queue reaches end of arrayAdd subsequent entries to beginningArray behaves as though it were circularFirst location follows last oneUse modulo arithmetic on indices backIndex = (backIndex + 1) % queue.lengthNote: with circular array frontIndex == backIndex + 1 both when queue is empty and when full

A Circular ArrayFig. 23-7 A circular array that represents a queue: (a) when full; (b) after removing 2 entries; (c) after removing 3 more entries;

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

A Circular Array with One Unused LocationFig. 23-8 A seven-location circular array that contains at most six entries of a queue continued Allows us to distinguish between empty and full queue

A Circular Array with One Unused LocationFig. 23-8 (ctd.) A seven-location circular array that contains at most six entries of a queue.

Array-Based Implementation of a QueueFig. 23-9 An array-base queue: (a) initially; (b) after removing its front by incrementing frontIndex;

Array-Based Implementation of a QueueFig. 23-9 An array-base queue: (c) after removing its front by setting queue[frontIndex] to null and then incrementing frontIndex.

Vector-Based Implementation of a QueueMaintain front of queue at beginning of vectorUse addElement method to add entry at backVector expands as necessaryWhen remove front element, remaining elements move so new front is at beginning of vectorIndexes at front and back not needed

Vector-Based Implementation of a QueueFig. 23-10 A vector that represents a queue.

Circular Linked Implementations of a QueueLast node references first nodeNow we have a single reference to last node And still locate first node quicklyNo node contains a nullWhen a class uses circular linked chain for queueOnly one data item in the classThe reference to the chain's last node

Circular Linked Implementations of a QueueFig. 23-11 A circular linked chain with an external reference to its last node that (a) has more than one node; (b) has one node; (c) is empty.

A Two-Part Linked ChainLinked nodes that form the queue followed by linked nodes available for use in the queuequeueNode references front of queue nodefreeNode references first available node following end of queueIn essence we have two chainsOne for the queueOne for available nodesAll joined in a circle

A Two-Part Linked ChainFig. 32-12 A two-part circular linked chain that represents both a queue and the nodes available to the queue.

A Two-Part Linked ChainFig. 32-13 A two-part circular linked chain that represents a queue: (a) when it is empty; (b) after adding one entry; (c) after adding three more entries.

A Two-Part Linked ChainFig. 32-13 A two-part circular linked chain that represents a queue: (d) after removing the front; (e) after adding one more entry

A Two-Part Linked ChainFig. 32-14 A chain that requires a new node for an addition to a queue: (a) before the addition; (b) after the addition.

A Two-Part Linked ChainFig. 32-15 A chain with a node available for an addition to a queue: (a) before the addition; (b) after the addition.

A Doubly Linked Implementation of a DequeChain with head reference enables reference of first and then the rest of the nodesTail reference allows reference of last node but not next-to-lastWe need nodes that can reference bothPrevious nodeNext nodeThus the doubly linked chain

A Doubly Linked Implementation of a DequeFig. 23-16 A doubly linked chain with head and tail references

A Doubly Linked Implementation of a DequeFig. 23-17 Adding to the back of a non empty deque: (a) after the new node is allocated; (b) after the addition is complete.

A Doubly Linked Implementation of a DequeFig. 23-18 (a) a deque containing at least two entries; (b) after removing first node and obtaining reference to the deque's first entry.

Possible Implementations of a Priority QueueFig. 23-19 Two possible implementations of a priority queue using (a) an array; (b) a chain of linked nodes.

of 33

Embed Size (px)

Recommended