1 / 15

Queues

Queues. A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic Network packets Unlike a stack, both ends are used One for adding new elements One for removing elements

royce
Download Presentation

Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Queues • A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic Network packets • Unlike a stack, both ends are used • One for adding new elements • One for removing elements • The last element has to wait until all elements preceding it on the queue are removed • For this reason, it is a First in/First out (FIFO) structure

  2. Queues as ADTs • Data • A collection of elements of the same type • Operations • clear() • isEmpty() • enqueue(el) • dequeue() • firstEl()

  3. Queues as ADTs (cont’d)

  4. Queues as ADT’s (cont’d) • Implementation • With a doubly linked list, similar to stack, code in book

  5. Priority Queues • In many situations, FIFO scheduling must be overruled using some priority criteria • E.g., a handicapped person that arrives to a post office may have priority over others and may not have to wait in line • In a priority queue, elements are dequeued according to their priority and their current queue position

  6. Implementation of a priority queue • Linked list implementation of a priority queue • If list is ordered, adding an element is O(n), searching for highest/lowest-priority element is O(1) • Can also have list of list • If list is unordered, adding an element is O(1), searching for highest/lowest-priority element is O(n)

  7. Implementing the Stack, Queue As an ADT • there are different ways for the Stack Queue Implementation, the pros and cons? • Possible Ways of Implementation. • Dynamic array based implementation. • Dynamic Array (1) • Circular Array (2) • Linked list implementation • The implementation is like a linked list but with only the necessary methods (3) • Reuse a Linked List object (book) (4) • Inheritance from a Linked List

  8. Queue: Dynamic Array Implementation #pragma once #include <iostream> #include <cassert> template <class T> class Queue { public: Queue(size_t capacity = 1000); ~Queue(); bool IsEmpty() const; bool IsFull() const; // Add a value to the end of the Queue. void Enqueue(const T& value); // Remove and return value at front of Queue. T Dequeue(); // Retrieve value at front of Queue without removing it. T Front() const;

  9. Queue.h // Display Queue contents. void Display(std::ostream& out) const; void Clear(); private: T* data; int size; int head; int tail; };

  10. Dynamic Array Implementation(1) • When new jobs enter queue, may run out of space. • The array may not really be full though, if items have been removed from queue. Possible Solution: When tail = (maxSize – 1) attempt to shift data forwards into empty spaces and then do Add.

  11. Worst Case Queue Shift • Worst Case: • Shift entire queue: Cost of O(n-1) • Do every time perform an add • Too expensive to be useful Worst case is not that unlikely, so this suggests finding an alternative implementation.

  12. ‘Circular’ Array Implementation (2) Basic Idea: Allow the queue to wrap-around Implement with addition mod size: tail = (tail + 1) % queueSize; 4 4 J4 3 3 J3 J2 J1 2 2 N-2 J1 J2 N-2 J3 1 N-1 1 N-1 0 0

  13. Linked Queues (3) Class QueueNode{ public: QueueNode(int d, QueueNode * l); int data; QueueNode *link; }; class Queue { public: Queue(); ~Queue(); void enqueue(const int); int* dequeue(int&); bool isEmpty(); private: QueueNode* head; QueueNode* tail; void QueueEmpty(); } Implementation is similar to linked-list, addToTail, deleteFromHead

  14. Queues as ADT’s (4) • Reuse a doubly linked list, similar to stack, code in book class Queue { public: Queue(); ~Queue(); void enqueue(const int el) {lst.addToTail(el);} int* dequeue(int&); bool isEmpty(); private: DLList lst; }

  15. Queue ADT Implementations • No matter how queue is implemented, -different internal representation, - different implementation of the methods • the users that use the Queue class will see the same functionalities • However, the efficiency of different operation might be different • Analyze them. What about priority queue?

More Related