// EJEMPLO DE PILA
// DADO UN CONJUNTO DE PALABRAS COMO ARGUMENTO,
// SE GUARDARAN EN UNA PILA FINALIZANDO EL PROGRAMA
// MOSTRANDO EL CONTENIDO DE LA PILA
// se hace uso del pop() para leer y sacar de la pila
// y del push() para guardar en la pila
// OJO: en una pila, los datos se meten y se sacan por arriba
// el último en entrar es el primero en salir
// LIFO ... Last In -- First Out
// =======================================================
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct nodo
{
char *dato;
struct nodo *sig;
};
struct nodo *Inicio=NULL; // TIENE QUE SER GLOBAL e inicia apuntando a NULL
void EnPila(struct nodo *new)
{
if ( !Inicio ) // SI NO EXISTES???
Inicio = new;
else
{
new->sig = Inicio;
Inicio = new;
}
}
void guarda(int n, char **a)
{
struct nodo *nuevo = (struct nodo*)malloc(sizeof(struct nodo));
nuevo->sig = NULL;
nuevo->dato = (char*)malloc(sizeof(char)*80);
if ( !n ) return;
strcpy(nuevo->dato, a[n]);
EnPila(nuevo);
n--;
guarda(n, a);
}
void muestra(struct nodo *aux)
{
if ( !aux ) return;
printf("%s\n", aux->dato);
aux = aux->sig;
muestra(aux);
}
int nargs(int x) { return x>1; }
int main(int x, char **a)
{
if ( nargs(x) )
{
guarda(x-1, a);
muestra(Inicio);
}
else printf("Npoo hay argumentos suficientes ...\n");
}