{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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).
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern