0% found this document useful (0 votes)
10 views13 pages

What Is Queue

CSE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views13 pages

What Is Queue

CSE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

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.

FIFO Principle of Queue:


 A Queue is like a line waiting to purchase tickets, where the first person in line is the first person
served. (i.e. First come first serve).
 Position of the entry in a queue ready to be served, that is, the first entry that will be removed from
the queue, is called the front of the queue(sometimes, head of the queue), similarly, the position of
the last entry in the queue, that is, the one most recently added, is called the rear (or the tail) of the
queue. See the below figure
 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 an array representing the queue.

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.

Real-time application of Queue:


 ATM Booth Line
 Ticket Counter Line
 Key press sequence on the keyboard
 CPU task scheduling
 Waiting time of each customer at call centers.

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.

some real time senario for queue:

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.

Implementation of queue using array:


#include<stdio.h>

#define N 5

int queue[N];

int front=-1;

int rear=-1;

void enqueue(int x)

if(rear == N-1)

printf("overflow\n");

else if(front==-1 && rear==-1)

front=rear=0;

queue[rear]=x;

else

rear++;
queue[rear]=x;

void dequeue()

if(front==-1 && rear==-1)

printf("empty\n");

else if(front==rear)

front=rear=-1;

else

printf("%d\n",queue[front]);

front++;

void display()

int i;

if(front==-1 && rear==-1)

printf("empty\n");

else

for(i=front; i<=rear; i++)


{

printf("%d\n",queue[i]);

void peek()

if(front==-1 && rear==-1)

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;

Implementation of queue using linklist:


#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *next;

};

struct node *front=0;

struct node *rear=0;

void enqueue(int x)

struct node *newnode;

newnode=(struct node*)malloc(sizeof(struct node));

newnode->data=x;

newnode->next=0;

if(front==0 && rear==0)

front=rear=newnode;

else

rear->next=newnode;

rear=newnode;

void display()

struct node *temp;

if(front==0&&rear==0)
{

printf("empty");

else

temp=front;

while(temp!=0)

printf("%d\n",temp->data);

temp=temp->next;

void dequeue()

struct node *temp;

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;

void enqueue(int x, int priority)

{
Element newItem;

newItem.data = x;

newItem.priority = priority;

if(rear == N-1)

printf("overflow\n");

return;

else if(front==-1 && rear==-1)

front=rear=0;

queue[rear]=newItem;

else

int i;

for(i = rear; i >= front; i--) {

if(priority > queue[i].priority) {

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;

if(front==-1 && rear==-1)

printf("empty\n");

else

for(i=front; i<=rear; i++)

printf("%d (Priority: %d)\n",queue[i].data, queue[i].priority);

}
void peek()

if(front==-1 && rear==-1)

printf("empty\n");

else

printf("%d (Priority: %d)\n",queue[front].data, queue[front].priority);

int main()

enqueue(2, 1);

enqueue(5, 3);

enqueue(-1, 2);

display();

peek();

dequeue();

peek();

peek();

display();

return 0;

algorithm of queue using array:


Algorithm for Basic Queue using an Array

Initialize the Queue:


 Initialize the queue array of size N.
 Set front and rear to -1 to indicate an empty queue.
Enqueue Operation (enqueue(x)):
 Check if rear is at the end of the array (N-1). If true, print "overflow" as the queue is full.
 If the queue is empty (front and rear are both -1), set front and rear to 0 and insert
the element x at queue[rear].
 If the queue is not empty, increment rear and insert x at queue[rear].
Dequeue Operation (dequeue()):
 Check if the queue is empty (front and rear are both -1). If true, print "empty" as there
are no elements to dequeue.
 If there is only one element in the queue (front == rear), set front and rear to -1 to
indicate an empty queue.
 If there are more than one elements, print and return queue[front] (the element at the
front) and increment front to remove it.
Display Operation (display()):
 Check if the queue is empty (front and rear are both -1). If true, print "empty" as there
are no elements to display.
 Otherwise, iterate from front to rear and print each element in the queue.
Peek Operation (peek()):
 Check if the queue is empty (front and rear are both -1). If true, print "empty" as there
are no elements to peek.
 Otherwise, print queue[front] (the element at the front).
Main Function (main()):
 Call enqueue three times to insert elements 2, 5, and -1 into the queue.
 Call display() to display the elements in the queue.
 Call peek() to print the element at the front.
 Call dequeue() to dequeue the front element.
 Call peek() again to print the new element at the front.
 Call peek() once more to demonstrate the "empty" message when the queue is empty.
 Finally, call display() again to show the state of the queue after dequeuing.

Algorithm of queue using linklist:


Initialize the Queue:

Create pointers front and rear and set them to NULL to indicate an empty queue.

Enqueue Operation (enqueue(x)):

Allocate memory for a new node newnode.

Set newnode->data to x and newnode->next to NULL.

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.

Display Operation (display()):

Create a pointer temp and set it to front.


Check if the queue is empty (front == NULL && rear == NULL). If true, print "empty".

 If the queue is not empty, iterate through the queue:


 Print temp->data.
 Update temp to temp->next.
 Dequeue Operation (dequeue()):
 Create a pointer temp and set it to front.
 Check if the queue is empty (front == NULL && rear == NULL). If true, print "empty".
 If the queue is not empty:
 Print front->data.
 Update front to front->next.
 Free the memory allocated for temp.
Peek Operation (peek()):
 Check if the queue is empty (front == NULL && rear == NULL). If true, print "empty".
 If the queue is not empty, print front->data.
Main Function (main()):
 Call enqueue three times to insert elements 5, 0, and -3 into the queue.
 Call display() to display the elements in the queue.
 Call peek() to print the element at the front.
End of Program.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy