ROGRAMA - EJEMPLO DE COLA DE DATOS
ghjghjhApuntes9 de Noviembre de 2015
1.050 Palabras (5 Páginas)218 Visitas
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
struct nodo
{
int dato;
struct nodo *sig;
};
struct cola
{
nodo *frente;
nodo *atras ;
};
void cabecera(void) {
int i;
printf("\t\t|");
for(i = 0; i < 50; i++) printf("°");
printf("|\n\t\t|");
printf("\t\tEJEMPLO DE COLA DE DATOS\t |\n\t\t|");
printf("\t\tBy: Ricardo Flores Mejia\t |\n");
printf("\t\t|");
for(i = 0; i < 50; i++) printf("°");
printf("|\n");
return;
}
void encolar( struct cola &q, int valor )
{
struct nodo *base = new(struct nodo);
base->dato = valor;
base->sig = NULL;
if( q.frente == NULL)
q.frente = base; // encola el primero elemento
else
(q.atras)->sig = base;
q.atras = base; // apunta al ultimo elemento
}
int desencolar( struct cola &q )
{
int num ;
struct nodo *base ;
base = q.frente; // apunta al inicio de la cola
num = base->dato;
q.frente = (q.frente)->sig;
delete(base); // libera memoria base
return num;
}
void muestraCola( struct cola q )
{
struct nodo *base;
base = q.frente;
printf("\n\tELEMENTOS EN COLA\n\n");
printf("INICIO");
while( base != NULL )
{
printf(" <-- [%d]",base->dato);
base = base->sig;
}
printf(" <-- FIN");
printf("\n\n");
}
void vaciaCola( struct cola &q)
{
struct nodo *base;
while( q.frente != NULL)
{
base = q.frente;
q.frente = base->sig;
delete(base);
}
q.frente = NULL;
q.atras = NULL;
}
int menu(void) {
int op;
system("cls");
cabecera();
printf("\n\t1. Insertar elemento\n");
printf("\t2. Retirar elemento\n");
printf("\t3. Imprimir cola de datos\n");
printf("\t4. Vaciar cola de datos\n");
printf("\t5. Salir\n\n");
printf(" Selecciona una opcion --> ");
scanf("%d", &op);
return (op);
}
int main()
{
struct cola q;
q.frente = NULL;
q.atras = NULL;
int dato, opc, x, num, cont;
system("color 2");
do
{
opc=menu();
switch(opc)
{
case 1:
printf("\n\tCuantos datos vas a ingresar?\n\t ");
scanf("%d",&num);
for(cont=0;cont<num;cont++){
printf("\n\tDame el dato a ingresar --> ");
...