Ejemplo
de Pila
#include <alloc.h>
struct nodo
{int dato;
struct nodo * ant;
};
int vacia(struct nodo *tope) { return (!tope)? 1:0; }
struct nodo * crearnodo(int dato)
{ struct nodo* p;
p=(struct nodo*)malloc (sizeof (p));
p->dato=dato;
p->ant=NULL;
return(p);
}
struct nodo* push(struct nodo * tope,int dato)
{ struct nodo *aux;
aux=crearnodo(dato);
aux->ant=tope;
tope=aux;
return(tope);
}
void mostrar(struct nodo *tope)
{ struct nodo *aux;
clrscr();
if(!tope) printf("Esta vacia");
else
{ aux=tope;
do{
printf("
%d",aux->dato);
aux=aux->ant;
}while(aux!= NULL);
}
getch();
}
struct nodo* pop(struct nodo *tope,int* valor)
{ struct nodo *aux;
int dato;
aux=tope;
*valor=tope->dato;
tope=tope->ant;
free(aux);
return(tope);
}
void
main()
{struct nodo *tope=NULL;
char opc;
int dato,ban;
do{ clrscr ();
gotoxy(27,8); printf(" 1. Push");
gotoxy(27,10); printf(" 2. Pop");
gotoxy(27,12); printf(" 3. Mostrar Pila");
gotoxy(27,14); printf(" 4. Salir");
gotoxy(27,16); printf("Opcion: [ ]\b\b");
opc=getche();
switch(opc)
{ case '1':clrscr();
printf("Escribe dato: ");
scanf("%d",&dato);
if(tope==NULL)
tope=crearnodo(dato);
else
tope=push(tope,dato);
break;
case '2': clrscr();
if(!vacia(tope)) {
tope=pop(tope,&dato);
printf("El dato en la cima es: %d",dato);
}
else printf("Pila Vacia");
getch(); break;
case '3': if (!vacia(tope) )
mostrar(tope);
else printf("Pila Vacia");
break;
case '4': break;
}
}while(opc!='4');
}
|