#include typedef struct _node { char element; struct _node *next; } node; typedef node *stack; stack init () { stack S; /* Create the dummy node */ S = (node *)malloc(sizeof(node)); S -> element = '\0'; S -> next = NULL; return S; } int isEmpty ( stack S ) { return (S -> next == NULL); } int isFull ( stack S ) { return 0; } char top ( stack S ) { if (isEmpty(S)) { fprintf(stderr, "top: Empty stack\n"); return '\0'; } return S -> next -> element; } stack push ( stack S , char ch ) { node *T; if (isFull(S)) { fprintf(stderr, "push: Full stack\n"); return S; } /* Copy the new element in the dummy node */ S -> element = ch; /* Create a new dummy node */ T = (node *)malloc(sizeof(node)); T -> element = '\0'; T -> next = S; return T; } stack pop ( stack S ) { if (isEmpty(S)) { fprintf(stderr, "pop: Empty stack\n"); return S; } /* Treat the stack top as the new dummy node */ S -> next -> element = '\0'; return S -> next; } void print ( stack S ) { node *T; T = S -> next; while (T != NULL) { printf("%c", T -> element); T = T -> next; } } int main () { stack S; S = init(); printf("Current stack : "); print(S); printf(" with top = %c.\n", top(S)); S = push(S,'d'); printf("Current stack : "); print(S); printf(" with top = %c.\n", top(S)); S = push(S,'f'); printf("Current stack : "); print(S); printf(" with top = %c.\n", top(S)); S = push(S,'a'); printf("Current stack : "); print(S); printf(" with top = %c.\n", top(S)); S = pop(S); printf("Current stack : "); print(S); printf(" with top = %c.\n", top(S)); S = push(S,'x'); printf("Current stack : "); print(S); printf(" with top = %c.\n", top(S)); S = pop(S); printf("Current stack : "); print(S); printf(" with top = %c.\n", top(S)); S = pop(S); printf("Current stack : "); print(S); printf(" with top = %c.\n", top(S)); S = pop(S); printf("Current stack : "); print(S); printf(" with top = %c.\n", top(S)); S = pop(S); printf("Current stack : "); print(S); printf(" with top = %c.\n", top(S)); }