DSA Hacking Club Library
Loading...
Searching...
No Matches
stack.h File Reference

Public interface for a simple stack (LIFO) data structure implemented with dynamic memory allocation. More...

#include <stdbool.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.

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

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

Detailed Description

Public interface for a simple stack (LIFO) data structure implemented with dynamic memory allocation.

Author
Alifoo
Date
2025-09-24

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.

Macro Definition Documentation

◆ STACK_EMPTY

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

Function Documentation

◆ stack_create()

Stack * stack_create ( int capacity)

Allocate and initialize a new stack.

Parameters
capacityMaximum number of elements the stack can hold.
Returns
Pointer to a new Stack instance, or NULL if allocation fails.

This function must be called before using the stack. The returned pointer should later be freed with stack_destroy().

◆ stack_destroy()

void stack_destroy ( Stack * s)

Destroy a stack and release its allocated memory.

Parameters
sPointer to the stack to be destroyed. Safe to pass NULL.

This function should be called when the stack is no longer needed.

◆ stack_is_empty()

bool stack_is_empty ( const Stack * s)

Check whether the stack is empty.

Parameters
sPointer to the stack.
Returns
true if the stack contains no elements, false otherwise.

◆ stack_peek()

int stack_peek ( const Stack * s)

Return the value at the top of the stack without removing it.

Parameters
sPointer to the stack.
Returns
The top value, or STACK_EMPTY if the stack is empty.

◆ stack_pop()

int stack_pop ( Stack * s)

Pop the value at the top of the stack.

Parameters
sPointer to the stack.
Returns
The popped value, or STACK_EMPTY if the stack is empty.

◆ stack_push()

bool stack_push ( Stack * s,
int value )

Push a value onto the top of the stack.

Parameters
sPointer to the stack.
valueValue to push.
Returns
true if the value was successfully pushed, false if the stack is full.

◆ stack_size()

int stack_size ( const Stack * s)

Get the current number of elements in the stack.

Parameters
sPointer to the stack.
Returns
The number of elements currently stored in the stack.