LEARNING ABOUT A NEW DATA STRUCTURE
•QUEUES
What is a queue?
Basic features of Queue
Queue data structure is a linear data structure in which the
operations are performed based on FIFO principle.
Queue data structure is a collection of similar data items in which
insertion and deletion operations are performed based on FIFO
principle
Queue
Basic features of Queue
• Queue is an abstract data structure
• A queue is open at both its ends
• One end (rear) is always used to insert data (enqueue) and
• The other end (front) is used to remove data (dequeue).
• Queue follows First-In-First-Out methodology, i.e., the data
item stored first will be accessed first.
• Real-world examples can be seen as queues at the ticket
windows and bus-stops.
Queues
Basic features of Queue
• Linear list.
• One end is called front.
• Other end is called rear.
• Additions are done at the rear only.
• Removals are made from the front only.
Queue Operations
Enqueue
Dequeue
Queue Front
Queue Rear
Queue Example
ENQUEUE
DEQUEUE
FRONT OF QUEUE
REAR OF QUEUE
Queue: a First-In-First-Out (FIFO) list
D rear
C rear C D rear
B rear B B C
rear front A front
A A front A front B
front
*Figure 3.4: Inserting and deleting elements in a queue (p.108)
CHAPTER 3 11
Queues - Application
• Used by operating system (OS) to
create job queues.
• If OS does not use priorities then the
jobs are processed in the order they
enter the system
CHAPTER 3 12
Application: Job scheduling
fron
t re
ar Q[0]Q
[1]Q[2]Q[3
]C omments
-1 -1 que
ueisemp ty
-1 0 J1 Jo
b1isadde d
-1 1 J1 J2 Jo
b2isadde d
-1 2 J1 J2 J3 Jo
b3isadde d
0 2 J2 J3 Jo
b1isd e
leted
1 2 J3 Jo
b2isd e
leted
*Figure 3.5: Insertion and deletion from a sequential queue (p.117)
CHAPTER 3 13
Applications of Queue
➢ Serving requests on a single shared resource, like a
printer, CPU task scheduling etc.
➢ In real life scenario, Call Center phone systems uses
Queues to hold people calling them in an order, until
a service representative is free.
➢ Handling of interrupts in real-time systems. The
interrupts are handled in the same order as they
arrive i.e. First come first served.
Implementation of Queue Data Structure- Insertion and deletion
Implementation of Queue Data Structure- Insertion and deletion
In [A] approach, we remove the element at head position, and then
one by one shift all the other elements in forward position.
In approach [B] we remove the element from head position and then
move head to the next position.
In approach [A] there is an overhead of shifting the elements one
position forward every time we remove the first element.
In approach [B] there is no such overhead, but whenever we move
head one position ahead, after removal of first element, the size on
Queue is reduced by one space each time.
Operations on a Queue
1.enQueue(value) - (To insert an element into the queue)
2.deQueue() - (To delete an element from the queue)
3.display() - (To display the elements of the queue)
Algorithm for ENQUEUE operation
1.Check if the queue is full or not.
2.If the queue is full, then print overflow error and exit the program.
3.If the queue is not full, then increment the tail and add the element.
Algorithm for DEQUEUE operation
1.Check if the queue is empty or not.
2.If the queue is empty, then print underflow error and exit the program.
3.If the queue is not empty, then print the element at the head and
increment the head.
Enqueue
Enqueue: If the queue is not full, this function adds an element to
the back of the queue, else it prints “OverFlow”.
void enqueue(int queue[], int element, int& rear, int arraySize)
{
if(rear == arraySize) // Queue is full
printf(“OverFlow\n”);
else
{
queue[rear] = element; // Add the element to the back
rear++;
}
}
Dequeue
Dequeue: If the queue is not empty, this function removes the
element from the front of the queue, else it prints “UnderFlow”.
void dequeue(int queue[], int& front, int rear)
{
if(front == rear) // Queue is empty
printf(“UnderFlow\n”);
else
{
queue[front] = 0; // Delete the front element
front++;
}
}
IsEmpty: If a queue is empty, this function returns 'true', else it
returns 'false'.
bool isEmpty(int front, int rear)
{
return (front == rear);
}
Front: This function returns the front element of the queue.
int Front(int queue[], int front)
{
return queue[front];
}
Size: This function returns the size of a queue or the number of
elements in a queue.
int size(int front, int rear)
{
return (rear - front);
}
22
Complexity Analysis of Queue Operations
•Enqueue: O(1)
•Dequeue: O(1)
•Size: O(1)
Queue variations
The standard queue data structure has the following variations:
1.Double-ended queue
2.Circular queue
3. Priority queue
Circular queues
• A circular queue is an improvement over the standard queue
structure.
• In a standard queue, when an element is deleted, the vacant space
is not reutilized. However, in a circular queue, vacant spaces are
reutilized.
• While inserting elements, when you reach the end of an array and
you need to insert another element, you must insert that element at
the beginning (given that the first element has been deleted and the
space is vacant).
25
26
Basic features of Circular Queue
1. Head pointer will always point to the front of the queue, and
2.Tail pointer will always point to the end of the queue.
3.Initially, the head and the tail pointers will be pointing to the same location,
this would mean that the queue is empty.
27
New data is always added to the location pointed by the tail pointer,
and once the data is added, tail pointer is incremented to point to the
next available location.
28
The head and the tail pointer will get reinitialised to 0 every time they
reach the end of the queue.
➢ Also, the head and the tail pointers can cross each other.
➢ In other words, head pointer can be greater than the tail.
➢ This will happen when we dequeue the queue a couple of times and
the tail pointer gets reinitialised upon reaching the end of the queue.
Going Round and Round
➢ Another very important point is keeping the value of the tail and
the head pointer within the maximum queue size.
➢ In the diagrams above the queue has a size of 8, hence, the value
of tail and head pointers will always be between 0 and 7.
➢ This can be controlled either by checking everytime
whether tail or head have reached the maxSize and then setting
the value 0
So the formula to increment the head and tail pointers to make them go
round and round over and again will be,
head = (head+1) % maxSize ,or
tail = (tail+1) % maxSize
Application of Circular Queue
Below we have some common real-world examples where circular
queues are used:
1.Computer controlled Traffic Signal System uses circular queue.
2.CPU scheduling and Memory management.
33
Implementation of Circular Queue
1.Initialize the queue, with size of the queue defined (maxSize),
and head and tail pointers.
2.enqueue: Check if the number of elements is equal to maxSize - 1:
•If Yes, then return Queue is full.
•If No, then add the new data element to the location of tail pointer and
increment the tailpointer.
3.dequeue: Check if the number of elements in the queue is zero:
•If Yes, then return Queue is empty.
•If No, then increment the head pointer.
34
Enqueue
void enqueue(int queue[], int element, int& rear, int arraySize, int& count)
{
if(count == arraySize) // Queue is full
printf(“OverFlow\n”);
else
{
queue[rear] = element;
rear = (rear + 1)%arraySize;
count = count + 1;
}
}
35
Dequeue
void dequeue(int queue[], int& front, int rear, int& count)
{
if(count == 0) // Queue is empty
printf(“UnderFlow\n”);
else
{
queue[front] = 0; // Delete the front element
front = (front + 1)%arraySize;
count = count - 1;
}
} 36
Front Size
int Front(int queue[], int front) int size(int count)
{ {
return queue[front]; return count;
} }
IsEmpty
bool isEmpty(int count)
{
return (count == 0);
}
37
38
39
Double-ended queue
➢ In a standard queue, a character is inserted at the
back and deleted in the front.
➢ However, in a double-ended queue, characters can
be inserted and deleted from both the front and
back of the queue.
Double Ended Queue
◼ What is a double ended queue (deque) ?
⚫ Queue in which insertion (enqueue) and deletion (dequeue) can
be made at both ends
◼ Operations on a deque
⚫ insert_rear
⚫ insert_front
⚫ delete_front
⚫ delete_rear
41
Double Ended Queue
◼ Types of deque
1. Input-restricted: Insert at rear end only, delete from any end
2. Output-restricted: Delete from front end only, insert at any end
42
Priority Queue
◼ What is a priority queue?
⚫ Queue in which each element has a priority associated with it
⚫ Priority – a unique number that determines the relative importance of
that element compared to other
43
Priority Queue
◼ Operations on Priority Queue
⚫ Insertion according to priority
⚫ Deletion as usual
⚫ Types
Max (descending) priority queue – delete element with highest
priority
Min (ascending) priority queue – delete element with least
priority
44
END OF QUEUE
45