Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queues Queues Queues.

Similar presentations


Presentation on theme: "Queues Queues Queues."— Presentation transcript:

1 Queues Queues Queues

2 The Queue Operations A queue is like a line of people waiting for a bank teller. The queue has a front and a rear. $ $ When you think of a computer science queue, you can imagine a line of people waiting for a teller in a bank. The line has a front (the next person to be served) and a rear (the last person to arrive. Front Rear Queues

3 The Queue Operations New people must enter the queue at the rear. This is called an enqueue operation. $ $ Don’t ask me why the C++ STL used the name push. It only confuses matters with a stack. In any case, when a new item enters a queue, it does so at the rear. Front Rear Queues

4 The Queue Operations When an item is taken from the queue, it always comes from the front. This is called a dequeue operation. $ $ When an item is removed from a queue, the removal occurs at the front. Front Rear Queues

5 Applications of Queues
Direct applications Waiting lists Access to shared resources (e.g., printer) Multiprogramming Indirect applications Auxiliary data structure for algorithms Component of other data structures Queues

6 Array Implementation A queue can be implemented with an array, as shown here. For example, this queue contains the integers 4 (at the front), 8 and 6 (at the rear). [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . An array of integers to implement a queue of integers 4 8 6 We don't care what's in this part of the array. Just like our stack implementation in the previous chapter, one way to implement a queue is to store the elements in an array. Queues

7 Array Implementation The easiest implementation also keeps track of the number of items in the queue, the index of the first element (at the front of the queue), the last element (at the rear). [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 4 8 6 size 3 front rear 2 The easiest implementation also keeps track of three numbers. The size could be as small as zero or as large as the number of items in the array. The index of the front element is stored in the first member variable. The front item in the queue is at that index of the array. The next item is after the first one and so on until the rear of the queue that occurs at the index stored in a member variable called last. Queues

8 A Dequeue Operation When an element leaves the queue, size is decremented, and first changes, too. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 4 8 6 size 2 front 1 rear This shows how the member variables change when an item leaves the queue. Queues

9 An Enqueue Operation When an element enters the queue, size is incremented, and last changes, too. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 2 8 6 size 3 front 1 rear And this shows how the member variables change when a new item enters the queue. For a fixed size array, a new item may enter only if the current size of the queue is less than the size of the array. For a dynamic array, we could increase the size of the array when the queue grows beyond the current array size. Queues

10 At the End of the Array There is special behavior at the end of the array. For example, suppose we want to add a new element to this queue, where the last index is [5]: [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 2 1 6 size 3 front rear 5 An array implementation of a queue must have special behavior when the rear of the queue reaches the end of the array. In this example, suppose we want to add the number 4 to the queue. We can do so… Queues

11 At the End of the Array The new element goes at the front of the array (if that slot isn’t already used): [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 2 1 6 size 4 first 3 last …by putting it at location 0 (if that location is not already used). Queues

12 Array Implementation Easy to implement
But it has a limited capacity with a fixed array Or you must use a dynamic array for an unbounded capacity Special behavior is needed when the rear reaches the end of the array. size 3 front rear 2 Here are some of the key aspects of an array implementation of a queue. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 4 8 6 Queues

13 The Queue Abstract Data Type (ADT)
Queues The Queue Abstract Data Type (ADT) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front of the queue Main queue operations: enqueue(object): inserts an element at the end of the queue object dequeue(): removes and returns the element at the front of the queue Auxiliary queue operations: object front(): returns the element at the front without removing it integer size(): returns the number of elements stored boolean isEmpty(): indicates whether no elements are stored Exceptions Attempting the execution of dequeue or front on an empty queue throws an EmptyQueueException Queues

14 Example Operation Output Q enqueue(5) enqueue(3) dequeue() enqueue(7)
front() isEmpty() enqueue(9) size() Queues

15 Example Operation Output Q enqueue(5) – (5) enqueue(3) – (5, 3)
dequeue() 5 (3) enqueue(7) – (3, 7) dequeue() 3 (7) front() 7 (7) dequeue() 7 () dequeue() “error” () isEmpty() true () enqueue(9) – (9) enqueue(7) – (9, 7) size() 2 (9, 7) enqueue(3) – (9, 7, 3) enqueue(5) – (9, 7, 3, 5) dequeue() 9 (7, 3, 5) Queues

16 wrapped-around configuration
Array-based Queue Use an array of size N in a circular fashion Two variables keep track of the front and rear f index of the front element r with arrays, index immediately past the rear element Array location r is kept empty normal configuration Q 1 2 r f wrapped-around configuration Q 1 2 f r Queues

17 Queue Operations We use the modulo operator (remainder of division)
Algorithm size() return (N - f + r) mod N Algorithm isEmpty() return (f = r) Q 1 2 r f Q 1 2 f r Queues

18 Queue Operations (cont.)
Algorithm enqueue(o) if size() = N  1 then throw FullQueueException else Q[r]  o r  (r + 1) mod N Operation enqueue throws an exception if the array is full This exception is implementation-dependent Q 1 2 r f Q 1 2 f r Queues

19 Queue Operations (cont.)
Algorithm dequeue() if isEmpty() then throw EmptyQueueException else o  Q[f] f  (f + 1) mod N return o Operation dequeue throws an exception if the queue is empty This exception is specified in the queue ADT Q 1 2 r f Q 1 2 f r Queues

20 Linked List Implementation
Which end do you think is the front of the queue? Does it matter which end of a singly-linked list we use for the front of the queue? 10 15 7 null 13 next Queues

21 Linked List Implementation
A queue can also be implemented with a linked list with both a head and a tail pointer. 10 15 7 null 13 head_ptr tail_ptr next A linked list can also be used to implement a queue, but we must maintain both a head and a tail pointer because we need access to both the front and the rear of the queue. Queues

22 Linked List Implementation
The head_ptr points to the front of the list. it is harder to remove items from the tail of the list. 10 15 7 null 13 head_ptr tail_ptr next Of course, we could put the front of the queue at the end of the linked list, but it would be hard to remove an item. Do you see why? Front Back Queues

23 Summary Stacks and queues are fundamental to CS and have many applications. Stack items push and pop at the top. Queue items enqueue at the rear and dequeue from the front. Both can be implemented either via an array or a linked-list. Queues


Download ppt "Queues Queues Queues."

Similar presentations


Ads by Google