.h file containing
struct Stack {
int top;
int arr[100];
};
void push(struct Stack *s, int val); int pop(struct Stack *s); int isEmpty(struct Stack *s);
.c file containing definitions of the above functions
void clear(struct Stack *s) {
while (!isEmpty(s))
pop(s);
}
i.e., repeatedly pop the stack until empty.
Here is a second (seemingly more clever) way of clearing the stack, taking into consideration the knowledge that the stack is being represented by an array and a pointer to the top element of the stack:
void clear(struct Stack *s) {
s->top = -1;
}
i.e., assuming that top points to the top element of the stack (and having taken 22, assuming--
correctly as the case may be-- that an empty stack has a top of -1), the function simply assigns
that value to top.