-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.c
executable file
·134 lines (114 loc) · 3.33 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "Stack.h"
typedef struct StackNode
{
void *data;
struct StackNode *next;
} Node;
//------------------------------------------------------------------------------
// Frees all the nodes and data. Top point to NULL, size is reset to zero, but
// memSize is kept intact.
//------------------------------------------------------------------------------
void stackClear(Stack *s)
{
while(s->size > 0)
{
Node *temp = s->top;
s->top = s->top->next;
free(temp->data);
free(temp);
s->size--;
}
s->top = NULL;
}
//------------------------------------------------------------------------------
// Returns 1 if the stack is empty, 0 otherwise.
//------------------------------------------------------------------------------
int stackEmpty(const Stack *s)
{
return (s->size == 0);
}
//------------------------------------------------------------------------------
// Initializes the stack.
// Returns 0 on success, -1 on failure.
//------------------------------------------------------------------------------
int stackInit(Stack *s, const size_t memSize)
{
if (memSize > 0)
{
s->memSize = memSize;
s->size = 0;
s->top = NULL;
return 0;
}
return -1;
}
//------------------------------------------------------------------------------
// Returns by reference the data at the top of the stack.
// Returns 0 on success, -1 on failure.
//------------------------------------------------------------------------------
int stackPeek(const Stack *s, void *data)
{
if (s->size > 0)
{
memcpy(data, s->top->data, s->memSize);
return 0;
}
return -1;
}
//------------------------------------------------------------------------------
// Returns by reference the data at the top of the stack, and removes the top
// node from the stack.
// Returns 0 on success, -1 on failure.
//------------------------------------------------------------------------------
int stackPop(Stack *s, void *data)
{
if (s->size > 0)
{
memcpy(data, s->top->data, s->memSize);
Node *temp = s->top->next;
free(s->top->data);
free(s->top);
if (s->size > 1)
s->top = temp;
else
s->top = NULL;
s->size--;
return 0;
}
return -1;
}
//------------------------------------------------------------------------------
// Adds the given data to the top of the stack.
// Returns 0 on success, -1 on failure.
//------------------------------------------------------------------------------
int stackPush(Stack *s, const void *data)
{
if (s->memSize > 0)
{
Node *node = (Node *) malloc(sizeof(Node));
if (node == NULL)
return -1;
node->data = malloc(s->memSize);
if (node->data == NULL)
{
free(node);
return -1;
}
memcpy(node->data, data, s->memSize);
if (s->size == 0)
node->next = NULL;
else
node->next = s->top;
s->top = node;
s->size++;
return 0;
}
return -1;
}
//------------------------------------------------------------------------------
// Returns the size of the stack.
//------------------------------------------------------------------------------
size_t stackSize(const Stack *s)
{
return s->size;
}