Ejemplo: cola circular usando una lista vinculada en c ++
#include<iostream>
#define SIZE 100
using namespace std;
class node
{
public:
node()
{
next = NULL;
}
int data;
node *next;
}*front=NULL,*rear=NULL,*n,*temp,*temp1;
class cqueue
{
public:
void insertion();
void deletion();
void display();
};
int main()
{
cqueue cqobj;
int ch;
do
{
cout<<"nntMain Menu";
cout<<"n##########################";
cout<<"n1. Insertn2. Deleten3. Displayn4. ExitnnEnter Your Choice: ";
cin>>ch;
switch(ch)
{
case 1:
cqobj.insertion();
cqobj.display();
break;
case 2:
cqobj.deletion();
break;
case 3:
cqobj.display();
break;
case 4:
break;
default:
cout<<"nnWrong Choice!!! Try Again.";
}
}while(ch!=4);
return 0;
}
void cqueue::insertion()
{
n=new node[sizeof(node)];
cout<<"nEnter the Element: ";
cin>>n->data;
if(front==NULL)
{
front=n;
}
else
{
rear->next=n;
}
rear=n;
rear->next=front;
}
void cqueue::deletion()
{
int x;
temp=front;
if(front==NULL)
{
cout<<"nCircular Queue Empty!!!";
}
else
{
if(front==rear)
{
x=front->data;
delete(temp);
front=NULL;
rear=NULL;
}
else
{
x=temp->data;
front=front->next;
rear->next=front;
delete(temp);
}
cout<<"nElement "<<x<<" is Deleted";
display();
}
}
void cqueue::display()
{
temp=front;
temp1=NULL;
if(front==NULL)
{
cout<<"nnCircular Queue Empty!!!";
}
else
{
cout<<"nnCircular Queue Elements are:nn";
while(temp!=temp1)
{
cout<<temp->data<<" ";
temp=temp->next;
temp1=front;
}
}
}
¡Haz clic para puntuar esta entrada!
(Votos: 1 Promedio: 5)