#include <iostream>
using namespace std;
struct nodo
{
int dato;
struct nodo *siguiente;
};
struct cola
{
nodo *cabeza;
nodo *talon;
};
void menu();
void encolar(struct cola &q, int valor);
int desencolar(struct cola &q);
void imprimirCola(struct cola q);
void vaciaCola(struct cola &q);
int main()
{
struct cola q;
q.cabeza = NULL;
q.talon = NULL;
int dato, x;
char opcion;
do{
menu();
cin >> opcion;
switch (opcion)
{
case '1':
cout << "\n NUMERO DE CARTAS: "; cin >> dato;
do {
if (dato > 5)
{
encolar(q, 5);
cout << "\n\n " << 5 << " Cartas en cola..." << endl;
dato -= 5;
}
else
{
encolar(q, dato);
cout << "\n\n " << dato << " Cartas en cola..." << endl;
dato = 0;
}
} while (dato > 0);
break;
case '2':
x = desencolar(q);
cout << "\n\n " << x << " Cartas desencoladas...\n\n";
break;
case '3':
cout << "\n\n COLA:";
if( q.cabeza != NULL)
imprimirCola(q);
else
cout << "\n\n Cola vacia...!" << endl;
break;
case '4':
vaciaCola(q);
cout << "\n\n Hecho...Cola vacia...!" << endl;
break;
case '5':
cout << "\n\n FIN DEL PROGRAMA" << endl;
vaciaCola(q);
break;
default:
cout << "\n\n OPCION NO VALIDA" << endl;
break;
}
cout << endl;
}while( opcion != '5' );
return 0;
}
void menu()
{
cout << "\n MENU PRINCIPAL"
<< "\n ============================="
<< "\n 1. ENCOLAR "
<< "\n 2. DESENCOLAR "
<< "\n 3. MOSTRAR COLA "
<< "\n 4. VACIAR COLA "
<< "\n 5. SALIR "
<< "\n ============================="
<< "\n OPCION (1-5): ";
}
void encolar(struct cola &q, int valor)
{
struct nodo *aux = new(struct nodo);
aux->dato = valor;
aux->siguiente = NULL;
if(q.cabeza == NULL)
q.cabeza = aux;
else
(q.talon)->siguiente = aux;
q.talon = aux;
}
int desencolar(struct cola &q)
{
int num ;
struct nodo *aux ;
aux = q.cabeza;
num = aux->dato;
q.cabeza = (q.cabeza)->siguiente;
delete(aux);
return num;
}
void imprimirCola(struct cola q)
{
struct nodo *aux;
aux = q.cabeza;
while(aux != NULL)
{
cout << " " << aux->dato;
aux = aux->siguiente;
if (aux != NULL)
cout << " <-";
}
}
void vaciaCola(struct cola &q)
{
struct nodo *aux;
while(q.cabeza != NULL)
{
aux = q.cabeza;
q.cabeza = aux->siguiente;
delete(aux);
}
q.cabeza = NULL;
q.talon = NULL;
}