What Is Queue
What Is Queue
answer: A Queue is defined as a linear data structure that is open at both ends and the
operations are performed in First In First Out (FIFO) order.
Applications of Queue:
Multi programming: Multi programming means when multiple programs are running in
the main memory. It is essential to organize these multiple programs and these
multiple programs are organized as queues.
Network: In a network, a queue is used in devices such as a router or a switch.
another application of a queue is a mail queue which is a directory that stores data and
controls files for mail messages.
Job Scheduling: The computer has a task to execute a particular number of jobs that
are scheduled to be executed one after another. These jobs are assigned to the
processor one by one which is organized using a queue.
Shared resources: Queues are used as waiting lists for a single shared resource.
Advantages of Queue:
A large amount of data can be managed efficiently with ease.
Operations such as insertion and deletion can be performed with ease as it follows the
first in first out rule.
Queues are useful when a particular service is used by multiple consumers.
Queues are fast in speed for data inter-process communication.
Queues can be used in the implementation of other data structures.
Disadvantages of Queue:
The operations such as insertion and deletion of elements from the middle are time
consuming.
Limited Space.
In a classical queue, a new element can only be inserted when the existing elements
are deleted from the queue.
Searching an element takes O(N) time.
Maximum size of a queue must be defined prior.
1.Supermarket Checkout:
Customers form a queue to check out their items. The first customer in line is served first,
and new customers join the end of the queue.
2.ATM Machine:
People form a queue to withdraw or deposit money. The ATM serves one person at a
time in the order they arrived.
3.Customer Support Call Center:
Calls are placed in a queue and answered by available agents as they become free. Calls
are typically answered in the order they were received.
4.Online Ticket Booking:
When booking tickets for events, movies, or flights, users are placed in a virtual queue.
Tickets are allocated based on availability and the order of requests.
5.Print Queue:
In an office setting, multiple users may send print jobs to a shared printer. The printer
processes the jobs in the order they were received.
6.Traffic Signal:
At a busy intersection, cars form a queue. The traffic signal allows a certain number of
cars from each direction to pass in a synchronized manner.
7.Job Scheduling in Operating Systems:
Processes waiting to be executed are placed in a queue. The operating system scheduler
determines which process to run next based on priority and scheduling algorithms.
8.Ride-Sharing Services:
Users requesting rides are placed in a queue. Drivers are assigned based on factors like
proximity and availability.
9.Hospital Emergency Room:
Patients arriving at an ER are triaged based on the severity of their condition. They are
then placed in a queue to see a doctor.
10.Buffer in Networking:
In networking, queues are used to temporarily store data packets when there's
congestion in the network. The packets are sent in the order they were received once the
network congestion subsides.
11.Email Inbox:
Emails are typically received and processed in the order they were received, forming a
kind of queue in your inbox.
12.Food Delivery Apps:
When multiple orders are placed, they are placed in a queue for preparation and
delivery. The orders are typically fulfilled in the order they were received.
#define N 5
int queue[N];
int front=-1;
int rear=-1;
void enqueue(int x)
if(rear == N-1)
printf("overflow\n");
front=rear=0;
queue[rear]=x;
else
rear++;
queue[rear]=x;
void dequeue()
printf("empty\n");
else if(front==rear)
front=rear=-1;
else
printf("%d\n",queue[front]);
front++;
void display()
int i;
printf("empty\n");
else
printf("%d\n",queue[i]);
void peek()
printf("empty\n");
else
printf("%d\n",queue[front]);
int main()
enqueue(2);
enqueue(5);
enqueue(-1);
display();
peek();
dequeue();
peek();
peek();
display();
return 0;
#include<stdlib.h>
struct node
int data;
};
void enqueue(int x)
newnode->data=x;
newnode->next=0;
front=rear=newnode;
else
rear->next=newnode;
rear=newnode;
void display()
if(front==0&&rear==0)
{
printf("empty");
else
temp=front;
while(temp!=0)
printf("%d\n",temp->data);
temp=temp->next;
void dequeue()
temp=front;
if(front==0&&rear==0)
printf("empty");
else
printf("%d\n",front->data);
front=front->next;
free(temp);
}
void peek()
if(front==0&&rear==0)
printf("empty");
else
printf("%d\n",front->data);
void main()
enqueue(5);
enqueue(0);
enqueue(-3);
display();
peek();
priority queue:
#include<stdio.h>
#define N 5
typedef struct {
int data;
int priority;
} Element;
Element queue[N];
int front=-1;
int rear=-1;
{
Element newItem;
newItem.data = x;
newItem.priority = priority;
if(rear == N-1)
printf("overflow\n");
return;
front=rear=0;
queue[rear]=newItem;
else
int i;
queue[i+1] = queue[i];
} else {
break;
queue[i+1] = newItem;
rear++;
void dequeue()
{
if(front==-1 && rear==-1)
printf("empty\n");
else if(front==rear)
front=rear=-1;
else
printf("%d\n",queue[front].data);
front++;
void display()
int i;
printf("empty\n");
else
}
void peek()
printf("empty\n");
else
int main()
enqueue(2, 1);
enqueue(5, 3);
enqueue(-1, 2);
display();
peek();
dequeue();
peek();
peek();
display();
return 0;
Create pointers front and rear and set them to NULL to indicate an empty queue.
Check if the queue is empty (front == NULL && rear == NULL). If true, set both front and
rear to newnode.
If the queue is not empty, set rear->next to newnode and update rear to newnode.