-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.c
57 lines (46 loc) · 935 Bytes
/
Stack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "Stack.h"
#include <stdlib.h>
void StackInitialize(Stack* _stack)
{
_stack->head = NULL;
_stack->size = 0;
}
bool StackEmpty(const Stack* _stack)
{
return _stack->size == 0;
}
voidPtr_t StackPop(Stack* _stack)
{
if (StackEmpty(_stack)) return NULL;
StackNode* front = _stack->head;
const voidPtrConst_t frontVal = front->value;
_stack->head = front->next;
_stack->size--;
free(front);
return frontVal;
}
void StackPush(Stack* _stack, voidPtrConst_t _value)
{
StackNode* node = malloc(sizeof(StackNode));
if(node)
{
node->value = _value;
node->next = _stack->head;
_stack->head = node;
_stack->size++;
}
}
voidPtr_t StackFront(const Stack* _stack)
{
return _stack->head->value;
}
void StackDestroy(const Stack* _stack)
{
StackNode* node = _stack->head;
while(node)
{
StackNode* next = node->next;
free(node);
node = next;
}
}