Queues
-
Upload
bikash-agrawal -
Category
Education
-
view
7.171 -
download
1
description
Transcript of Queues
![Page 1: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/1.jpg)
Data Structures
BIM, 3nd Semester
Tribhuvan University
![Page 2: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/2.jpg)
Lecture 1: Queues
Readings: Chapter 4
![Page 3: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/3.jpg)
3
What is a Queue
• In ordinary English a queue is defined as a waiting line, like a line of people waiting to purchase tickets– Here, the first person in the line is the first person served
• A queue is an ordered collection of items in which all insertions are made at one end (the rear), and all deletions are made at the other end (the front)
![Page 4: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/4.jpg)
4
Queues
12 69 50 32 78 88
frontrear
frontrear
A waiting queueComputer Implementation
of queue
![Page 5: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/5.jpg)
5
Enqueue & Dequeue Operations
• Enqueue – Inserts an item at the rear of the queue. Other names – Add, Insert
• Dequeue – Deletes an item from the front of the queue. Other names – Delete, Remove, Serve
• Since it is always the first item to be put into the queue that is the first item to be removed, a queue is a first-in, first-out or FIFO list
• Front and rear are also called head and tail respectively
![Page 6: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/6.jpg)
6
Illustration
7869 50 32 69 50 32Enqueue(78)
7869 50 32
Enqueue(88)
88
7850 32 88
Dequeue() → 69
7832 88 Dequeue() → 50
f r rf
f r
f rf r
A queue is a FIFO list
![Page 7: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/7.jpg)
7
Applications
• Direct Applications– Access to shared resources
• Within a computer system there may be queues of tasks waiting for the printer, for access to disk storage, or even in a time-sharing system, for use of the CPU
• Indirect Applications– Auxiliary data structure for algorithms
– Component of other data structures
![Page 8: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/8.jpg)
8
Checking Palindromes
• Read each letter in the phrase. Enqueue the letter into the queue, and push the letter onto the stack.
• After we have read all of the letters in the phrase:– Until the stack is empty, Dequeue a letter from the
queue and pop a letter from the stack. – If the letters are not the same, the phrase is not a
palindrome
![Page 9: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/9.jpg)
9
The Queue ADT
• A queue of elements of type T is a finite sequence of elements of T together with the operations– MakeEmpty(Q) – Create an empty queue Q
– Empty(Q) – Determine if the queue Q is empty or not
– Enqueue(Q, x) – Insert element x at the end of the queue Q
– Dequeue(Q) – If the queue Q is not empty, remove the element at the front of the queue
– Front(Q) – Retrieve the element at the front of the queue Q, without deleting it
![Page 10: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/10.jpg)
10
Implementation of Queues
• The physical model: a linear array with the front always in the first position and all entries moved up the array whenever the front is deleted
• Array Implementation:– A linear array with two indices always increasing– A circular array with front and rear indices and one position
left vacant
• Linked List– A linked list with pointers to the front and rear nodes
![Page 11: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/11.jpg)
11
Linear Implementation
• For all type of array implementation, we set up an array to hold the items in the queue
• In linear implementation, we use two indices to keep track the front and the rear of the queue
• To enqueue an item, we increase the rear by one and put the item in that position
• To dequeue an item, we take it from the position at the front and then increase front by one
![Page 12: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/12.jpg)
12
The QueueType Declaration
#define MAXQUEUESIZE 100
typedef int ItemType;
typedef struct {ItemType items[MAXQUEUESIZE];int front, rear;
} QueueType;
![Page 13: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/13.jpg)
13
The MakeEmpty Operation
• To initialize an empty queue, we set front to 0 and rear to -1
void MakeEmpty(QueueType *pq){
pq->front = 0;pq->rear = -1;
}
![Page 14: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/14.jpg)
14
Illustration
r = -1, f = 0
10 2 3 4
r = 2
1832 58
10 2 3 4
f = 0
f = 2, r = 2
58
10 2 3 4
Enqueue(32)Enqueue(18)Enqueue(58)
Dequeue() → 32Dequeue() → 18
Enqueue(68)Enqueue(96)
f = 2
58 68 96
10 2 3 4
r = 4
A queue with an array of size 5
![Page 15: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/15.jpg)
15
The Full and Empty function
int Empty(QueueType *pq){
return pq->rear < pq->front;}
int Full(QueueType *pq){
return pq->rear == MAXQUEUESIZE-1;}
![Page 16: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/16.jpg)
16
The Enqueue function
void Enqueue(QueueType *pq, ItemType newitem){
if (Full(pq)) {printf("Queue Full\n",);exit(1);
}pq->items[++pq->rear] = newitem;
}
![Page 17: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/17.jpg)
17
The Dequeue function
ItemType Dequeue(QueueType *pq){
if (Empty(pq)) {printf("Queue is Empty\n",);exit(1);
}return pq->items[pq->front++];
}
![Page 18: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/18.jpg)
18
Problems with Linear Implementation
• Both the rear and front indices are increased but never decreased
• As items are removed from the queue, the storage space at the beginning of the array is discarded and never used again
f = 2
58 68 96
10 2 3 4
r = 4
This queue is considered full, even though the space at beginning is vacant
![Page 19: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/19.jpg)
19
Circular Array Implementation
• View the array, holding the queue, as a circle rather than a straight line
• Imagine the first element of the array (i.e., the element at index 0) as immediately following its last element
• If incrementing either rear or front causes it to go past the array, reset its value to zero
• With this approach, we can always insert an item unless the array is fully occupied
![Page 20: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/20.jpg)
20
Illustration
Enqueue(21)
Dequeue() → 58Dequeue() → 68
Enqueue(81)
f = 2
58 68 96
10 2 3 4
r = 4 f = 2
21 58 68 96
10 2 3 4
r = 0
f = 4
21 96
10 2 3 4
r = 0f = 4
8121 96
10 2 3 4
r = 1
f = 0
8121
10 2 3 4
r = 1
Dequeue() → 96
![Page 21: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/21.jpg)
21
Checking Boundary Conditions
• The condition rear < front does not hold for empty queue
f = 2
21 58 68 96
10 2 3 4
r = 0
f = 4
21 96
10 2 3 4
r = 0
f = 4
8121 96
10 2 3 4
r = 1
In all cases, rear < front but the queue is not empty
![Page 22: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/22.jpg)
22
Checking Boundary Conditions
• Same relative positions for an empty and a full queue
r = 2, f = 2
58
10 2 3 4
f = 3
5821 68 96
10 2 3 4
r = 1
f = 3
10 2 3 4
r = 2 f = 3
5821 97 68 96
10 2 3 4
r = 2
Dequeue() → 58 Enqueue(97)
Empty queue Full queue
![Page 23: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/23.jpg)
23
Possible Solutions
• At least 3 ways to resolve the problem– Leave one empty position in the array so that the
queue is considered full when the rear index has moved within two positions ahead of front
• For convenience, initialize both rear and front to same value for empty queue
We will use this method
![Page 24: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/24.jpg)
24
Possible Solutions (Cont…)
– Introduce a Boolean variable that will indicate whether the queue is full (or empty) or not, or an integer variable that counts the no of items in the queue
– Set one or both of the indices to some values that would otherwise never occur in order to indicate an empty (or full) queue.
• For e.g., an empty queue could be indicated by setting the rear index to -1
![Page 25: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/25.jpg)
25
The MakeEmpty and Empty Function
• For circular queue, we set both front and rear to MAXQUEUESIZE-1
void MakeEmpty(QueueType *pq){
pq->front = MAXQUEUESIZE-1;pq->rear = MAXQUEUESIZE-1;
}int Empty(QueueType *pq){
return pq->rear == pq->front;}
![Page 26: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/26.jpg)
26
Why to sacrifice one element?
f = 1
58 68 96
10 2 3 4
r = 4
r = 0 f = 1
21 58 68 96
10 2 3 4
r = 1, f = 1
3521 58 68 96
10 2 3 4
Enqueue(35)
Enqueue(21)
rear == front, but queue is not empty, so sacrifice one element
![Page 27: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/27.jpg)
27
The Full Function
• The queue is full, if rear is just one position ahead of front
int Full(QueueType *pq){
int newrear;newrear = (pq->rear+1)%MAXQUEUESIZE;return newrear == pq->front;
}
![Page 28: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/28.jpg)
28
The Enqueue Function
void Enqueue(QueueType *pq, ItemType newitem){
if (Full(pq)) {printf("Queue Full\n",);exit(1);
}pq->rear = (pq->rear+1)%MAXQUEUESIZE;pq->items[pq->rear] = newitem;
}
![Page 29: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/29.jpg)
29
The Dequeue Function
ItemType Dequeue(QueueType *pq){
if (Empty(pq)) {printf("Queue is Empty\n",);exit(1);
}pq->front = (pq->front+1)%MAXQUEUESIZE;return pq->items[pq->front];
}
![Page 30: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/30.jpg)
30
Summary of Array Implementation of Queues
• A linear array with the front always in the first position and all entries moved up the array whenever the front is deleted
• A linear array with two indices always increasing• A circular array with front and rear indices and one position
left vacant• A circular array with front and rear indices and either a
Boolean variable to indicate fullness (or emptiness) or an integer variable counting entries
• A circular array with front and rear indices taking special values to indicate emptiness
![Page 31: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/31.jpg)
31
Priority Queues
• In stacks and queues, the elements are ordered are based on the sequence in which they have been inserted
• A priority queue is a data structure in which the intrinsic ordering of the elements determines the results of its basic operations
![Page 32: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/32.jpg)
32
Types of Priority Queues
• Ascending (Min) Priority Queue– A collection of items into which items can be inserted
arbitrarily but only the smallest item can be removed
• Descending (Max) Priority Queue– A collection of items into which items can be inserted
arbitrarily but only the largest item can be removed
![Page 33: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/33.jpg)
33
Illustration
69 50 32 50 32 78Insert(78)
7869 50 32
Insert(88)
88
8869 50 78
DeleteMin() → 32
8869 78 DeleteMin() → 50
A min priority queue
69
![Page 34: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/34.jpg)
34
Applications of Priority Queue
• In a time-sharing computer system, a large number of tasks may be waiting for the CPU
• Some of these tasks have higher priority than others
• The set of tasks waiting for the CPU forms a priority queue
![Page 35: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/35.jpg)
35
The Priority Queue ADT
• A (min) priority queue of elements of type T is a finite sequence of elements of T together with the operations– MakeEmpty(P) – Create an empty priority queue P– Empty(P) – Determine if the priority queue P is empty or not– Insert(P, x) – Add element x on the priority queue P– DeleteMin(P) – If the priority queue P is not empty, remove
the minimum element of the queue and return it– FindMin(P) – Retrieve the minimum element of the priority
queue P
![Page 36: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/36.jpg)
36
Array Implementation of Priority Queue
• Unordered Array Implementation– To insert an item, insert it at the rear end of the
queue
– To delete an item, find the position of the minimum element and
• either mark it as deleted (lazy deletion) or
• shift all elements past the deleted element by one position and then decrement rear
![Page 37: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/37.jpg)
37
Illustration
69 50 32
78 69 50 32
Insert(88)
88
5078 69 88
DeleteMin() → 32
rf
f r
f r
78
-1
78 69 -1 -1
DeleteMi() → 50
88
f r
5078 69 88
f r
-1
The value -1 marks these entries as deleted
![Page 38: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/38.jpg)
38
Array Implementation of Priority Queue
• Ordered Array Implementation– Maintain the queue as a circular ordered array making the
front as the position of the smallest element and the rear as the position of the largest element
– To insert an element, locate the proper position of the new element and shift preceding or succeeding elements by one position
– To delete the minimum element, increment the front position
![Page 39: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/39.jpg)
39
Illustration
50 69 78
32 46 50 69
Insert(46)
78
5046 78
DeleteMin() → 32
rf
f r
f r
32
69
50 69 78
46 50 69 72
Insert(72)
78
6950 78
DeleteMin() → 46
rf
f r
f r
46
72
![Page 40: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/40.jpg)
40
Notes on Array Implementation
• The array implementation of priority queue is not satisfactory
• Either the Insert operation or DeleteMin operation is proportional to n (n is the queue size), i.e., O (n)
• A heap allows to implement the Insert and DeleteMin in O (log n) time
![Page 41: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/41.jpg)
41
Deque
• A deque (double-ended queue) is a data structure consisting of a list of items, on which the following operations are possible– Push(D, x) – Insert item x on the front of deque D– Pop(D) – Remove the front item from deque D and
return it– Inject(D, x) – Insert item x on the rear end of deque D– Eject(D) – Remove the rear item from deque D and
return it
![Page 42: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/42.jpg)
42
Illustration
69 50 32 69 50 32Push(78)
78 69 50 32
Inject(88)
88
3269 50 88
Pop() → 78
5069 32 Eject() → 88
f r rf
f r
f rf r
A deque
78
![Page 43: Queues](https://reader033.fdocuments.in/reader033/viewer/2022052821/5549584bb4c90566498b51f4/html5/thumbnails/43.jpg)
The End