Lecture 7 - Queues
Lecture 7 - Queues
DATA
STRUCTURES
QUEUE
Real world examples Queues in computer science
People on an escalator or
waiting in line for payment
• The working mechanism: The element to enter the queue first will be deleted from
the queue first (First-In-First-Out (FIFO) system).
STACK VS QUEUE
• One of the main difference between stacks and queues is in removing.
– In a stack we remove the item the most recently added;
– in a queue, we remove the item the least recently added.
• Also, significant structural difference is that the queue implementation needs to keep
track of the front and the rear of the queue, whereas the stack only needs to worry
about one end: the top.
OPERATIONS ON QUEUE
HOW
On enqueuing an element, we
increase the value of REAR index
QUEUE
and place the new element in the
position pointed to by REAR.
1 5 3 2
0 1 2 3 4 0 1 2 3 4
Rear=-1 Rear=2
Front=0 enqueue 8 Front=1 Enqueue 5
2 8 6 3 2 5
0 1 2 3 4 0 1 2 3 4
Rear=0 Rear=3
Front=0 enqueue 3 Front=2 dequeue
3 8 3 7 2 5
0 1 2 3 4 0 1 2 3 4
Rear=1 Rear=3
Front=1 dequeue Front=2 Enqueue 9
4 3 8 2 5 9
0 1 2 3 4 0 1 2 3 4
Rear=1 Rear=4
THE TYPES OF QUEUE
• Types
– Simple Queue
• CPU scheduling, Disk Scheduling
• Synchronization between two process.
– Circular Queue
• Memory management, Traffic Management
– Deque
• To execute undo and redo operation.
– Priority Queue
• Sorting heap
• Representations
– Queue Representation with Array
– Queue Representation with Linked List
QUEUE REPRESENTATION WITH ARRAY
queue->array[++queue->rear] = item;
return item;
}
It is known as a
boundary case problem.
Rear
Rear
3. Insert 50, Rear = 2, Front = 1. 6. Delete front, Rear = 4, Front = 2.
Front Rear Front
Rear
Note: Circular Queue‘s circularity is only logical. There cannot be a physical circularity in main memory.
7. Insert 100, Rear = 5, Front = 2. 10. Delete front, Rear = 1, Front = 3.
Front Rear
Front
Rear
Front
Rear Front
Front
ALGORITHMS FOR ENQUEUE AND
DEQUEUE OPERATIONS
10 enqueued to queue
20 enqueued to queue
30 enqueued to queue
40 enqueued to queue
10 dequeued from queue
Front item is 20
Rear item is 40
#include <stdio.h>
#include <stdlib.h>
#define SIZE 6
struct Queue {
int front, rear, currSize;
unsigned maxSize;
int* a;
};
return 0;
}
// Function to do dequeue
int dequeue(struct Queue* queue)
{
int item;
// Function to do enqueue if (isEmpty(queue))
void enqueue(struct Queue* queue,int value){ {
if (isFull(queue)) printf("Can't add the queue is empty \n");
printf("Can't add the queue is full \n"); return (-1);
}
else else
{ {
if (queue->front == -1) item = queue->a[queue->front];
queue->front = 0; if (queue->front == queue->rear)
{
queue->rear = (queue->rear + 1) % queue->maxSize; queue->front = queue->rear = -1 ;
queue->a[queue->rear] = value; }
printf("%d was added\n", value); else
} {
} queue->front = (queue->front + 1) % queue->maxSize;
}
printf("%d dequeued\n", item);
}
}
// Function to print the queue
void print(struct Queue* queue) {
int i;
if (isEmpty(queue))
printf("Empty Queue\n");
else {
enqueue(queue,12);
enqueue(queue,14);
enqueue(queue,16);
enqueue(queue,18);
enqueue(queue,20);
print(queue);
dequeue(queue);
dequeue(queue);
print(queue);
enqueue(queue,22);
enqueue(queue,24);
enqueue(queue,26);
enqueue(queue,28);//Overflow condition
print(queue);
return 0;
}
A DOUBLE-ENDED QUEUE (DEQUE)
• A linear list that generalizes a queue, in which the insertion and deletion operations are
performed at both the ends (front and rear), but not in the middle (FIFO rule).
– It is also often called a head-tail linked list
• Output Restricted Double Ended Queue: Deletions can be made from only one end
of the list but insertions can be made at both ends of the list
OPERATIONS
• There are four basic operations in usage of Deque.
– Insertion at rear end
– Insertion at front end
– Deletion at front end
– Deletion at rear end
n o v a n g j h l m u u r x l v
Key 3 1 7 4 2 5 3 1 7 4 2 5 3 1 7 4
k n o w l e d g e i s p o w e r
Decoded message
6-46
USING QUEUES: CODED MESSAGES
• We can use a queue to store the values of the key
• dequeue a key value when needed
• After using it, enqueue it back onto the end of
the queue
https://www.csd.uwo.ca/Courses/CS1027b/notes/queues.pdf