Search This Blog

Wednesday, 7 November 2012

Implement add,delete operations of a queue using arrays

#include<iostream.h>
#include<conio.h>
class queue
{
int a[10],front,rear,n;
public:
void head();
void add();
void del();
void view();
};
void queue::head()
{
rear=0;
front=0;
cout<<"implementation of queue using array"<<endl;
cout<<"**********************************"<<endl;
cout<<"1.add"<<endl;
cout<<"2.del"<<endl;
cout<<"3.view"<<endl;
cout<<"4.exit"<<endl;
cout<<"enter the value of n:";
cin>>n;
}
void queue::add()
{
if(rear>=n)
{
cout<<"queue is full"<<endl;
}
else
{
cout<<"enter the num:";
cin>>a[rear];
rear++;
}
}
void queue::del()
{
if(rear==front)
{
cout<<"queue is empty"<<endl;
}
else
{
cout<<"deleted elements is:"<<a[front]<<endl;
front++;
}
}
void queue::view()
{
cout<<"the result is:"<<endl;
for(int j=front;j<rear;j++)
{
cout<<a[j]<<endl;
}
}
void main()
{
int a;
queue q;
clrscr();
q.head();
do
{
cout<<"enter your choice:";
cin>>a;
switch(a)
{
case 1:
q.add();
break;
case 2:
q.del();
break;
case 3:
q.view();
break;
case 4:
cout<<"exit"<<endl;
break;
}
}
while(a!=4);
getch();
}
 

Output:
1.add
2.del
3.view
4.exit
enter the value of n:2
enter your choice:1
enter the num:10
enter your choice:1
enter the num:20
enter your choice:1
queue is full
enter your choice:2
the result is:
10 

20
enter your choice:2
deleted elements is:10
enter your choice:2
deleted elements is :20
enter your choice:2
queue is empty
enter your choice:4
exit
 

3 comments: