module2 - Stacks and Queues Stack A stack is an ADT...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Stacks and Queues Stack A stack is an ADT consisting of a collection of items with operations Push and Pop . Items are removed in LIFO ( last-in first-out ) order. Queue A queue is an ADT consisting of a collection of items with operations Enqueue and Dequeue . Items are removed in FIFO ( first-in first-out ) order. 47 / 63 Priority Queues Priority Queue A priority queue is an ADT consisting of a collection of items with operations Insert and DeleteMax . Items are removed according to their priority , which (typically) is a positive integer. That is, at any given time, only the item of highest priority can be removed form the queue. The operation ConstructQueue builds a priority queue from a specified collection of items. Other possible operations include InitializeEmptyQueue , IsEmpty , and SizeOfQueue . The above definition is for a maximum-oriented priority queue. A minimum-oriented priority queue is defined in the natural way, by replacing the operation DeleteMax by DeleteMin . 48 / 63 Observations The operation ConstructQueue ( PQ ) can be implemented by performing InitializeEmptyQueue ( PQ ) followed by n Insert operations. IsEmpty ( PQ ) = true if and only if SizeOfQueue ( PQ ) = 0. Any priority queue can be used to sort. First, build a priority queue of n items, and then perform n DeleteMax operations, storing the results in an array. The array is sorted in decreasing order. To sort in increasing order, fill the array from back to front when performing the n DeleteMax operations. Alternatively, use a minimum-oriented priority queue. 49 / 63 First Implementation: Unsorted Array Using an unsorted array (or an unsorted linked list), priority queue operations have the following complexities: InitializeEmptyQueue , IsEmpty , and SizeOfQueue : Θ(1)....
View Full Document

This note was uploaded on 02/21/2012 for the course PSYCH 101 taught by Professor Ennis during the Winter '09 term at Waterloo.

Page1 / 17

module2 - Stacks and Queues Stack A stack is an ADT...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online