-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
255 lines (197 loc) · 4.8 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//@AUTHOR : Guilherme Cardoso Oliveira <[email protected]>
//@lICENSE: MIT
//@DATE : 2021-04-28
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define DATA_STRUCT_NAME "Stack"
//-----------------------------------------------------------
//DATA
typedef int Data;
#define DATA_FORMAT "%d"
#define PRINT_SEPARATOR "<-"
//-----------------------------------------------------------
//NODE
typedef struct node {
Data data;
struct node *next;
} * Node;
//-----------------------------------------------------------
//STACK
typedef struct stack {
Node top;
} * Stack;
//-----------------------------------------------------------
//NODE METHODS
// Checks if a node is empty
bool empty(Node node) {
return node == NULL;
}
// Allocate a new node
Node node_alloc() {
Node node = (Node) malloc(sizeof(Node));
if(empty(node)) {
printf("[ERR] Not enough memory");
exit(EXIT_FAILURE);
}
return node;
}
// Creates a new node and fill it with data
Node node_new(Data data) {
Node node = node_alloc();
node->data = data;
node->next = NULL;
return node;
}
// Get a node from a specified position
Node node_getpos(Node node, int position) {
if(position <= 0 || empty(node)) {
printf("[ERR] Invalid parameter position");
exit(EXIT_FAILURE);
}
if(position == 1)
return node;
return node_getpos(node->next, position-1);
}
// Get penult node or exit with error otherwise
Node node_getpenult(Node node) {
if(empty(node) || empty(node->next)) {
printf("[ERR] Hasn't penult node!");
exit(EXIT_FAILURE);
}
if(empty(node->next->next))
return node;
return node_getpenult(node->next);
}
// prints all linked nodes
void node_print(Node node) {
if(empty(node)) {
printf("[NULL]\n");
return;
}
printf("[");
printf(DATA_FORMAT, node->data);
printf("]");
printf(PRINT_SEPARATOR);
return node_print(node->next);
}
// Checks if a specified data exists in a group of linked nodes
bool node_contains(Node node, Data data) {
if(empty(node))
return false;
if(node->data == data)
return true;
return node_contains(node->next, data);
}
// Returns the number of nodes in a group of linked nodes
int node_count(Node node) {
if(empty(node)) return 0;
return 1 + node_count(node->next);
}
// Remove all nodes from a group of linked nodes
void node_removeAll(Node node) {
if(empty(node)) return;
Node next = node->next;
free(node);
return node_removeAll(next);
}
//-----------------------------------------------------------
//LINKEDLIST METHODS
// Checks if stack is initialized
bool isInit(Stack stack) {
return stack != NULL;
}
// Checks if stack is empty
bool isEmpty(Stack stack) {
return stack->top == NULL;
}
// Ensures that the stack is initialized or generates an error otherwise
void assertInit(Stack stack) {
if(!isInit(stack)) {
printf("[ERR] ");
printf(DATA_STRUCT_NAME);
printf(" isn't initialized!");
exit(EXIT_FAILURE);
}
}
// Ensures that the stack isn't empty or generates an error otherwise
void assertNotEmpty(Stack stack) {
assertInit(stack);
if(isEmpty(stack)) {
printf("[ERR] ");
printf(DATA_STRUCT_NAME);
printf(" is empty!");
exit(EXIT_FAILURE);
}
}
// Creates a new stack
Stack new_stack() {
Stack stack = (Stack) malloc(sizeof(Stack));
if(!isInit(stack)) {
printf("[ERR] Not enough memory");
exit(EXIT_FAILURE);
}
stack->top = NULL;
return stack;
}
// Adds a new node to first position in the list
void push(Stack stack, Data data) {
assertInit(stack);
Node node = node_new(data);
node->next = stack->top;
stack->top = node;
}
// Returns data from the first node in the list
Data peek(Stack stack) {
assertNotEmpty(stack);
return stack->top->data;
}
// Returns the number of nodes in the stack
int size(Stack stack) {
assertInit(stack);
return node_count(stack->top);
}
// Check if there is a specific data in the stack
bool contains(Stack stack, Data data) {
assertInit(stack);
return node_contains(stack->top, data);
}
// Prints all data from nodes in the stack
void print(Stack stack) {
assertInit(stack);
node_print(stack->top);
}
// Removes all nodes in the stack
void clear(Stack stack) {
node_removeAll(stack->top);
stack->top = NULL;
}
// Removes the first node in the list
Data pop(Stack stack) {
Data data = peek(stack);
Node top = stack->top;
stack->top = top->next;
free(top);
if(isEmpty(stack))
stack->top = NULL;
return data;
}
//-----------------------------------------------------------
//MAIN
int main() {
//Example:
Stack stack = new_stack();
push(stack, 30);
push(stack, 20);
push(stack, 10);
print(stack);
printf("Top: %d\n", peek(stack));
printf("size: %d\n", size(stack));
printf("Has 30? %d\n", contains(stack, 30));
printf("Has 60? %d\n", contains(stack, 60));
pop(stack);
pop(stack);
print(stack);
clear(stack);
return 0;
}