L'errore grosso sta nella push, perche' tu modifichi un puntatore locale alla funzione, fuori da essa la modifica viene persa. Per farlo come vuoi fare tu dovresti passare l'indirizzo di quel puntatore, e quindi modificarlo tramite esso.
Prova a dare un'occhiata a questa versione... visto che anche io sono abbastanza indietro con il C sono ber gradite critiche/suggerimenti, ecc...
codice:#include <stdio.h> #include <stdlib.h> struct dato { int elemento; struct dato * punt; }; struct stack { struct dato * last; }; struct stack * create(struct stack **); void push (struct stack *, int); struct dato * pop (struct stack *); void destroy (struct stack *); int main () { int a; struct stack * s1; struct dato * d; create (&s1); push (s1, 10); push (s1, 20); push (s1, 30); d = pop (s1); while(d) { a = d -> elemento; printf ("%d \n", a); free(d); d = pop (s1); } destroy(s1); return (0); } struct stack * create (struct stack ** s) { *s = (struct stack *) malloc (sizeof (struct stack)); if(*s) { (*s) -> last = NULL; } return *s; } void push (struct stack * s, int n) { struct dato * d = (struct dato *) malloc (sizeof (struct dato)); if(d) { d -> punt = s -> last; d -> elemento = n; s -> last = d; } } struct dato * pop (struct stack * s) { struct dato * tmp = s -> last; if(tmp) { s -> last = s -> last -> punt; } return tmp; } void destroy (struct stack * s) { if(s == NULL) return; while(s -> last) { struct dato * tmp = s -> last; if(tmp) { s -> last = s -> last -> punt; free(tmp); } } free(s); }

Rispondi quotando