-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
199 lines (158 loc) · 4.28 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
A simple, fast dynamic stack
Copyright (C) 2012 William Kennington
This file is part of libadt.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "stack.h"
/*
IMPLEMENTATION NOTES:
Uses dynamically allocated arrays for storage and expands in exponentially growing links on a linked list
*/
#define START_SIZE 32
typedef struct _stack_node_t * stack_node_t;
struct _stack_t
{
stack_node_t head;
stack_node_t unused;
size_t size;
};
struct _stack_node_t
{
void ** data;
size_t size, len;
stack_node_t next;
};
stack_t
stack_init (void)
{
stack_t stack;
/* Allocate memory for the stack, the first node, and the first node's array */
stack = (stack_t) malloc (sizeof (struct _stack_t) +
sizeof (struct _stack_node_t) +
sizeof (void *) * START_SIZE);
/* Check for a failed alloc */
if (stack == NULL)
return stack;
/* Initialize the stack */
stack->size = 0;
stack->head = (stack_node_t)((uint8_t*)stack + sizeof (struct _stack_t));
stack->unused = NULL;
/* Initialize the first node */
stack->head->len = 0;
stack->head->size = START_SIZE;
stack->head->next = NULL;
stack->head->data = (void**)((uint8_t*)stack->head + sizeof (struct _stack_node_t));
return stack;
}
static void
stack_eob (stack_t stack)
{
if (stack->head->len == 0)
{
/* If there is already an unused node delete it */
if (stack->unused != NULL)
free (stack->unused);
stack->unused = stack->head;
stack->head = stack->head->next;
}
}
void *
stack_peek (stack_t stack)
{
/* Empty Stack Check */
if (stack->size == 0)
return NULL;
/* Check for the end of the array and move the link into unused */
stack_eob (stack);
return stack->head->data[stack->head->len-1];
}
void *
stack_pop (stack_t stack)
{
/* Empty Stack Check */
if (stack->size == 0)
return NULL;
/* Check for the end of the array and move the link into unused */
stack_eob (stack);
/* Decrease the size of the stack */
stack->size--;
return stack->head->data[--stack->head->len];
}
size_t
stack_size (stack_t stack)
{
return stack->size;
}
stack_error_t
stack_push (stack_t stack, void * data)
{
/* Handle list overflow */
if (stack->head->len == stack->head->size)
{
/* Add a new node if an empty one doesn't exist */
if (stack->unused == NULL)
{
/* Allocate a new node */
stack->unused = (stack_node_t) malloc (sizeof (struct _stack_node_t) +
sizeof (void *) * (stack->head->size << 1));
/* Check for malloc errors */
if (stack->unused == NULL)
return STACK_MALLOC_FAILED;
/* Initialize the unused node */
stack->unused->next = stack->head;
stack->unused->size = stack->head->size << 1;
stack->unused->len = 0;
stack->unused->data = (void**)((uint8_t*)stack->unused + sizeof (struct _stack_node_t));
}
stack->head = stack->unused;
stack->unused = NULL;
}
/* Add Element to stack */
stack->head->data[stack->head->len++] = data;
/* Increase Stack Size */
stack->size++;
return STACK_OK;
}
void
stack_clear (stack_t stack)
{
stack_node_t tmp;
/* Iterate over each node removing it */
while (stack->head->next != NULL)
{
tmp = stack->head;
stack->head = stack->head->next;
free (tmp);
}
/* Free Any unused nodes */
if (stack->unused != NULL)
{
free (stack->unused);
stack->unused = NULL;
}
/* Set the length of the last node to 0 */
stack->head->len = 0;
/* Reset the size of the stack */
stack->size = 0;
}
void
stack_destroy (stack_t stack)
{
/* Remove all of the nodes from the stack */
stack_clear (stack);
/* Free the stack */
free (stack);
}