/* Autor: Juan Antonio Pastor Gallardo japgallardo@hotmail.com jantoniocolme@terra.es Con este programa se muestran tres operaciones de gestion sobre una pila */ #include #include #include typedef struct lista{ int dato; struct lista* sig; }LISTA;//definicion de elemento lista void Vaciar(LISTA** l); int EstaVacia(LISTA** l); void Insertar(LISTA** l,int d); void Sacar(LISTA** l); void Mostrar(LISTA** l); void main(void){ LISTA* lista; Vaciar(&lista); int salir=1,opcion,numero; do{ clrscr(); printf("\nMENU DE OPERACIONES DE LA PILA"); printf("\n------------------------------"); printf("\n1.-Insertar"); printf("\n2.-Mostar"); printf("\n3.-Sacar"); printf("\n4.-Salir"); printf("\nOpcion: "); scanf("%d",&opcion); switch(opcion) { case 1: printf("\nIntroduzca el numero: "); scanf("%d",&numero); Insertar(&lista,numero); break; case 2: Mostrar(&lista); break; case 3: Sacar(&lista); break; case 4: salir=0; getch(); break; default: printf("\nOpcion incorrecta"); } }while(salir); getch(); } void Vaciar(LISTA** l){ *l=NULL; } int EstaVacia(LISTA** l){//1 si esta vaicia int aux=0; if(NULL==*l) aux=1; else aux=0; return aux; } void Insertar(LISTA** l,int d){ LISTA *nuevo; //reserva memo nuevo=(LISTA*)malloc(sizeof(LISTA)); if(NULL==nuevo){ perror("malloc: "); exit(0); getch(); } //mete datos nuevo->dato=d; nuevo->sig=NULL; //si lista==NULL if(EstaVacia(l)) *l=nuevo; else { nuevo->sig=*l; *l=nuevo; //nuevo->sig->sig=NULL; } } void Mostrar(LISTA** l){ if(EstaVacia(l)){ printf("\nNo puedo mostar,lista esta vacia"); getch(); } else { printf("\nMostrando dato: %d",(*l)->dato); getch(); } } void Sacar(LISTA** l){ LISTA* aux=NULL; if(EstaVacia(l)){ printf("\nNo puedo borrar dato coleguita, pues la lista esta vacia"); getch(); } else{ printf("El dato borrado es: %d",(*l)->dato); getch(); aux=*l; *l=(*l)->sig; free(aux); } }