{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

module2

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

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

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

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

View Full Document
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

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

View Full Document
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).
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern