DS Lab 7
DS Lab 7
DS Lab 7
: Operations On Queue
if queue is full
return overflow
end if
rear ← rear + 1
queue[rear] ← data
return true
end procedure
if queue is empty
return underflow
end if
data = queue[front]
front ← front + 1
return true
end procedure
Step 4: EXIT
Step 1: IF FRONT = -1
Write " UNDERFLOW "
Goto Step 4
[END of IF]
Step 4: EXIT
#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();
if(c==true)
{
cout<<"Queue after deletion is:"<<endl;
o.display();
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();
if(c==true)
{
cout<<"Circular Queue after deletion is :"<<endl;
o.display();
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):
Evaluation Grid: