Queues
Queues
Lecture 8: Queues
Today‘s Lecture
► Queues
Lecture 8: Queues 2
Queues
▪ Unlike a stack, a queue is a structure in which both ends are
used: one for adding new elements and one for removing them
Lecture 8: Queues 3
Queues
Examples:
Printer SPOOL
CPU Scheduling
(at times)
Lecture 8: Queues 4
Queues
Lecture 8: Queues 5
Queue Attributes
Lecture 8: Queues 7
Queue Operations
▪ Enqueue Concerns
► What if when the queue is full?
▪ Dequeue Concerns
► What if the queue is empty
Solution:
Lecture 8: Queues 9
Choice of implementation
Lecture 8: Queues 10
Array based queue implementation
#define length 5
Struct q // queue
{
int items[length];
int front, rear;
}
insert(q, x) // enqueue
q.items[++q.rear]=x;
X=remove(q) // dequeue
x=q.items[++q.front];
Lecture 8: Queues 12
Array based queue implementation
4 4
3 3
2 2 C q.rear = 2
1 1 B
0 q.front = 0 0 A q.front = 0
q.rear = -1
Lecture 8: Queues 14
One possible solution: Shift items
0 1 2 3 4
A B C D
Rear Rear
0 1 2 3 4
B C D dequeue
Lecture 8: Queues 15
Dequeue operation
x = q.items[0];
for ( i = 0 ; i < q.rear; i++ )
q.items[i] = q.items[i+1]
q.rear--;
first index
[0] [3]
[1] [2]
Lecture 8: Queues 17
Circular Queue Operations cont.
The queue is empty now
front Condition: front
front - 1 == rear
q[0] q[1] q[2] q[3] q[4] q[5] q[0] q[1] q[2] q[3] q[4] q[5]
? ? ? ? ? ? A ? ? ? ? ?
rear (1) Empty Queue (2) Enqueue(A)
rear
front front
q[0] q[1] q[2] q[3] q[4] q[5] q[0] q[1] q[2] q[3] q[4] q[5]
A D ? ? ? ? A D T ? ? ?
front front
q[0] q[1] q[2] q[3] q[4] q[5] q[0] q[1] q[2] q[3] q[4] q[5]
A D T E ? ? A D T E F ?
rear rear
(5) Enqueue (E) (6) Enqueue (F)
front
q[0] q[1] q[2] q[3] q[4] q[5] Now the queue is full, what is
A D T E F G the condition to check that?
Condition:
rear front == 0 && rear == Max-1
(7) Enqueue (G)
Lecture 8: Queues 19
Circular Queue Operations cont.
front front
q[0] q[1] q[2] q[3] q[4] q[5] q[0] q[1] q[2] q[3] q[4] q[5]
A D T E F G A D T E F G
q[0] q[1] q[2] q[3] q[4] q[5] q[0] q[1] q[2] q[3] q[4] q[5]
H D T E F G H I T E F G
rear rear
(10) Enqueue (H) (12) Enqueue (I)
Now the queue is full, what is the condition to check
that?
Condition:
front - 1 == rear
NOTE: it is the same condition as if queue
is empty
We need a distinguishing condition for empty queue
and full queue
Lecture 8: Queues 21
Circular Queue Operations cont.
front
front = 0
q[0] q[1] q[2] q[3] q[4] q[5] rear = 5
H I T E F G
Condition:
front == 0 && rear == Max - 1
front rear
front = 2
q[0] q[1] q[2] q[3] q[4] q[5]
rear = 1
H I T E F G
Condition:
( (rear + 1) % Max == front - 1)
rear
Lecture 8: Queues 23
Better way!
rear
#define length 5
bool IsFull(struct queue *pq)
struct queue
{
{
return (
int items[length]; int
(pq->rear + 1) % length == pq->front
front, rear;
);
}; }
Struct queue q;
q.front = length –1; //initialize
q.rear = length –1; // initialize
Full: ( (rear + 1) % length == front)
bool IsEmpty(struct queue *pq) Empty: rear == front
{
return(pq->front==pq->rear)
}
Lecture 8: Queues 25
Implementation cont.
void insert(struct queue *pq, int x) int remove (struct queue *pq){
{ if (! isEmpty(pq)){
pq->front = (pq->front + 1) % length;
if (!isFull(pq))
return pq->items[pq->front];
{ }
pq->rear = (pq->rear + 1) % length;
else{
pq->items[pq->rear] = x; cout<<“queue underflow”
}
}
else return 0;
cout<< “Queue is full: ”; }
}
Lecture 8: Queues 26
Another way
Lecture 8: Queues 27
struct Queue {
int Count; // number of queue items
int Front; // head of queue
int Rear; // tail of queue
int Items[length];
};
Q->Rear = length-1;
}
int isEmpty(Queue *Q)
{ return (Q->Count == 0); }
Lecture 8: Queues 29
Linked List Based Implementation of Queues?
Lecture 8: Queues 30
Priority queues
▪ In many situations, simple queues are inadequate, because first
in/first out scheduling has to be overruled using some priority criteria
► A handicapped person may have priority over others and may be
served first
► On roads with tollbooths, some vehicles may be put through
immediately, even without paying (police cars, ambulances, fire
engines, and the like)
► In a sequence of processes, process P2 may need to be
executed before process P1 for the proper functioning of a
system
▪ In priority queues, elements are dequeued according to their
priority and their current queue position
▪ Although priority queue can be implemented using many of the data
structures e.g., an array, a linked list etc., they do not provide the most
efficient operations. To make all of the operations very efficient, heap
data structure is typically employed
Lecture 8: Queues 31
Deques (‚Decks‘)
remove remove
rear front from front
from rear
Lecture 8: Queues 32
Deque Operations
Lecture 8: Queues 33
Deques
Lecture 8: Queues 34