Вы находитесь на странице: 1из 13

Experiment Title.

: Operations On Queue

Student Name: Harsh Bhardwaj UID: 19BCS1468


Branch: CSE Section/Group : 3(A)
Semester: 3 Date of Performance:
Subject Name: Data Structure Lab Subject Code: CSP-231

1. Aim/Overview of the practical:


Write a program the implementation of various operations on a linear queue and circular
queue represented using linear array.
2. Task to be done:
We have to write code in which we make linear queue and circular queue using linear
array and perform various operations.
3. Algorithm/Flowchart :
Enqueue In Linear Queue :

if queue is full

return overflow

end if

rear ← rear + 1

queue[rear] ← data

return true

end procedure

Dequeue in Linear Queue:

if queue is empty

return underflow
end if

data = queue[front]

front ← front + 1

return true

end procedure

Enqueue in Circular Queue :

Step 1: IF (REAR+1)%MAX = FRONT


Write " OVERFLOW "
Goto step 4
[End OF IF]

Step 2: IF FRONT = -1 and REAR = -1


SET FRONT = REAR = 0
ELSE IF REAR = MAX - 1 and FRONT ! = 0
SET REAR = 0
ELSE
SET REAR = (REAR + 1) % MAX
[END OF IF]

Step 3: SET QUEUE[REAR] = VAL

Step 4: EXIT

Dequeue in Circular Queue :

Step 1: IF FRONT = -1
Write " UNDERFLOW "
Goto Step 4
[END of IF]

Step 2: SET VAL = QUEUE[FRONT]

Step 3: IF FRONT = REAR


SET FRONT = REAR = -1
ELSE
IF FRONT = MAX -1
SET FRONT = 0
ELSE
SET FRONT = FRONT + 1
[END of IF]
[END OF IF]

Step 4: EXIT

4. Code for experiment/practical:


Linear Queue :

#include<iostream>
using namespace std;

class Queue
{
int front,rear,q[50];

public:
Queue()
{
front=rear=0;
}

bool enqueue(int k)
{
if(rear>49)
{
cout<<"Over Flow !!!"<<endl;
return false;
}

else
{
q[rear]=k;
rear++;
return true;
}
}

bool dequeue()
{
if(front == rear)
{
cout<<"Under Flow !!!"<<endl;
return false;
}
else
{
int *d=&q[front];
front=front+1;
delete(d);
return true;
}
}
void peek()
{
if(front == rear)
{
cout<<"Queue is Empty !!!"<<endl;
}
else
{
cout<<q[front]<<endl;
}
}

void display()
{
for(int i=front;i<rear;i++)
{
cout<<q[i]<<" ";
}
cout<<endl;
}

};

int main()
{
Queue o;
cout<<"Enter number of elements of queue:"<<endl;
int n;
cin>>n;
bool b;
cout<<"Enter elements of queue:"<<endl;
for(int i=0;i<n;i++)
{
int k;
cin>>k;
b=o.enqueue(k);
if(b==false)
break;
}
cout<<"Queue is:"<<endl;
o.display();

if(b==true)
{
cout<<"Top of queue is:"<<endl;
o.peek();

cout<<"Enter number of elements to delete:"<<endl;


int d;
bool c;
cin>>d;
for(int i=0;i<d;i++)
{
c=o.dequeue();
if(c==false)
break;
}

if(c==true)
{
cout<<"Queue after deletion is:"<<endl;
o.display();

cout<<"Top of queue after deletion is:"<<endl;


o.peek();
}
}
}

Circular Queue :
#include<iostream>
using namespace std;

class CircularQueue
{
int front,rear,*q,size;
public:

CircularQueue(int s)
{
q=new int[s];
front=-1;
rear=-1;
size=s;
}

bool enqueue(int k)
{
if((front == 0 && rear == size-1) || front==rear+1)
{
cout<<"Over Flow !!!"<<endl;
return false;
}
if(front == -1)
{
front = 0;
rear = 0;
q[rear]=k;
return true;
}
else if(rear==size-1)
{
rear=0;
q[rear]=k;
return true;
}
else
{
rear++;
q[rear]=k;
return true;
}
}

bool dequeue()
{
if(front==-1)
{
cout<<"Under Flow !!!"<<endl;
return false;
}
if(front==rear)
{
front=rear=-1;
return false;
}
else if(front==size-1)
{
front=0;
return true;
}
else
{
front=front+1;
return true;
}
}

void peek()
{
if(front==-1)
{
cout<<"Under Flow !!!"<<endl;
}
else
{
cout<<q[front]<<endl;
}
}

void display()
{
int f=front,r=rear;
if(f<=r)
{
while(f<=r)
{
cout<<q[f]<<" ";
f++;
}
cout<<endl;
}
else
{
while(f<=size-1)
{
cout<<q[f]<<" ";
f++;
}
f=0;
while(f<=r)
{
cout<<q[f]<<" ";
f++;
}
cout<<endl;
}
}
};

int main()
{
cout<<"Enter size of circular queue :"<<endl;
int s;
cin>>s;
CircularQueue o(s);

begin:
cout<<"Enter number of elements to add in queue :"<<endl;
int n;
bool b;
cin>>n;
cout<<"Enter elemenst :"<<endl;
for(int i=0;i<n;i++)
{
int k;
cin>>k;
b=o.enqueue(k);
if(b==false)
break;
}

cout<<"Queue is :"<<endl;
o.display();

if(b==true)
{
cout<<"Top of the Queue is :"<<endl;
o.peek();

cout<<"Enter number of elements you want to delete :"<<endl;


int d;
bool c;
cin>>d;
for(int i=0;i<d;i++)
{
c=o.dequeue();
if(c==false)
break;
}

if(c==true)
{
cout<<"Circular Queue after deletion is :"<<endl;
o.display();

cout<<"Top of the Queue is :"<<endl;


o.peek();
}

cout<<"If you want to add element enter 1 otherwise enter 2"<<endl;


int z;
cin>>z;

if(z==1)
{
goto begin;
}
}

5.Discussion /Observation/Complexity :

A linear queue can be found in a time-sharing computer system where many users share the
system simultaneously. The first element, which is added into the queue will be the first one to
be removed. Thus queues are also called First-in First-Out lists (FIFO) or Last-In-Last-Out lists
(LILO).Circular Queue is a linear data structure in which the operations are performed based on
FIFO (First In First Out) principle and the last position is connected back to the first position to
make a circle.

6.Result/Output/Writing Summary:

Linear Queue :

Circular Queue :
Learning outcomes (What I have learnt):

1. Learn about Linear Queue.

2. Learn about Circular Queue.

3. Learn about Linear Array.

Evaluation Grid:

Sr. No. Parameters Marks Obtained Maximum Marks


1. Demonstration and Performance 5
(Pre Lab Quiz)
2. Worksheet 10
3. Post Lab Quiz 5

Вам также может понравиться