#include #include #define OK -8888 #define STACKFULL -7777 #define STACKEMPTY -6666 typedef struct node* Nodeptr; typedef struct node { int val; Nodeptr prev; } Node ; void clearstack (Nodeptr *top) { Nodeptr np; while (*top != NULL) { np = *top; *top = (*top)->prev; free (np); } } int push (int i, Nodeptr *top) { Nodeptr np; np = (Nodeptr) malloc (sizeof (Node)); /* sizeof (struct node) */ if (np == NULL) return STACKFULL; else { np->val = i; np->prev = *top; *top = np; } ; return OK; } int pop (Nodeptr *top) { Nodeptr np; int data; if (*top == NULL) return STACKEMPTY; else { data = (*top)->val; np = *top; *top = (*top)->prev; free (np); } ; return data; } int stacksize (Nodeptr top) { Nodeptr np; int size = 0; for (np = top; np; np = np->prev) size++; return size; } void printstack (Nodeptr top) { Nodeptr np; printf ("stack:"); for (np = top; np; np = np->prev) printf ("%d ", np->val); printf("\n"); } void main (void) { Nodeptr top = NULL; int i; while (scanf ("%d", &i) != EOF) { printf ("%d ", i); if (push (i, &top)) break; } printstack (top); clearstack (&top); printf ("All done: %d\n", (int) top); }