Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C++ program to implement a Queue Class with structure variables as its attributes:
#include<iostream>
#define maxi 1000
using namespace std;
struct strqueue{
int a[maxi];
int f,r;
};
class qqueue{
strqueue q;
public:
void initialize();
void enqueue(int val);
void dequeue();
void display();
};
void qqueue::initialize(){
cout<<"Empty Queue is created :D";
q.f=q.r=-1;
}
void qqueue::enqueue(int val){
if(q.r==maxi-1){
cout<<"\nQueue Overflow..";
}
else if(q.f==-1 && q.r==-1){
q.f=q.r=1;
q.a[q.r]=val;
}
else{
q.r++;
q.a[q.r]=val;
}
}
void qqueue::dequeue(){
if(q.f==-1 && q.r==-1){
cout<<"\nQueue is already empty..";
}
else if(q.f==q.r){
cout<<"\nThe dequeued element is.."<<q.a[q.f];
q.f=q.r=-1;
}
else{
cout<<"\nThe dequeued element is.."<<q.a[q.f];
q.f++;
}
}
void qqueue::display(){
int x;
if(q.f==-1 && q.r==-1){
cout<<"\nThe Queue is empty..";
return;
}
cout<<"\nThe Contents of the Queue is..";
for(x=q.f;x<=q.r;x++){
cout<<q.a[x]<<" ";
}
}
int main()
{
qqueue q;
q.initialize();
int ch,val;
cout<<"\nChoice: \n1)Enqueue \n2)Dequeue \n3)Display \n4)Exit";
while(1){
cout<<"\nEnter your choice..";
cin>>ch;
if(ch==1){
cout<<"\nEnter the element to be enqueued..";
cin>>val;
q.enqueue(val);
}
else if(ch==2){
q.dequeue();
}
else if(ch==3){
q.display();
}
else{
break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment