Mohi Q PR
Mohi Q PR
Roll no: 80
Assignment no: 1(B)
Title: Implementation of Queue using Array.
________________________________________________________________
#include<iostream.h>
#include<conio.h>
class QUEUE
{
int A[6],size,front,rear;
public:
QUEUE();
void ADD(int);
int DELETE();
void VIEW_ALL();
};
QUEUE::QUEUE()
{
size=5;
front=0;
rear=0;
}
void QUEUE::ADD(int ele)
{
if(rear==size)
cout<<"\nQUEUE is full";
else
{
if(front==0)
front=1;
rear=rear+1;
A[rear]=ele;
}
}
int QUEUE::DELETE()
{
int ele;
if(front==0) //when queue is empty
{
cout<<"\nQUEUE is empty";
return NULL;
}
else
{
ele=A[front];
if(front==rear) //when only one element in the q
front=rear=0;
else //otherwise
front=front+1;
return ele;
}
}
void QUEUE:: VIEW_ALL()
{
if(front==0)
cout<<"\nQueue is empty";
else
for(int i=front;i<=rear;i++)
cout<<A[i]<<" ";
}
void MENU()
{
int option,ele;
QUEUE obj;
do
{
cout<<"\n select the option : ";
cout<<"\n 1 ADD ";
cout<<"\n 2 DELETE ";
cout<<"\n 3 VIEW ALL ";
cout<<"\n 4 EXIT ";
cout<<"\n Enter your option : ";
cin>>option;
switch(option)
{
case 1:
cout<<"\n enter an element in add ";
cin>>ele;
obj.ADD(ele);
break;
case 2:
ele=obj.DELETE();
if(ele!=NULL)
cout<<endl<<ele<<"deleted:";
break;
case 3:
cout<<endl<<"The Queue elements are";
obj.VIEW_ALL();
break;
case 4:
return;
default:
cout<<endl<<"Invalid Option" ;
}
}while(1);
}
void main()
{
clrscr();
MENU();
getch();
}