#include<iostream.h>
#include<conio.h>
classsqueue
{
private:
squeue *prev,*next;
int data;
public :
void add(int);
void display(void);
void remove(void);
};
squeue *front;
squeue *rear;
voidsqueue::add(intnum)
{
squeue *temp;
if(front==NULL && rear==NULL)
{
front=rear=new squeue;
front->prev=NULL;
front->next=NULL;
front->data=num;
}
else
{
temp=front;
rear->next=new squeue;
rear->next->data=num;
rear->next->prev=rear;
rear->next->next=front;
rear=rear->next;
front->prev=rear;
}}
voidsqueue::display()
{
squeue *q=front;
if(front==NULL && rear==NULL)
{
cout<<"no queue";
}
else
{do
{
cout<<q->data<<"\t";
q=q->next;
}
while(q!=rear->next);
}}
voidsqueue::remove()
{
squeue *temp=front;
front=front->next;
front->prev=NULL;
cout<<endl<<temp->data<<" is
removed\n";
delete(temp);
display();
}
void main()
{
clrscr();
squeue s;
char c='y';
intopt,num;
while(c=='y')
{
cout<<"\n 1.add \n 2. display \nenter option: ";
cin>>opt;
switch(opt)
{
case 1:
cout<<"enter number in queue:";
cin>>num;
s.add(num);
cout<<"\ndo you want to continue:";
break;
case 2:
s.display();
cout<<"\ndo you want to continue:";
break;
case 3:
s.remove();
cout<<"\ndo you want to continue:";
break;
default:
cout<<"wrng selection";
cout<<"\ndo you want to continue:";
break;
}c=getch();
}getch();}
No comments:
Post a Comment