Design Pattern Examples
Overview of object-oriented design patterns
stack.h File Reference

Declaration of the StackEntry structure and the supporting functions that represents a simple stack. 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  StackEntry
 Represents an entry on a simple stack that wraps an "item" represented by an opaque pointer. More...
 

Macros

#define __STACK_H__
 

Typedefs

typedef struct StackEntry StackEntry
 Represents an entry on a simple stack that wraps an "item" represented by an opaque pointer.
 

Functions

void Stack_Push (StackEntry **stack, void *item)
 Push the given entry onto the given stack.
 
void * Stack_Pop (StackEntry **stack)
 Pop the last entry from the given stack, returning the item.
 
bool Stack_IsEmpty (StackEntry **stack)
 Determines if the given stack is empty.
 

Detailed Description

Declaration of the StackEntry structure and the supporting functions that represents a simple stack.

Definition in file stack.h.

Macro Definition Documentation

◆ __STACK_H__

#define __STACK_H__

Definition at line 8 of file stack.h.

Typedef Documentation

◆ StackEntry

typedef struct StackEntry StackEntry

Represents an entry on a simple stack that wraps an "item" represented by an opaque pointer.

This structure is allocated on the heap and then linked into the stack. The item is what is being pushed onto the stack, the StackEntry structure provides the necessary link to the next older StackEntry structure containing the next older item.

StackEntry* _undoStack = NULL;
void* item; // A pointer to something to be pushed onto the stack
Stack_Push(&_undoStack, item);
item = Stack_Pop(&_undoStack);
bool is_empty = Stack_IsEmpty(&_undoStack);
bool Stack_IsEmpty(StackEntry **stack)
Determines if the given stack is empty.
Definition: stack.c:82
void Stack_Push(StackEntry **stack, void *item)
Push the given entry onto the given stack.
Definition: stack.c:40
void * Stack_Pop(StackEntry **stack)
Pop the last entry from the given stack, returning the item.
Definition: stack.c:63
Represents an entry on a simple stack that wraps an "item" represented by an opaque pointer.
Definition: stack.h:32

Function Documentation

◆ Stack_IsEmpty()

bool Stack_IsEmpty ( StackEntry **  stack)

Determines if the given stack is empty.

Parameters
stackA pointer to a pointer to a StackEntry object representing the top of the stack. Points to a NULL if the stack is empty. This pointer to a pointer cannot be NULL.
Returns
Returns true if the stack is empty; otherwise, returns false, there is at least one item on the stack.

Definition at line 82 of file stack.c.

Referenced by Command_Undo().

◆ Stack_Pop()

void * Stack_Pop ( StackEntry **  stack)

Pop the last entry from the given stack, returning the item.

Parameters
stackA pointer to a pointer to a StackEntry object representing the top of the stack. Points to a NULL if the stack is empty. This pointer to a pointer cannot be NULL.
Returns
Returns the item from the top of the stack. Returns NULL if the stack was empty at the time of the call.

Definition at line 63 of file stack.c.

References StackEntry::item, StackEntry::next, and StackEntry_Destroy().

Referenced by Command_Undo(), and Memento_Undo().

◆ Stack_Push()

void Stack_Push ( StackEntry **  stack,
void *  item 
)

Push the given entry onto the given stack.

Parameters
stackA pointer to a pointer to a StackEntry object representing the top of the stack. Points to a NULL if the stack is empty. This pointer to a pointer cannot be NULL.
itemThe item to store on the stack.

Definition at line 40 of file stack.c.

References StackEntry::next, and StackEntry_Create().

Referenced by Command_Save_And_Execute(), Command_Undo(), and Memento_SaveForUndo().