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

Implementation of the supporting functions for the StackEntry structure that represents a simple stack. More...

#include <stdlib.h>
#include "stack.h"
Include dependency graph for stack.c:

Go to the source code of this file.

Functions

static StackEntryStackEntry_Create (void *item)
 Create a new StackEntry object with the given item.
 
static void StackEntry_Destroy (StackEntry *entry)
 Destroys the specified StackEntry object. After this function returns, the object is no longer valid.
 
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

Implementation of the supporting functions for the StackEntry structure that represents a simple stack.

Definition in file stack.c.

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().

◆ StackEntry_Create()

static StackEntry * StackEntry_Create ( void *  item)
static

Create a new StackEntry object with the given item.

Parameters
itemThe item to add to the stack.
Returns
Returns a new StackEntry object if successful; otherwise, returns NULL, indicating an out of memory error.

Definition at line 16 of file stack.c.

References StackEntry::item.

Referenced by Stack_Push().

◆ StackEntry_Destroy()

static void StackEntry_Destroy ( StackEntry entry)
static

Destroys the specified StackEntry object. After this function returns, the object is no longer valid.

Parameters
entryThe StackEntry object to destroy.

Definition at line 32 of file stack.c.

Referenced by Stack_Pop().