#include #define MAXLEN 100 typedef struct { char element[MAXLEN]; int top; } stack; stack init () { stack S; S.top = -1; return S; } int isEmpty ( stack S ) { return (S.top == -1); } int isFull ( stack S ) { return (S.top == MAXLEN - 1); } char top ( stack S ) { if (isEmpty(S)) { fprintf(stderr, "top: Empty stack\n"); return '\0'; } return S.element[S.top]; } stack push ( stack S , char ch ) { if (isFull(S)) { fprintf(stderr, "push: Full stack\n"); return S; } ++S.top; S.element[S.top] = ch; return S; } stack pop ( stack S ) { if (isEmpty(S)) { fprintf(stderr, "pop: Empty stack\n"); return S; } --S.top; return S; } void print ( stack S ) { int i; for (i = S.top; i >= 0; --i) printf("%c",S.element[i]); } 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)); }