LibRCG  3.1.1
stack.c File Reference

Implementation of a stack as a linked list. More...

Go to the source code of this file.

Functions

Stack newStack (void)
 Creates a stack. More...
 
void stackDelete (Stack stack)
 Deletes a stack. More...
 
int stackPush (Stack stack, void *value)
 Inserts an element at the top of the stack. More...
 
int stackPop (Stack stack, void **value)
 Removes an elements from the top of a stack. More...
 
int stackTop (Stack stack, void **value)
 Provides the value of the element at the top of a stack. More...
 
int stackSize (Stack stack)
 Returns the size of a stack. More...
 
int stackMap (Stack stack, void(*fun)(void *))
 Applies a function to the elements of a stack. More...
 
Iterator stackIterator (Stack stack)
 Creates an iterator from a stack. More...
 

Detailed Description

Implementation of a stack as a linked list.

Author
Rui Carlos Gonçalves
Version
3.0.1
Date
01/2014

Definition in file stack.c.

Function Documentation

Stack newStack ( void  )

Creates a stack.

Returns
NULL if an error occurred
the new stack otherwise

Definition at line 13 of file stack.c.

void stackDelete ( Stack  stack)

Deletes a stack.

Attention
This function only frees the memory used by the stack. It does not free the memory used by elements the stack contains.
Parameters
stackthe stack to be deleted

Definition at line 26 of file stack.c.

Iterator stackIterator ( Stack  stack)

Creates an iterator from a stack.

See Also
Iterator
Parameters
stackthe stack
Returns
NULL if an error occurred
the iterator otherwise

Definition at line 122 of file stack.c.

int stackMap ( Stack  stack,
void(*)(void *)  fun 
)

Applies a function to the elements of a stack.

The function to be applied must be of type void fun(void*).

Parameters
stackthe stack
funthe function to be applied
Returns
0 if the stack was not empty
1 otherwise

Definition at line 105 of file stack.c.

int stackPop ( Stack  stack,
void **  value 
)

Removes an elements from the top of a stack.

Provides the value of the removed element if the value of value is not NULL.

Attention
This function does not free the memory used by the removed element.
Parameters
stackthe stack
valuepointer where the removed value should be put (or NULL)
Returns
0 if the element was removed
1 if the stack was empty

Definition at line 62 of file stack.c.

int stackPush ( Stack  stack,
void *  value 
)

Inserts an element at the top of the stack.

Parameters
stackthe stack
valuethe value to be inserted
Returns
0 if the new value was inserted
1 if it was not possible to insert the new element

Definition at line 44 of file stack.c.

int stackSize ( Stack  stack)

Returns the size of a stack.

Parameters
stackthe stack
Returns
the size of the stack

Definition at line 98 of file stack.c.

int stackTop ( Stack  stack,
void **  value 
)

Provides the value of the element at the top of a stack.

If the stack is empty, it will be put the value NULL at value.

Attention
This function puts at value a pointer to the value at the top. Changes to this value will affect the element in the stack.
Parameters
stackthe stack.
valuepointer where the value at the first position will be put
Returns
0 if the stack was not empty
1 otherwise

Definition at line 84 of file stack.c.

LibRCG © 2004-2015   Rui Carlos Gonçalves