Files | |
file | stack.c |
Stacks implementation. | |
file | stack.h |
Stacks header. | |
Functions | |
void | stack_init (stack *_s) |
Initialize a stack. | |
stack * | stack_create () |
Create and initialize a stack. | |
int | stack_push (stack *_s, void *_elem) |
Pushes a new element onto the stack. | |
void * | stack_pop (stack *_s) |
Removes the top element from the stack. | |
size_t | stack_size (stack *_s) |
Compute the size of a stack. | |
int | stack_is_empty (stack *_s) |
Checks if the stack is empty. | |
void | stack_delete (stack *_s) |
Deletes a stack, not including its data items. | |
void | stack_reset (stack *_s) |
Reset a stack as if it has just been created. | |
void | stack_destroy (stack *_s) |
Destroys a stack, including its data items. | |
node_l * | stack_data (stack *_s) |
Get a pointer to the internal stack data. |
void stack_init | ( | stack * | _s | ) |
Initialize a stack.
This function initializes a stack, setting all values to 0 or NULL
respectively. This function is here for portability, because not all systems handle NULL
as a series of binary zeros.
_s | a stack |
Definition at line 50 of file stack.c.
References stack::data, and stack::size.
Referenced by stack_create(), and stack_reset().
stack* stack_create | ( | ) |
Create and initialize a stack.
NULL
on failure Definition at line 64 of file stack.c.
References stack_init().
int stack_push | ( | stack * | _s, | |
void * | _elem | |||
) |
Pushes a new element onto the stack.
This function puts a new element pointed to by _elem
on top of the stack.
_s | a stack | |
_elem | an element |
Definition at line 84 of file stack.c.
References stack::data, list_push(), and stack::size.
void* stack_pop | ( | stack * | _s | ) |
Removes the top element from the stack.
This function removes the top element from the stack and returns its location. If the stack is empty, stack_pop() returns NULL
. Note that NULL
is also a valid stack element, so if you previously pushed a NULL
pointer onto the stack, you should use stack_size() to determine whether the stack is now empty or the popped stack element was a NULL
pointer.
_s | a stack |
NULL
on failure (empty stack) Definition at line 106 of file stack.c.
References stack::data, list_pop(), and stack::size.
size_t stack_size | ( | stack * | _s | ) |
Compute the size of a stack.
This function returns the size of the stack. Because the stack_*() familiy of functions keeps track of the current stack size, this is a constant time operation.
_s | a stack |
Definition at line 125 of file stack.c.
References stack::size.
int stack_is_empty | ( | stack * | _s | ) |
Checks if the stack is empty.
_s | a stack |
Definition at line 137 of file stack.c.
References stack::size.
void stack_delete | ( | stack * | _s | ) |
Deletes a stack, not including its data items.
This function deletes a stack, not including its data items. This will result in memory leaks if you don't have a copy of the stack, because all references to the data items are lost.
_s | a stack |
Definition at line 152 of file stack.c.
References stack::data, and list_delete().
void stack_reset | ( | stack * | _s | ) |
Reset a stack as if it has just been created.
array_reset() sets the stack size to 0. This will result in memory leaks if you don't have a copy of the stack, because all references to the data items are lost.
Use stack_destroy() first to free the data items.
_s | a stack |
Definition at line 171 of file stack.c.
References stack::data, list_delete(), and stack_init().
void stack_destroy | ( | stack * | _s | ) |
Destroys a stack, including its data items.
This function deletes a stack, including its data items. After a call to stack_destroy(), the stack and its data items will be lost. If you have a copy of the stack, it will be invalid (use stack_delete() on it).
_s | a stack |
Definition at line 191 of file stack.c.
References stack::data, and list_destroy().
Get a pointer to the internal stack data.
Internally, stacks are represented as lists. This function returns a pointer to the stack's internal list. Use with care.
_s | a stack |
Definition at line 208 of file stack.c.
References stack::data.