-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
170 lines (133 loc) · 3.2 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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int32_t value;
struct Node *previous;
} Node;
typedef struct Stack {
size_t size;
Node *top;
} Stack;
typedef enum StackError {
OK,
NULL_POINTER,
ALLOCATION_FAILED,
EMPTY_STACK
} StackError;
StackError new_stack(Stack **out);
StackError push(Stack *stack, int32_t value);
StackError pop(Stack *stack, int32_t *out);
StackError peek(Stack *stack, int32_t *out);
StackError free_stack(Stack *stack);
char *error_message_stack(StackError error);
int main(void) {
Stack *stack;
StackError stack_error;
if ((stack_error = new_stack(&stack)) != OK) {
fprintf(stderr, "could not create stack, error: %s\n",
error_message_stack(stack_error));
exit(EXIT_FAILURE);
}
for (int32_t i = 0; i < 3; ++i) {
if ((stack_error = push(stack, i)) != OK) {
fprintf(stderr, "could not push, error: %s\n",
error_message_stack(stack_error));
}
}
for (size_t i = 0; i < 3; ++i) {
int32_t value;
if ((stack_error = peek(stack, &value)) != OK) {
fprintf(stderr, "could not peek, error: %s\n",
error_message_stack(stack_error));
} else {
printf("peek: %d\n", value);
}
if ((stack_error = pop(stack, &value)) != OK) {
fprintf(stderr, "could not pop, error: %s\n",
error_message_stack(stack_error));
} else {
printf("pop: %d\n", value);
}
}
free_stack(stack);
return 0;
}
StackError new_stack(Stack **out) {
if (out == NULL) {
return NULL_POINTER;
}
Stack *stack = (Stack *)malloc(sizeof(Stack));
if (stack == NULL) {
return ALLOCATION_FAILED;
}
stack->size = 0;
stack->top = NULL;
*out = stack;
return OK;
}
StackError push(Stack *stack, int32_t value) {
if (stack == NULL) {
return NULL_POINTER;
}
Node *node = (Node *)malloc(sizeof(Node));
if (node == NULL) {
return ALLOCATION_FAILED;
}
node->value = value;
node->previous = stack->top;
stack->top = node;
++stack->size;
return OK;
}
StackError pop(Stack *stack, int32_t *out) {
if (stack == NULL || out == NULL) {
return NULL_POINTER;
}
if (stack->size == 0) {
return EMPTY_STACK;
}
Node *popped_top = stack->top;
stack->top = popped_top->previous;
--stack->size;
*out = popped_top->value;
free(popped_top);
return OK;
}
StackError peek(Stack *stack, int32_t *out) {
if (stack == NULL || out == NULL) {
return NULL_POINTER;
}
if (stack->size == 0) {
return EMPTY_STACK;
}
*out = stack->top->value;
return OK;
}
StackError free_stack(Stack *stack) {
if (stack == NULL) {
return NULL_POINTER;
}
Node *current = stack->top;
while (current != NULL) {
Node *delete = current;
current = current->previous;
free(delete);
}
free(stack);
return OK;
}
char *error_message_stack(StackError error) {
switch (error) {
case OK:
return "no error";
case NULL_POINTER:
return "operation cannot be performed on a null pointer";
case ALLOCATION_FAILED:
return "could not allocate memory for operation";
case EMPTY_STACK:
return "operation could not be performed because stack is empty";
default:
return "unknown error";
}
}