C++ program for circular queue using arrays. It performs the basic operations such as insertion, deletion elements
C++ program to implement circular queue
#include<iostream>
using namespace std;
#define SIZE 10
class impcqueue
{
int a[SIZE],front,rear;
public:
impcqueue()
{
front=-1,rear=0;
}
void insert()
{
if(front==(rear+1)%SIZE)
cout<<"Overflow";
else
{
if(front==-1)
front=0,rear=-1;
rear=(rear+1)%SIZE;
cout<<"Enter the element to be inserted ";
cin>>a[rear];
}
}
void delet()
{
if(front<0)
cout<<"Empty"<<endl;
else
{
cout<<"Deleted element is "<<a[front]<<endl;
front=(front+1)%SIZE;
if(front==(rear+1)%SIZE)
front=-1,rear=0;
}
}
void display()
{
if(front<0)
cout<<"Empty"<<endl;
else
{
int f=front;
while(f!=rear)
{
cout<<a[f]<<" ";
f=(f+1)%SIZE;
}
cout<<a[rear]<<" "<<endl;
}
}
};
int main()
{
int ch,flag=1;
impcqueue queue;
while(flag)
{
cout<<"Enter your choice\n1.)Insert\n2.)Delete\n3.)Display\n4.)Exit\nEnter your choice ";
cin>>ch;
switch(ch)
{
case 1:
queue.insert();
break;
case 2:
queue.delet();
break;
case 3:
queue.display();
break;
case 4:
flag=0;
break;
default:
cout<<"Wrong choice";
}
}
}
using namespace std;
#define SIZE 10
class impcqueue
{
int a[SIZE],front,rear;
public:
impcqueue()
{
front=-1,rear=0;
}
void insert()
{
if(front==(rear+1)%SIZE)
cout<<"Overflow";
else
{
if(front==-1)
front=0,rear=-1;
rear=(rear+1)%SIZE;
cout<<"Enter the element to be inserted ";
cin>>a[rear];
}
}
void delet()
{
if(front<0)
cout<<"Empty"<<endl;
else
{
cout<<"Deleted element is "<<a[front]<<endl;
front=(front+1)%SIZE;
if(front==(rear+1)%SIZE)
front=-1,rear=0;
}
}
void display()
{
if(front<0)
cout<<"Empty"<<endl;
else
{
int f=front;
while(f!=rear)
{
cout<<a[f]<<" ";
f=(f+1)%SIZE;
}
cout<<a[rear]<<" "<<endl;
}
}
};
int main()
{
int ch,flag=1;
impcqueue queue;
while(flag)
{
cout<<"Enter your choice\n1.)Insert\n2.)Delete\n3.)Display\n4.)Exit\nEnter your choice ";
cin>>ch;
switch(ch)
{
case 1:
queue.insert();
break;
case 2:
queue.delet();
break;
case 3:
queue.display();
break;
case 4:
flag=0;
break;
default:
cout<<"Wrong choice";
}
}
}
No comments:
Post a Comment