Queues Like Stacks Queues are a special type

  • Slides: 22
Download presentation
Queues

Queues

Like Stacks, Queues are a special type of List for storing collections of entities.

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. delete insert 23 Head of Queue 17 18 6 Tail of Queue

Insertions takes place at the tail of the Queue whereas deletions occur at the

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.

Since the item inserted first ( the first enque ) is the first item

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.

Priority Queues A refinement of the basic Queue class is the Priority Queue. In

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.

In computing terms, priority queues are essential to the well-being of a computer system.

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. insert f with priority 4 here head x p(6) y p(5) z p(3) tail

Using this method it should be trivial to see that items with the highest

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.

Unfortunately, Queues relying on a linked list implementation suffer from some minor problems, while

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).

By modifying the List class with an extra end pointer, the items at the

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.

A further complication with this approach concerns re-assigning the end pointer to the last

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 reassigning the end pointer takes n operations since we cannot traverse backwards through the list to find the previous node.

singly linked List first end 6. 5 delete last element 10. 7 4. 8

singly linked List first end 6. 5 delete last element 10. 7 4. 8 NULL

singly linked List first end 6. 5 10. 7 NULL Re-assignment of end pointer

singly linked List first end 6. 5 10. 7 NULL Re-assignment of end pointer requires list traversal.

When inserting at the end of a linked list , there is no node

When inserting at the end of a linked list , there is no node traversal required as we can re-assign the end pointer directly. first singly linked List 6. 5 end 10. 7 4. 8 inserted element 12. 3 NULL

singly linked List first 6. 5 end 10. 7 4. 8 inserted element 12.

singly linked List first 6. 5 end 10. 7 4. 8 inserted element 12. 3 NULL direct re-assignment of end pointer

In general, if a linked list is used to implement a Queue, insertions should

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.

Linked List Implementation of a Queue Class class Queue : public List { private:

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

Queue: : Queue() : List(), end(NULL) {} /* This code is written non-defensively. We

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; }

void Queue: : enqueue(Element e) { /* If the queue is empty, insert the

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->set. Next(n); /* and reset end */ end = n; } }

Queues using Array-based List Implementations Since we will be enqueing or dequeing elements at

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.

1. Enque at the end of the List and Deque at the start Enqueing

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.

2. Deque at the end of the List and Enque at the start Dequeing

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.

Whichever method is used for an array-based list implementation, the queuing operation at the

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