-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
180 lines (143 loc) · 2.77 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
/*
* https://github.com/rafaelvanoni/C.git
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <assert.h>
#include <stdbool.h>
#define MIN_SZ (10)
#define MAX_SZ (12)
#define MAX_VAL (99)
struct node {
int val;
struct node *next;
};
void
stack_push(struct node **s, struct node *n)
{
assert(s != NULL);
assert(n != NULL);
n->next = *s;
*s = n;
}
struct node *
stack_pop(struct node **s)
{
struct node *n;
assert(s != NULL);
n = *s;
if (*s != NULL) {
*s = n->next;
}
return (n);
}
bool
stack_is_empty(struct node **s)
{
struct node *n;
if ((n = stack_pop(s)) == NULL) {
return (true);
}
stack_push(s, n);
return (false);
}
int
stack_count(struct node **s)
{
struct node *t = NULL, *n;
int c = 0;
while ((n = stack_pop(s)) != NULL) {
stack_push(&t, n);
c++;
}
assert(stack_is_empty(s));
while ((n = stack_pop(&t)) != NULL) {
stack_push(s, n);
}
assert(stack_is_empty(&t));
return (c);
}
struct node *
stack_reverse(struct node **s)
{
struct node *r = NULL, *n;
while ((n = stack_pop(s)) != NULL) {
stack_push(&r, n);
}
assert(stack_is_empty(s));
return (r);
}
int
stack_peek(struct node *s)
{
if (s != NULL) {
return (s->val);
}
return (-1);
}
void
stack_sort(struct node **s)
{
struct node *t = NULL, *n;
int peek;
while (!stack_is_empty(s)) {
n = stack_pop(s);
while (!stack_is_empty(&t) && n->val < stack_peek(t)) {
stack_push(s, stack_pop(&t));
}
stack_push(&t, n);
}
while ((n = stack_pop(&t)) != NULL) {
stack_push(s, n);
}
}
int
main(int argc, char **argv)
{
struct node *s = NULL, *n, *rev = NULL;
int i, n_nodes;
srand(time(NULL));
do {
n_nodes = MIN_SZ + (rand() % MAX_SZ);
} while (n_nodes < MIN_SZ || n_nodes > MAX_SZ);
printf("creating a stack with %d nodes\n", n_nodes);
for (i = 0; i < n_nodes; i++) {
n = calloc(1, sizeof (struct node));
n->val = rand() % MAX_VAL;
printf("pushing %d\n", n->val);
stack_push(&s, n);
}
printf("stack has %d items\n", stack_count(&s));
while ((n = stack_pop(&s)) != NULL) {
printf("popped %d\n", n->val);
free(n);
}
printf("creating a stack with %d nodes\n", n_nodes);
for (i = 0; i < n_nodes; i++) {
n = calloc(1, sizeof (struct node));
n->val = rand() % MAX_VAL;
printf("pushing %d\n", n->val);
stack_push(&s, n);
}
printf("reversing the stack..\n");
rev = stack_reverse(&s);
while ((n = stack_pop(&rev)) != NULL) {
printf("popped %d\n", n->val);
free(n);
}
printf("creating a stack with %d nodes\n", n_nodes);
for (i = 0; i < n_nodes; i++) {
n = calloc(1, sizeof (struct node));
n->val = rand() % MAX_VAL;
printf("pushing %d\n", n->val);
stack_push(&s, n);
}
printf("sorting the stack..\n");
stack_sort(&s);
while ((n = stack_pop(&s)) != NULL) {
printf("popped %d\n", n->val);
free(n);
}
return (0);
}