stack.h

This interface exports an abstraction for stacks.
Type
Stack The abstract data type for the stack.
Functions
newStack() Allocates and returns a new stack, which is initially empty.
freeStack(stack) Frees the storage associated with the stack.
push(stack, element) Pushes the specified element onto the stack.
pop(stack) Pops the top element from the stack and returns that value.
peek(stack) Returns the top element from the stack without removing it.
isEmpty(stack) Tests whether the stack is empty.
size(stack) Returns the number of elements currently pushed on the stack.
clear(stack) Removes all elements from the stack.
clone(stack) Creates a copy of the stack.

Type detail


typedef struct StackCDT *Stack;
The abstract data type for the stack.

Function detail


Stack newStack(void);
Allocates and returns a new stack, which is initially empty.

Usage:

stack = newStack();

void freeStack(Stack stack);
Frees the storage associated with the stack.

Usage:

freeStack(stack);

void push(Stack stack, void *element);
Pushes the specified element onto the stack.

Usage:

push(stack, element);

void *pop(Stack stack);
Pops the top element from the stack and returns that value. The first value popped is always the last one that was pushed. If the stack is empty when pop is called, the implementation calls error with an appropriate message.

Usage:

element = pop(stack);

void *peek(Stack stack);
Returns the top element from the stack without removing it. If the stack is empty when pop is called, the implementation calls error with an appropriate message.

Usage:

element = peek(stack);

bool isEmpty(Stack stack);
Tests whether the stack is empty.

Usage:

if (isEmpty(stack)) . . .

int size(Stack stack);
Returns the number of elements currently pushed on the stack.

Usage:

n = size(stack);

void clear(Stack stack);
Removes all elements from the stack.

Usage:

clear(stack);

Stack clone(Stack stack);
Creates a copy of the stack. The clone function copies only the first level of the structure and does not copy the individual elements.

Usage:

newstack = clone(stack);