22
Queues Queues

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

Embed Size (px)

Citation preview

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

QueuesQueues

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

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

Stacks are Lists where insertions (Stacks are Lists where insertions (pushespushes) and deletions () and deletions (popspops) ) occur at one end of the list.occur at one end of the list.

Queues are Lists in which the insertion and deletion occur at Queues are Lists in which the insertion and deletion occur at differentdifferent ends of the list. ends of the list.

23 17 18 6

Head of Queue Tail of Queue

insertdelete

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

Insertions takes place at the Insertions takes place at the tailtail of the Queue whereas deletions of the Queue whereas deletions occur at the occur at the headhead of the Queue. of the Queue.

Insertion into a Queue is called Insertion into a Queue is called enqueingenqueing while deletion is called while deletion is called dequeingdequeing..

When deleting an element from the head of the Queue, the element When deleting an element from the head of the Queue, the element is retrieved and output from the deque() operation before it is is retrieved and output from the deque() operation before it is deleted. deleted.

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

Since the item inserted first ( Since the item inserted first ( the first enquethe first enque ) is the first item ) is the first item deleted then a Queue is known as a deleted then a Queue is known as a First-In-First-OutFirst-In-First-Out ( (FIFOFIFO) ) List.List.

Queuing ApplicationsQueuing Applications

There are many applications of Queues in Computer Science just as There are many applications of Queues in Computer Science just as in real life:in real life:

An electronic air traffic system will place approaching airplanes in An electronic air traffic system will place approaching airplanes in a landing queue.a landing queue.

A network printer will queue print jobs in the order they are A network printer will queue print jobs in the order they are received from the clients.received from the clients.

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

Priority QueuesPriority Queues

A refinement of the basic Queue class is the A refinement of the basic Queue class is the Priority QueuePriority Queue. .

In many situations, items being queued have some priority In many situations, items being queued have some priority associated with them. associated with them.

For example, in an A&E ward patients with more severe conditions For example, in an A&E ward patients with more severe conditions are treated before patients with less severe conditions, even are treated before patients with less severe conditions, even though those patients may have been in the queue before them.though those patients may have been in the queue before them.

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

In computing terms, priority queues are essential to the well-being In computing terms, priority queues are essential to the well-being of a computer system. An operating system will handle of a computer system. An operating system will handle requests from system administrators or troubled processes requests from system administrators or troubled processes before normal tasks enqued before them because they have before normal tasks enqued before them because they have higher priorities.higher priorities.

Enqueing in a Priority Queue involves traversing the Queue Enqueing in a Priority Queue involves traversing the Queue starting at the tail until an element is found with the same or 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 higher priority. The new item is then queued after this item in the Queue.the Queue.

x p(6) y p(5) z p(3)

insert f with priority 4 here

tailhead

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

Using this method it should be trivial to see that items with the Using this method it should be trivial to see that items with the highest priority collect at the head of the Queue.highest priority collect at the head of the Queue.

For items with the same priority, the For items with the same priority, the FIFOFIFO rule applies. rule applies.

Queue ImplementationsQueue Implementations

Since a Queue is a special case of the List ADT, it seems logical to 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.use the properties of a List to construct a Queue.

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

Unfortunately, Queues relying on a linked list implementation Unfortunately, Queues relying on a linked list implementation suffer from some minor problems, while queues represented by suffer from some minor problems, while queues represented by array-based lists fair even worse.array-based lists fair even worse.

Linked-List ImplementationsLinked-List Implementations

The main problem associated with simple linked-list The main problem associated with simple linked-list implementations of Queues is that only one pointer is implementations of Queues is that only one pointer is maintained (maintained (to the first node in the listto the first node in the list).).

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

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

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

A further complication with this approach concerns re-assigning A further complication with this approach concerns re-assigning the end pointer to the last node in the list after a delete the end pointer to the last node in the list after a delete operation is completed for a singly linked list.operation is completed for a singly linked list.

If an item is deleted from the end of a singly linked list, then re-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 assigning the end pointer takes n operations since we cannot traverse backwards through the list to find the previous node.traverse backwards through the list to find the previous node.

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

6.5

first

10.7

4.8NULL

end

singly linked List

delete last element

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

6.5

10.7

first

end

NULL

Re-assignment of end pointer requires list traversal.

singly linked List

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

When inserting at the end of a linked list , there is no node traversal When inserting at the end of a linked list , there is no node traversal required as we can re-assign the end pointer directly.required as we can re-assign the end pointer directly.

6.5

first

NULL

endsingly linked List

inserted element

10.7

4.8

12.3

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

6.5

first

NULL

end

singly linked List

inserted element

10.7

4.8

12.3

direct re-assignment of end pointer

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

In general, if a linked list is used to implement a Queue, insertions 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 should take place at the end of the list and deletions at the start of the list.of the list.

In other words, the In other words, the headhead of the Queue must be the of the Queue must be the startstart of the List, of the List, while the while the tailtail of the Queue is the of the Queue is the endend of the List. of the List.

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

Linked List Implementation of a Queue ClassLinked List Implementation of a Queue Class

class Queue : public Listclass Queue : public List{{    private:    private:        Node* end;        Node* end;    public:    public:        Queue();        Queue();        void enqueue(Element);        void enqueue(Element);        void dequeue(Element);        void dequeue(Element);}}

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

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

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

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

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

Queues using Array-based List ImplementationsQueues using Array-based List Implementations

Since we will be enqueing or dequeing elements at the end of a list 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.only array-based lists with an end-pointer will be considered.

Once again there are two choices when using an array-based list:Once again there are two choices when using an array-based list:

1.1. Enque at the end of the List and Deque at the start;Enque at the end of the List and Deque at the start;

2.2. Deque at the end of the List and Enque at the start. Deque at the end of the List and Enque at the start.

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

1.1. Enque at the end of the List and Deque at the startEnque at the end of the List and Deque at the start

Enqueing at the end of the List takes one operation.Enqueing at the end of the List takes one operation. We move We move to the end of the list using the end pointer, insert a new item to the end of the list using the end pointer, insert a new item and re-assign the end pointer. and re-assign the end pointer.

Unfortunately, dequeing at the start of the list now takes n Unfortunately, dequeing at the start of the list now takes n operations. When the element is deleted all subsequent operations. When the element is deleted all subsequent elements need to be shifted towards the beginning of the array. elements need to be shifted towards the beginning of the array.

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

2.2. Deque at the end of the List and Enque at the startDeque at the end of the List and Enque at the start

Dequeing at the end of the List takes one operation.Dequeing at the end of the List takes one operation. We move We move to the end of the list using the end pointer, delete the element to the end of the list using the end pointer, delete the element and re-assign the end pointer to the previous array element. and re-assign the end pointer to the previous array element.

Unfortunately, enqueing at the start of the list now takes n Unfortunately, enqueing at the start of the list now takes n operations. When a new element is inserted all subsequent operations. When a new element is inserted all subsequent elements need to be shifted up by one position. elements need to be shifted up by one position.

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

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