Circular Queue Using Array.

/*
Circular Queue using Array.
*/
#include<stdlib.h>
#include<iostream.h>
#include<conio.h>

class queue
{
private:
int queue_element[5];
int Front,Rear;
public:
queue();
int overflow();
int underflow();
void insert(int);
int deletion();
};

queue::queue()
{
Front=-1;
Rear=-1;
}
int queue::overflow()
{
if((Rear+1)%4==Front)
{
cout<<“Overflow”;
return(1);
}
else
return(-1);
}
int queue::underflow()
{
if(Front==-1 && Rear==-1)
{
cout<<“Underflow”;
return(1);
}
else
return(-1);
}
void queue::insert(int info)
{
if(overflow()==-1)
{
if(Rear==4)
Rear=0;
else
Rear+=1;

queue_element[Rear]=info;
if(Front==-1)
{
Front=0;
}
}

}
int queue::deletion()
{
int info=0;
if(underflow()==-1)
{
info=queue_element[Front];
if(Front==Rear)
{
Front=-1;
Rear=-1;
}
else
{
if(Front==4)
{
Front=0;
}
else
{
Front+=1;
}
}
}

return(info);
}
int main()
{
queue q;
int num,c;
char s;
clrscr();
do
{
cout<<“\nCircular Queue Options:\n”;
cout<<“1.To Insert an Element.\n”;
cout<<“2.To Delete an Element.\n”;
cout<<“\nPlease make your choice: “;
cin>>c;
switch(c)
{
case 1: cout<<“\nEnter the Element to insert: “;
cin>>num;
q.insert(num);
break;
case 2: num=q.deletion();
cout<<“\nDeleted Element is: ” <<num;
break;
default: cout<<“\nInvalid Selection”;
};

cout<<“\nDo you want to continue(y/n): “;
cin>>s;
}while(s==’Y’||s==’y’);
exit(1);
getch();
return(0);
}
/*
OUTPUT:
——-
Circular Queue Options:
1.To Insert an Element.
2.To Delete an Element.

Please make your choice: 1

Enter the Element to insert: 5

Do you want to continue(y/n): y

Circular Queue Options:
1.To Insert an Element.
2.To Delete an Element.

Please make your choice: 1

Enter the Element to insert: 18

Do you want to continue(y/n): n
*/

Share

About the Author

Akash Padhiyar

Visit Website

There are no comments yet, add one below.

Leave a Comment

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.