Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

QUEUING INSERTION

ALGORITHM
GROUP 6

QUEUING INSERTION ALGORITHM

• Queue is an abstract data structure or
linear data structure which is open at
both ends. One end is used for the
insertion of data (This is called
Enqueue which is our major topic of
discussion) and the other end is used
to remove data (dequeue).
Queue follows a first-in-first-out
methodology also known as FIFO
where the data item stored first will
be accessed first.

Real life representation of queue –


Cars held up in a one-way road,
People queuing for an exam or to
buy tickets.
Insertion occurs from one end
called the rear and the removal of
existing elements takes place from
the other end called the front.
STEPS TO INSERT DATA IN A QUEUE

STEP 1: Check if the queue is full.

STEP 2: If the queue is full produce overflow error and


exit.

STEP 3: If the queue is not full, increment rear pointer


to point the next empty space.

STEP 4: Add data element to the queue location


where rear is pointing.
FRONT

-1 A

0 1 2 3 4
REA
R

FRONT

0 1 2 3 4
FRONT
REA
R
A 3 5 7 8 9

0 1 2 3 4

REA
R
IMPLEMENTATION OF THE ALGORITHM USING C++
#include<iostream>
using namespace std;
#define size 5 f(1) = 1
int main(){
Int queue[size]; f(1) = 1
Int front = -1; f(1 ) = 1
Int rear = -1; f(1) = 1
Int value;
f(1) = 1
while (rear < size – 1) f(n ) = n
{
cout<<“Insert Value in the queue\n”; f(1*n) = n
cin>>value; f(1*n) = n
if(front==-1) f(1*n) = n
front = 0; f(1*n) = n
rear++; f(1*n) = n
queue[rear]=value; f(1*n) = n
}
if (rear==size-1) f(1) = 1
cout<<“Queue is full\n”;
return 0 ; f(1*1) = 1
f(1) = 1
}
COMPLEXITY ANALYSIS

f(n) = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + n + n + n + n + n + n + n
= 8 + 7n
= 7n
Big O = O(n)
APPLICATION OF QUEUE IN COMPUTER
SCIENCE

1.Serving request on a single shared


resource like a printer (it prints files in the
order it was sent in).

2.CPU task scheduling.

3.Handling interrupts in real time systems,


the interrupts are handled in the order that
they arrive.

You might also like