Stack structure
November 1, 2018 ยท View on GitHub
This project is based on GenericMap by mystborn, so there similar function names and structures
A simple stack of objects. Internally it is implemented as an array, so Push is O(n). Pop is O(1).
Defining a Type
Use the macro shlDeclareStack to generate the type and function definitions. It has the following arguments:
| Argument | Description |
|---|---|
| typeName | The name of the generated type. This will also prefix all of the function names. |
| itemType | The type of the list elements. |
Use the macro shlDefineStack to generate the function implementations.
| Argument | Description |
|---|---|
| typeName | The name of the generated type. This will also prefix all of the function names. |
| itemType | The type of the list elements. |
#include "stack.h"
shlDeclareStack(IntStack, int)
shlDefineStack(IntStack, int)
This list allows the following operations:
| Function | Description | Return type |
|---|---|---|
Init(typeName *stack, typeName Options options) | Initializes the data needed for the stack. | void |
Free(typeName *stack) | Frees the data used by the stack. It doesn't free the stack itself. | void |
Push(typeName *stack, itemType value) | Push an element in the top of the stack. | void |
Peek(typeName *stack) | Gets the top of the stack without removing it. | itemType |
Pop(typeName *stack) | Remove the top of the stack. | _itemType _ |
Contains(typeName *stack, itemType value) | Return true if an object is contained in the stack. | bool |
Clear(typeName* queue) | Clear the stack, freeing every element if a freeFn was provided. Doesn't free the stack itself. | void |
Options
Each definition of a queue declare a struct typeName Options that is used to initialize the stack. The struct has the following members:
| Name | Type | Description |
|---|---|---|
equalsFn | bool (*)(const itemType, const itemType) | (optional) A pointer to a function that takes two elements, and returns true if the elements are equals, and returns false otherwise. If no equalsFn is provided then the operation Contains always return false. |
freeFn | void (*)(itemType) | (optional) A pointer to a function that takes an element and free it. If no freeFn is provided, then the operation Clear and Free doesn't free the elements and the user of the stack is the responsible for free the elements. |
defaultValue | itemType | The value to return when you apply the Pop operation and the stack is empty. |
Example:
#include <stdio.h>
#include "stack.h"
bool intEquals(const int x, const int y)
{
return x == y;
}
shlDeclareStack(IntStack, int)
shlDefineStack(IntStack, int)
int main()
{
IntStackOptions options = (IntStackOptions){0};
options.defaultValue = 0;
options.equalsFn = intEquals;
IntStack stack;
IntStackInit(&stack, options);
for (int i = 0; i < 100; i++)
IntStackPush(&stack, i);
int sum = 0;
// sum all the numbers in the stack
while (stack.count > 0)
{
int value = IntStackPop(&stack);
sum += value;
}
printf("The sum of the elements of the stack is %d\n", sum);
return 0;
}