#include<iostream.h>
#include<conio.h>
classsqueue
{
private:
squeue *prev,*next;
int data;
public :
voidaddrear(int);
void display(void);
voidremovefront(void);
voidaddfront(int);
voidremoverear(void);
};
squeue *front;
squeue *rear;
voidsqueue::addrear(intnum)
{
squeue *temp;
if(front==NULL && rear==NULL)
{
front=rear=new squeue;
front->prev=NULL;
front->next=NULL;
front->data=num;
}
else
{
rear->next=new squeue;
rear->next->data=num;
rear->next->prev=rear;
rear->next->next=NULL;
rear=rear->next;
voidsqueue::addfront(intnum)
{
squeue *temp=front;
if(front==NULL && rear==NULL)
{
front=rear=new squeue;
front->prev=NULL;
front->next=NULL;
front->data=num;
}
else
{
front=new squeue();
front->data=num;
front->prev=NULL;
front->next=temp;
temp->prev=front;
}}
voidsqueue::display()
{
squeue *q=front;
if(front==NULL && rear==NULL)
{
cout<<"no queue";
}
else
{
while(q!=NULL)
{
cout<<q->data<<"\t";
q=q->next;
}}}
voidsqueue::removefront()
{
squeue *temp=front;
front=front->next;
front->prev=NULL;
cout<<endl<<temp->data<<" is
removed\n";
delete(temp);
display();
}
voidsqueue::removerear()
{
squeue *temp=rear;
rear=rear->prev;
rear->next=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 R \n 2. display \n 3. remove F \n 4. add F \n 5. Remove R \n
enter option: ";
cin>>opt;
switch(opt)
{
case 1:
cout<<"enter number in queue:";
cin>>num;
s.addrear(num);
cout<<"\ndo you want to continue:";
break;
case 2:
s.display();
cout<<"\ndo you want to continue:";
break;
case 3:
s.removefront();
cout<<"\ndo you want to continue:";
break;
case 4:
cout<<"enter number in queue:";
cin>>num;
s.addfront(num);
cout<<"\ndo you want to continue:";
break;
case 5:
s.removerear();
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