Stacks
[Data Structures]


Files

file  stack.c
 Stacks implementation.
file  stack.h
 Stacks header.

Functions

void stack_init (stack *_s)
 Initialize a stack.
stackstack_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_lstack_data (stack *_s)
 Get a pointer to the internal stack data.

Function Documentation

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.

Parameters:
_s a stack
Returns:
nothing

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.

Returns:
a pointer to a newly allocated stack on success, 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.

Parameters:
_s a stack
_elem an element
Returns:
-1 on failure (out of memory), 0 on success

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.

Parameters:
_s a stack
Returns:
top-most stack element on success, 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.

Parameters:
_s a stack
Returns:
its size

Definition at line 125 of file stack.c.

References stack::size.

int stack_is_empty ( stack _s  ) 

Checks if the stack is empty.

Parameters:
_s a stack
Returns:
0 if the stack is not empty, non-zero otherwise

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.

Parameters:
_s a stack
Returns:
nothing

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.

Parameters:
_s a stack
Returns:
nothing

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).

Parameters:
_s a stack
Returns:
nothing

Definition at line 191 of file stack.c.

References stack::data, and list_destroy().

node_l* stack_data ( stack _s  ) 

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.

Parameters:
_s a stack
Returns:
a pointer to the stack's internal list representation

Definition at line 208 of file stack.c.

References stack::data.


Generated on Thu Jul 19 13:36:09 2007 for libv by  doxygen 1.5.1-p1. Thank you, SourceForge.net Logo