Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queues. Like Stacks, Queues are a special type of List for storing collections of entities. Stacks are Lists where insertions (pushes) and deletions (pops)

Similar presentations


Presentation on theme: "Queues. Like Stacks, Queues are a special type of List for storing collections of entities. Stacks are Lists where insertions (pushes) and deletions (pops)"— Presentation transcript:

1 Queues

2 Like Stacks, Queues are a special type of List for storing collections of entities. Stacks are Lists where insertions (pushes) and deletions (pops) occur at one end of the list. Queues are Lists in which the insertion and deletion occur at different ends of the list. 2317186 Head of QueueTail of Queue insertdelete

3 Insertions takes place at the tail of the Queue whereas deletions occur at the head of the Queue. Insertion into a Queue is called enqueing while deletion is called dequeing. When deleting an element from the head of the Queue, the element is retrieved and output from the deque() operation before it is deleted.

4 Since the item inserted first ( the first enque ) is the first item deleted then a Queue is known as a First-In-First-Out (FIFO) List. Queuing Applications There are many applications of Queues in Computer Science just as in real life: An electronic air traffic system will place approaching airplanes in a landing queue. A network printer will queue print jobs in the order they are received from the clients.

5 Priority Queues A refinement of the basic Queue class is the Priority Queue. In many situations, items being queued have some priority associated with them. For example, in an A&E ward patients with more severe conditions are treated before patients with less severe conditions, even though those patients may have been in the queue before them.

6 In computing terms, priority queues are essential to the well-being of a computer system. An operating system will handle requests from system administrators or troubled processes before normal tasks enqued before them because they have higher priorities. Enqueing in a Priority Queue involves traversing the Queue starting at the tail until an element is found with the same or higher priority. The new item is then queued after this item in the Queue. x p(6) y p(5) z p(3) insert f with priority 4 here tail head

7 Using this method it should be trivial to see that items with the highest priority collect at the head of the Queue. For items with the same priority, the FIFO rule applies. Queue Implementations Since a Queue is a special case of the List ADT, it seems logical to use the properties of a List to construct a Queue.

8 Unfortunately, Queues relying on a linked list implementation suffer from some minor problems, while queues represented by array-based lists fair even worse. Linked-List Implementations The main problem associated with simple linked-list implementations of Queues is that only one pointer is maintained (to the first node in the list).

9 By modifying the List class with an extra end pointer, the items at the end of the queue can be accessed in one operation, independent of the size of the list.

10 A further complication with this approach concerns re-assigning the end pointer to the last node in the list after a delete operation is completed for a singly linked list. If an item is deleted from the end of a singly linked list, then re- assigning the end pointer takes n operations since we cannot traverse backwards through the list to find the previous node.

11 6.5 first 10.7 4.8 NULL end singly linked List delete last element

12 6.5 10.7 first end NULL Re-assignment of end pointer requires list traversal. singly linked List

13 When inserting at the end of a linked list, there is no node traversal required as we can re-assign the end pointer directly. 6.5 first NULL end singly linked List inserted element 10.7 4.8 12.3

14 6.5 first NULL end singly linked List inserted element 10.7 4.8 12.3 direct re-assignment of end pointer

15 In general, if a linked list is used to implement a Queue, insertions should take place at the end of the list and deletions at the start of the list. In other words, the head of the Queue must be the start of the List, while the tail of the Queue is the end of the List.

16 Linked List Implementation of a Queue Class class Queue : public List { private: Node* end; public: Queue(); void enqueue(Element); void dequeue(Element); }

17 Queue::Queue() : List(), end(NULL) {} /* This code is written non-defensively. We should first check whether the Queue is not empty */ Element Queue::dequeue() { Element e = retrieve(0); delete(e); return e; }

18 void Queue::enqueue(Element e) { /* If the queue is empty, insert the element at the front and make the end pointer point to it */ if (end == NULL) { insert(e,0); /* The following requires that first pointer has become a protected member in the List class */ end = first; } else { /* First make a new node */ Node* n = new Node(e); /* Now attach it to the end of the Queue */ end->setNext(n); /* and reset end */ end = n; } }

19 Queues using Array-based List Implementations Since we will be enqueing or dequeing elements at the end of a list only array-based lists with an end-pointer will be considered. Once again there are two choices when using an array-based list: 1. Enque at the end of the List and Deque at the start; 2. Deque at the end of the List and Enque at the start.

20 1. Enque at the end of the List and Deque at the start Enqueing at the end of the List takes one operation. We move to the end of the list using the end pointer, insert a new item and re-assign the end pointer. Unfortunately, dequeing at the start of the list now takes n operations. When the element is deleted all subsequent elements need to be shifted towards the beginning of the array.

21 2. Deque at the end of the List and Enque at the start Dequeing at the end of the List takes one operation. We move to the end of the list using the end pointer, delete the element and re-assign the end pointer to the previous array element. Unfortunately, enqueing at the start of the list now takes n operations. When a new element is inserted all subsequent elements need to be shifted up by one position.

22 Whichever method is used for an array-based list implementation, the queuing operation at the start of the list will take n operations.


Download ppt "Queues. Like Stacks, Queues are a special type of List for storing collections of entities. Stacks are Lists where insertions (pushes) and deletions (pops)"

Similar presentations


Ads by Google