|
DSA Hacking Club Library
|
Public interface for a simple stack (LIFO) data structure implemented with dynamic memory allocation. More...
#include <stdbool.h>

Go to the source code of this file.
Classes | |
| struct | Stack |
| Representation of a stack of integers. More... | |
Macros | |
| #define | STACK_EMPTY (-2147483647 - 1) |
| Sentinel value returned by stack operations when the stack is empty. | |
Functions | |
| Stack * | stack_create (int capacity) |
| Allocate and initialize a new stack. | |
| void | stack_destroy (Stack *s) |
| Destroy a stack and release its allocated memory. | |
| bool | stack_push (Stack *s, int value) |
| Push a value onto the top of the stack. | |
| int | stack_pop (Stack *s) |
| Pop the value at the top of the stack. | |
| int | stack_peek (const Stack *s) |
| Return the value at the top of the stack without removing it. | |
| int | stack_size (const Stack *s) |
| Get the current number of elements in the stack. | |
| bool | stack_is_empty (const Stack *s) |
| Check whether the stack is empty. | |
Public interface for a simple stack (LIFO) data structure implemented with dynamic memory allocation.
This header defines the Stack struct and the functions needed to create, manage, and destroy a stack of integers. The implementation uses malloc for dynamic allocation, allowing the user to specify the stack capacity at runtime.
| #define STACK_EMPTY (-2147483647 - 1) |
Sentinel value returned by stack operations when the stack is empty.
Defined as (-2147483647 - 1), which is a portable expression for INT_MIN. Used by functions such as stack_pop() and stack_peek() to indicate an underflow condition.
| Stack * stack_create | ( | int | capacity | ) |
Allocate and initialize a new stack.
| capacity | Maximum number of elements the stack can hold. |
This function must be called before using the stack. The returned pointer should later be freed with stack_destroy().
| void stack_destroy | ( | Stack * | s | ) |
Destroy a stack and release its allocated memory.
| s | Pointer to the stack to be destroyed. Safe to pass NULL. |
This function should be called when the stack is no longer needed.
| bool stack_is_empty | ( | const Stack * | s | ) |
Check whether the stack is empty.
| s | Pointer to the stack. |
| int stack_peek | ( | const Stack * | s | ) |
Return the value at the top of the stack without removing it.
| s | Pointer to the stack. |
| int stack_pop | ( | Stack * | s | ) |
Pop the value at the top of the stack.
| s | Pointer to the stack. |
| bool stack_push | ( | Stack * | s, |
| int | value ) |
Push a value onto the top of the stack.
| s | Pointer to the stack. |
| value | Value to push. |
| int stack_size | ( | const Stack * | s | ) |
Get the current number of elements in the stack.
| s | Pointer to the stack. |