-
Notifications
You must be signed in to change notification settings - Fork 52
/
stack.c
77 lines (64 loc) · 1.3 KB
/
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
@file
Code to provide a FIFO stack data structure
@date 1/25/2012
***************************************************************/
#include "ntddk.h"
#include "stack.h"
void StackInitStack(Stack * stack)
{
stack->empty = 1;
stack->top = 0;
}
void * StackPop(Stack * stack)
{
void *outVal;
if (stack->empty == 1)
return NULL;
outVal = stack->data[stack->top];
if (stack->top == 0)
{
stack->empty = 1;
}
else
{
stack->top--;
}
return outVal;
}
void StackPush(Stack * stack, void * ptr)
{
if (stack->top + 1 == MAX_STACK_SIZE)
return;
if (stack->empty == 1)
{
stack->top = 0;
stack->empty = 0;
stack->data[0] = ptr;
}
else
{
stack->top++;
stack->data[stack->top] = ptr;
}
}
uint8 StackIsEmpty(Stack * stack)
{
return stack->empty;
}
uint8 StackIsFull(Stack * stack)
{
return (stack->top + 1 == MAX_STACK_SIZE) ? 1 : 0;
}
uint32 StackNumEntries(Stack * stack)
{
if (stack->empty == 1)
return 0;
return stack->top + 1;
}
void * StackPeek(Stack * stack)
{
if (stack->empty == 1)
return NULL;
return stack->data[stack->top];
}