Program to Implement queue using Array in c++

  • by

/* Program to implement queue using array in c++*/
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#define MAX 20
class queue
{
int q[MAX];
int front,rear;
public:
queue()
{
front=-1;
rear=-1;
}

void display();
void insert(int x);
int delete_q();
};
void queue:: display()
{
int i;
for(i=front;i<=rear;i++)
cout<<q[i]<<“\t”;
}
void queue:: insert(int x)
{
if(rear==MAX-1)
{
cout<<“\n queue full “;
return;
}
else
{
if(front==-1)
front=0;
rear++;
q[rear]=x;
}
}
int queue:: delete_q()
{
int ele;
if(front==-1|| front>rear)
{
cout<<“\n queue empty “;
ele=0;
return ele;
}
else
{
ele=q[front];
front++;
return ele;
}
}
void main()
{
int ch;
queue obj;
int ele;
clrscr();
do
{
cout<<“\n1.insert \n2.Delete \n3.Display n4.Exit \n”;
cout<<“\n enter Choice “;
cin>>ch;
switch(ch)
{
case 1:
cout<<“\n enter Data “;
cin>>ele;
obj.insert(ele);
break;
case 2:
ele=obj.delete_q();
cout<<“\n ele= “<<ele;
break;
case 3:
obj.display();
break;

}
}while(ch!=4);
}

Leave a Reply