150 likes | 286 Views
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
E N D
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
Queues as ADTs • Data • A collection of elements of the same type • Operations • clear() • isEmpty() • enqueue(el) • dequeue() • firstEl()
Queues as ADT’s (cont’d) • Implementation • With a doubly linked list, similar to stack, code in book
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
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)
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
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;
Queue.h // Display Queue contents. void Display(std::ostream& out) const; void Clear(); private: T* data; int size; int head; int tail; };
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.
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.
‘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
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
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; }
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?