Design Pattern Examples
Overview of object-oriented design patterns
stack.c
Go to the documentation of this file.
1
5
6
#include <stdlib.h>
7
8
#include "
stack.h
"
9
16
static
StackEntry
*
StackEntry_Create
(
void
* item)
17
{
18
StackEntry
* entry = calloc(1,
sizeof
(
StackEntry
));
19
if
(entry != NULL)
20
{
21
entry->
item
= item;
22
}
23
24
return
entry;
25
}
26
32
static
void
StackEntry_Destroy
(
StackEntry
* entry)
33
{
34
free(entry);
35
}
36
38
// Stack_Push()
40
void
Stack_Push
(
StackEntry
** stack,
void
* item)
41
{
42
if
(stack != NULL && item != NULL)
43
{
44
StackEntry
* entry =
StackEntry_Create
(item);
45
if
(entry != NULL)
46
{
47
if
(*stack != NULL)
48
{
49
entry->
next
= *stack;
50
*stack = entry;
51
}
52
else
53
{
54
*stack = entry;
55
}
56
}
57
}
58
}
59
61
// Stack_Pop()
63
void
*
Stack_Pop
(
StackEntry
** stack)
64
{
65
void
* item = NULL;
66
if
(stack != NULL)
67
{
68
StackEntry
* entry = *stack;
69
if
(entry != NULL)
70
{
71
item = entry->
item
;
72
*stack = entry->
next
;
73
StackEntry_Destroy
(entry);
74
}
75
}
76
return
item;
77
}
78
80
// Stack_IsEmpty()
82
bool
Stack_IsEmpty
(
StackEntry
** stack)
83
{
84
return
stack == NULL || *stack == NULL;
85
}
StackEntry_Create
static StackEntry * StackEntry_Create(void *item)
Create a new StackEntry object with the given item.
Definition:
stack.c:16
Stack_IsEmpty
bool Stack_IsEmpty(StackEntry **stack)
Determines if the given stack is empty.
Definition:
stack.c:82
Stack_Push
void Stack_Push(StackEntry **stack, void *item)
Push the given entry onto the given stack.
Definition:
stack.c:40
StackEntry_Destroy
static void StackEntry_Destroy(StackEntry *entry)
Destroys the specified StackEntry object. After this function returns, the object is no longer valid.
Definition:
stack.c:32
Stack_Pop
void * Stack_Pop(StackEntry **stack)
Pop the last entry from the given stack, returning the item.
Definition:
stack.c:63
stack.h
Declaration of the StackEntry structure and the supporting functions that represents a simple stack.
StackEntry
Represents an entry on a simple stack that wraps an "item" represented by an opaque pointer.
Definition:
stack.h:32
StackEntry::next
struct StackEntry * next
Points to the next older entry in the stack.
Definition:
stack.h:33
StackEntry::item
void * item
The item being held in this entry in the stack.
Definition:
stack.h:34
c
helpers
stack.c
Generated on Tue Aug 29 2023 19:47:44 for Design Pattern Examples by
1.9.6