You are on page 1of 25

QUEUES

• A queue is a linear collection or linear list in which insertion can take


place only at one end and deletions from the other .

• The behavior of a queue is like a First-In-First-Out (FIFO) system.


• The element inserted to the data structure will be the first element to
be removed from it.
• We cannot add or remove random elements from this data structure.
• It is an ADT (Abstract Type Data Structure).

• This data structure has two ends viz. Front and Rear.
• Data added to the queue from the last called REAR.
• Data removed from the beginning end called FRONT.

• The process of inserting an element to the queue Data Structure is


called Enqueue and the process of removing/deleting an element
from the queue DataStructure is called Dequeue.
Types of Queues in Data Structure

There are four different types of queues in data structures:

• Simple Queue/Linear Queue


• Circular Queue
• Priority Queue
• Double-Ended Queue (Deque)
Simple Queue/Linear Queue
In a simple queue, insertion takes place at the rear and removal
occurs at the front.

It strictly follows the FIFO (First in First out) rule.

• Ordered collection of comparable data kinds.


• Queue structure is FIFO (First in, First Out).
• When a new element is added, all elements added before the new
element must be deleted in order to remove the new element.
Memory Representation of Linear Queue(Using Array)

In this representation the Queue is implemented using the array.


Variables used in this case are:

QUEUE- the name of the array storing queue elements.

FRONT- the index where the first element is stored in the array
representing the queue.

REAR- the index where the last element is stored in array


representing the queue.

MAX- defining that how many elements (maximum count) can be


stored in the array representing the queue.
Operations on Queue / Simple Queue

The following operations can be performed on Queue:-

• CreateQueue - To create an empty queue .

• Enqueue - To add (insert) and element in the queue.

• Dequeue - To access and remove an element of the queue .

• Peek - To access the first element of the queue without removing it.

• IsFull - To check whether the queue is full.

• IsEmpty - To check whether the queue is empty.


Enqueue() Operation
Process of adding or storing an element to the end of the queue.

The following steps should be followed to insert (enqueue) data


element into a queue -

Step 1: Check if the queue is full.


Step 2: If the queue is full, Overflow error.
Step 3: If the queue is not full, increment the rear pointer to point to
the next available empty space.
Step 4: Add the data element to the queue location where the rear is
pointing.
Algorithm to Add an element in a Queue( Enqueue)

Step 1: IF REAR = MAX – 1 Write “OVERFLOW” AND Exit .


Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: Set QUEUE[REAR] = ITEM
Step 4: EXIT
Step 5: Here, you have successfully added 7, 2, and -9.
Dequeue() Operation
Process of removing or accessing an element from the front of the
queue

Obtaining data from the queue comprises two subtasks:


• access the data where the front is pointing and
• remove the data after access.

You should take the following steps to remove data from the queue -

Step 1: Check if the queue is empty.


Step 2: If the queue is empty, Underflow error.
Step 3: If the queue is not empty, access the data where the front
pointer is pointing.
Step 4: Increment front pointer to point to the next available data
element.
Algorithm to Delete an element in a Queue( Dequeue)

Step 1: IF FRONT = -1 or FRONT > REAR Write UNDERFLOW


ELSE
SET VAL = QUEUE[FRONT]
SET FRONT = FRONT + 1
[END OF IF]
Step 2: EXIT
Step 5: Here, you have removed 7, 2, and -9 from the queue data
structure.
Peek() Operation

Used to get the element at the front of the queue without removing it.

This function helps in extracting the data element where the front is
pointing without removing it from the queue.

The algorithm of Peek() function is as follows-

Step 1: Check if the queue is empty.


Step 2: If the queue is empty, return “Queue is Empty.”
Step 3: If the queue is not empty, access the data where the front
pointer is pointing.
Step 4: Return data.
isFull() Operation

Checks if the queue is full.

This function checks if the rear pointer is reached at MAXSIZE to


determine that the queue is full.

The following steps are performed in the isFull() operation -

Step 1: Check if rear == MAXSIZE - 1.


Step 2: If they are equal, return “Queue is Full.”
Step 3: If they are not equal, return “Queue is not Full.”
isEmpty() Operation

The algorithm of the isEmpty() operation is as follows -

Step 1: Check if the rear and front are pointing to null memory space,
i.e., -1.
Step 2: If they are pointing to -1, return “Queue is empty.”
Step 3: If they are not equal, return “Queue is not empty.”
Circular Queue
A circular queue is a special case of a simple queue in which the last
member is linked to the first.
As a result, a circle-like structure is formed.

• The last node is connected to the first node.


• Also known as a Ring Buffer as the nodes are connected end to end.
• Insertion takes place at the front of the queue and deletion at the
end of the queue.
Circular queue application: Insertion of days in a week.
Priority Queue
In a priority queue, the nodes will have some predefined priority in the
priority queue.

A priority queue is a special type of queue in which each element is


associated with a priority and is served according to its priority.

If elements with the same priority occur, they are served according to
their order in the queue.

Insertion occurs based on the arrival of the values and removal occurs
based on priority.

Some of the applications of priority queue:


• Dijkstra’s shortest path algorithm
• Prim’s algorithm
• Data compression techniques like Huffman code
Below diagram shows how an application use priority queue for the
items consumed by the user.
Deque (Double Ended Queue)
In a double-ended queue, insertion and deletion can take place at
both the front and rear ends of the queue.
C program to implement Queue

#include<stdio.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0, i, j=1, x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
return 0;
}
Output :Queue using Array
1.Insertion
2.Deletion
3.Display
4.Exit
Enter the Choice:1

Enter no 1:10

Enter the Choice:1

Enter no 2:54

Enter the Choice:1

Enter no 3:98

Enter the Choice:1

Enter no 4:234

Enter the Choice:3

Queue Elements are:


10
54
98
234
Applications of Queue
Queue, as the name suggests, is utilized when you need to regulate a group of
objects in order.
This data structure caters to the need for First Come First Serve problems in
different software applications.
The scenarios mentioned below are a few systems that use the queue data
structure to serve their needs –

• Printers: Queue data structure is used in printers to maintain the order of pages
while printing.

• Interrupt handling in computes: The interrupts are operated in the same order
as they arrive, i.e., interrupt which comes first, will be dealt with first.

• Process scheduling in Operating systems: Queues are used to implement


round-robin scheduling algorithms in computer systems.

• Switches and Routers: Both switch and router interfaces maintain ingress
(inbound) and egress (outbound) queues to store packets.

• Customer service systems: It develops call center phone systems using the
concepts of queues.
• Managing requests on a single shared resource such as CPU scheduling and
disk scheduling

• Handling hardware or real-time systems interrupts.

• Handling website traffic

• Routers and switches in networking

• Maintaining the playlist in media players

You might also like