#include <stdio.h>
typedef enum {FALSE, TRUE} bool;
/* DECLARATION OF STACK ELEMENTS AND OF STACK STRUCTURE */
typedef char stackElt;
typedef struct stack {
stackElt* elts;
int size;
} stack;
#define STACK_CAPACITY (24)
/* FUNCTION PROTOTYPES AND DOCUMENTATION */
stack createStack(); // create a new empty stack
bool isEmptyStack(stack s); // check whether stack s is empty or not
bool isFullStack (stack s); // check whether stack s is full or not
int sizeOfStack (stack s);
stack push(stackElt e, stack s); // add element e on top of non-full stack s
stack pop(stack s); // remove top element of non-empty stack s
stackElt top(stack s); // get top element of non-empty stack s
void showStack(stack s); // print the contents of stack s
/* DEFINITION OF THE FUNCTIONS */
Back to PR21
/* TEST PROGRAMMA */
int main(void) {
stack s = createStack();
int i;
printf("An empty stack has been created.\n");
printf("The size of the stack: %d.\n\n", sizeOfStack(s));
printf("Push characters \'a\', \'b\', ... onto the stack.\n");
for (i = 0; i < 10; i++)
if (!isFullStack(s))
s = push('a' + i, s);
printf("The size of the stack: %d.\n", sizeOfStack(s));
printf("The stack contains: "); showStack(s);
printf("\nCharacters are popped from the stack.\n");
for (i = 0; i < 6; i++)
if (!isEmptyStack(s))
s = pop(s);
printf("The size of the stack: %d.\n", sizeOfStack(s));
printf("The stack contains: "); showStack(s);
printf("The top of the stack: %c\n", top(s));
printf("\nPush characters \'A\' ... onto the stack.\n");
for (i = 0; i < 6; i++)
if (!isFullStack(s))
s = push('A' + i, s);
printf("The size of the stack: %d.\n", sizeOfStack(s));
printf("The stack contains: "); showStack(s);
printf("\nCharacters are popped until stack is empty.\n");
while (!isEmptyStack(s))
s = pop(s);
printf("The size of the stack: %d.\n", sizeOfStack(s));
if (isEmptyStack(s))
printf("The stack is empty.\n");
else
printf("SHIT: the stack should be empty now.\n");
return(0);
}
/*
OUTPUT:
An empty stack has been created.
The size of the stack: 0.
Push characters 'a', 'b', ... onto the stack.
The size of the stack: 10.
The stack contains: j i h g f e d c b a
Characters are popped from the stack.
The size of the stack: 4.
The stack contains: d c b a
The top of the stack: d
Push characters 'A' ... onto the stack.
The size of the stack: 10.
The stack contains: F E D C B A d c b a
Characters are popped until stack is empty.
The size of the stack: 0.
The stack is empty.
*/