My library
Library for common tasks
Functions
stack.h File Reference

Functions for working with Stack type. More...

#include "types.h"
Include dependency graph for stack.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

Stack newStack (const spec_t spec)
 Allocate a new Stack of specified type. More...
 
void push (Stack stack,...)
 Push an item into a Stack. More...
 
void printStack (const spec_t spec, const Stack stack)
 Print contents from a Stack. More...
 
void pop (Stack stack, void *dest)
 Pop an item from a Stack. More...
 
void deleteHeadFromStack (Stack stack)
 Delete current Stack head. More...
 
byte isStackEmpty (Stack stack)
 Check if Stack is empty. More...
 
void deleteStack (Stack stack)
 Delete a Stack. More...
 
void peekStack (Stack stack, void *dest)
 Get the item at the head of a Stack without popping it. More...
 
byte isInStack (Stack stack,...)
 Detect if an item is inside a Stack. More...
 
Stack chooseNewStackFromArray (const spec_t spec, const void *arr, unsigned int size)
 Create a Stack from an array. More...
 
void pushFromPtr (Stack stack, const void *element)
 Push an item into a Stack. More...
 
unsigned int getStackLength (const Stack stack)
 Get the size of a Stack. More...
 
Stack newStackFromCharArray (const char arr[], unsigned int size)
 Create a Stack from an array of chars. More...
 
Stack newStackFromIntArray (const int arr[], unsigned int size)
 Create a Stack from an array of integers. More...
 
Stack newStackFromFloatArray (const float arr[], unsigned int size)
 Create a Stack from an array of floats. More...
 
Stack newStackFromDoubleArray (const double arr[], unsigned int size)
 Create a Stack from an array of doubles. More...
 
Stack newStackFromPtrArray (const void *arr, unsigned int size)
 Create a Stack from an array of pointers. More...
 
byte areStacksEqual (const Stack stack1, const Stack stack2)
 Compare two Stack. More...
 

Detailed Description

Functions for working with Stack type.

Author
Pietro Firpo (pietr.nosp@m.o.fi.nosp@m.rpo@p.nosp@m.m.me)

Function Documentation

◆ areStacksEqual()

byte areStacksEqual ( const Stack  stack1,
const Stack  stack2 
)

Compare two Stack.

Parameters
stack1The first Stack you want to compare
stack2The second Stack you want to compare
Returns
The result of the comparison
Return values
TRUEstack1 and stack2 have equal type and equal contents
FALSEstack1 and stack2 do not have equal type or equal contents

◆ chooseNewStackFromArray()

Stack chooseNewStackFromArray ( const spec_t  spec,
const void *  arr,
unsigned int  size 
)

Create a Stack from an array.

Parameters
specThe type specifier of the array passed. Refer to spec_t for supported types
arrThe array you want to create the Stack from
sizeThe number of items in arr
Returns
A Stack containing the elements in arr, having the last element of arr as head

◆ deleteHeadFromStack()

void deleteHeadFromStack ( Stack  stack)

Delete current Stack head.

Parameters
stackThe Stack you want to delete the head from

◆ deleteStack()

void deleteStack ( Stack  stack)

Delete a Stack.

Parameters
stackThe Stack you want to delete

◆ getStackLength()

unsigned int getStackLength ( const Stack  stack)

Get the size of a Stack.

Parameters
stackThe Stack you want to evaluate
Returns
The number of elements in stack

◆ isInStack()

byte isInStack ( Stack  stack,
  ... 
)

Detect if an item is inside a Stack.

Parameters
stackThe Stack you want search in
...The element you want to search
Note
This function does not support float and double Stack types
Even though specifying more than one item for single call does not throw a compiler nor runtime error, only searching one item is supported. Other items are ignored. If you don't specify any item to be searched, still no errors occur but the return value of the function can be unpredictable
Return values
TRUEGiven element is contained in stack
FALSEGiven element is not contained in stack

◆ isStackEmpty()

byte isStackEmpty ( Stack  stack)

Check if Stack is empty.

Parameters
stackThe Stack to be checked
Return values
TRUEstack is empty
FALSEstack is not empty

◆ newStack()

Stack newStack ( const spec_t  spec)

Allocate a new Stack of specified type.

Parameters
specType specifier of the Stack you want to create. Refer to spec_t for supported types
Returns
An empty Stack

◆ newStackFromCharArray()

Stack newStackFromCharArray ( const char  arr[],
unsigned int  size 
)

Create a Stack from an array of chars.

Equivalent to chooseNewStackFromArray("%c", arr, size). Refer to chooseNewStackFromArray()

◆ newStackFromDoubleArray()

Stack newStackFromDoubleArray ( const double  arr[],
unsigned int  size 
)

Create a Stack from an array of doubles.

Equivalent to chooseNewStackFromArray("%lf", arr, size). Refer to chooseNewStackFromArray()

◆ newStackFromFloatArray()

Stack newStackFromFloatArray ( const float  arr[],
unsigned int  size 
)

Create a Stack from an array of floats.

Equivalent to chooseNewStackFromArray("%f", arr, size). Refer to chooseNewStackFromArray()

◆ newStackFromIntArray()

Stack newStackFromIntArray ( const int  arr[],
unsigned int  size 
)

Create a Stack from an array of integers.

Equivalent to chooseNewStackFromArray("%i", arr, size). Refer to chooseNewStackFromArray()

◆ newStackFromPtrArray()

Stack newStackFromPtrArray ( const void *  arr,
unsigned int  size 
)

Create a Stack from an array of pointers.

Equivalent to chooseNewStackFromArray("%p", arr, size). Refer to chooseNewStackFromArray()

◆ peekStack()

void peekStack ( Stack  stack,
void *  dest 
)

Get the item at the head of a Stack without popping it.

Parameters
stackThe Stack you want to get the item
destThe address of the variable you want to store the item in

◆ pop()

void pop ( Stack  stack,
void *  dest 
)

Pop an item from a Stack.

Parameters
stackThe Stack you want to pop an item from
destThe address of the variable you want to store the popped item in

◆ printStack()

void printStack ( const spec_t  spec,
const Stack  stack 
)

Print contents from a Stack.

Parameters
specThe type and format specifier you want to use to print the single element of the Stack. Use the printf() conventions
stackThe Stack you want to print

◆ push()

void push ( Stack  stack,
  ... 
)

Push an item into a Stack.

Parameters
stackThe Stack you want to push into
...The item you want to push into stack
Note
Even though pushing more than one item for single call does not throw a compiler nor runtime error, only pushing one item is supported. Other items are ignored and are not pushed into stack. If you don't specify any item to be pushed, still no errors occur but the content of your Stack can be messed up

◆ pushFromPtr()

void pushFromPtr ( Stack  stack,
const void *  element 
)

Push an item into a Stack.

Parameters
stackThe Stack you want to push an item into
elementPointer to the item you want to push into stack