Handout 3.1 - Collections-The Queue ADT.pdf

Handout 3.1 - Collections-The Queue ADT.pdf

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

View Full Document Right Arrow Icon
Kimani Njoroge. Collection: Queues Page 1 The Queue ADT Consider the Queue ADT. A queue is an abstract data type that stores elements in a first-in-first-out order. Elements are added at one end and removed from the other. Hence, the first element to be added to the queue is the first element to be taken out of the queue. This is analogous to a line-up that we see every day. The first person in line is the first person served (i.e., first-come-first-served). When people arrive, they go to the back of the line. People get served from the front of the line first. Therefore, with a queue, we add to the back and remove from the front . We are not allowed to insert or remove elements from the middle of the queue. Why is this restriction a good idea? Well, depending on how the queue is implemented, it can be more efficient (i.e., faster) to insert and remove elements since we know that all such changes will occur at the front or back of the queue. Removing from the front may then simply require moving the “front-of-the-line pointer” instead of shifting elements over. Also, adding to the back may require extending the “back-of-the-line pointer”. Typical methods for Queues are: add(Object x) Insert object x at the end of the queue. This operation is sometimes called push(x) or enqueue(x). e.g., aQueue.add(x) will do this remove() Remove and return the object at the front of the queue. The next item in the queue becomes the front item. This operation is sometimes called pop() or dequeue(). e.g., x = aQueue.remove() will return 23 peek() Return (but do not remove) the object at the front of the queue. This operation is sometimes called front(). e.g., x = aQueue.peek() will do this size(), isEmpty() and clear() Does the same as with lists Another more specialized type of queue is the PriorityQueue : A PriorityQueue is a queue in which the elements also maintain a priority. That is, we still add to the back of the queue and remove from 23 15 9 12 37 83 23 15 12 37 83 x 0 1 2 3 n-2 n-1 n-2 n-1 0 1 2 3 Before After 9 x n After 15 9 23 12 37 83 0 1 2 3 n-2 n-1 n-1 0 1 2 n-2 Before 23 15 9 12 37 83 23 0 1 2 3 n-2 n-1 23 15 9 12 37 83
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
Kimani Njoroge. Collection: Queues Page 2 the front, but elements with higher priority are automatically shifted closer to the front before lower priority elements. As a real life example, when we go to the hospital for an “emergency”, we wait in line (6 to 8 hours typically). We normally get served in the order that we came in at. However, if someone comes in after us who is bleeding or unconscious, they automatically get bumped up ahead of us since their injuries are likely more serious and demand immediate attention. We may think of a PriorityQueue as a sorted queue. The PriorityQueue is used in the same way as a regular Queue, except that we must ensure that each element added to the queue is given a priority. Hence, we are sometimes required to specify the priority of an item when we add it to the queue: add(int priority, Object x)
Image of page 2
Image of page 3
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