Implementación de las Operaciones de Colas con Listas Enlazadas

 


Ejemplo de Colas


#include<alloc.h>
struct nodo{
int dato;
struct nodo * sig;
};

struct nodo* crearNodo(int dato){
struct nodo * p;
p=(struct nodo*) malloc(sizeof (struct nodo) );
p->dato=dato;
p->sig=NULL;
return(p);
}
struct nodo* insertar(struct nodo *inicio, int dato){
struct nodo * p,* nuevo, *q;
nuevo=crearNodo(dato);
p=inicio;
while(p!=NULL){
q=p;
p=p->sig;
}
q->sig=nuevo;
return(inicio);
}

void mostrar(struct nodo* inicio){
struct nodo *aux;
clrscr();
if (!inicio) printf("Vacia");
else
{ aux=inicio;
do{ printf(" %d", aux->dato);
aux=aux->sig;
}while(aux);
}
getch();
}

struct nodo* borrar(struct nodo *inicio,int dato){
struct nodo *p, *q=NULL;
p=inicio;
while(p->dato!=dato && p!=NULL){
q=p;
p=p->sig;
}
if(q==NULL)
{ inicio=p->sig;
free(p);
}
else
if(p!=NULL)
{ q->sig=p->sig;
free(p);
}
else
{ printf("No encontrado"); getch();
}
return inicio;
}

void main()
{ struct nodo * inicio=NULL;
char op;
int x;
do{ clrscr();
puts("1) Insertar 2) Mostrar 3)Borrar 4) Salida");
printf("Opcion:[ ]\b\b"); op=getch();
switch(op){
case '1': clrscr(); printf("Dato: "); scanf("%d",&x);
if(!inicio)
inicio=crearNodo(x);
else
inicio=insertar(inicio,x);
break;
case '2': clrscr(); mostrar(inicio); break;
case '3': clrscr(); printf("Dato: ");scanf("%d",&x);
inicio=borrar(inicio,x);
break;
}
}while(op!='4');
}