-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.c
316 lines (297 loc) · 8.94 KB
/
linkedlist.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdarg.h>
#include "linkedlist.h"
#include "talloc.h"
/* Create a new NULL_TYPE value node. */
Value *makeNull() {
Value *new = talloc(sizeof(Value));
new->type = NULL_TYPE;
return new;
}
/* Create a new VOID_TYPE value node. */
Value *makeVoid() {
Value *new = talloc(sizeof(Value));
new->type = VOID_TYPE;
return new;
}
/* Create a new BOOL_TYPE value node. */
Value *makeBool(int boolean) {
Value *new = talloc(sizeof(Value));
new->type = BOOL_TYPE;
new->i = boolean;
return new;
}
/* Create a new UNSPECIFIED_TYPE value node. */
Value *makeUnspecified() {
Value *new = talloc(sizeof(Value));
new->type = UNSPECIFIED_TYPE;
return new;
}
/* Create a new CONS_TYPE value node. */
Value *cons(Value *newCar, Value *newCdr) {
Value *new = talloc(sizeof(Value));
new->type = CONS_TYPE;
new->c.car = newCar;
new->c.cdr = newCdr;
return new;
}
struct format_info {
int first_in_list, leading_space, is_list;
};
/* Print values in a cons list. Returns 1 if next displayed value should have
* a leading space (excluding NULL_TYPE). Otherwise returns 0. */
int displayHelper(Value *list, struct format_info *info, FILE *fd) {
struct format_info car_info, cdr_info;
int rax = 0;
if (info->leading_space && list->type != NULL_TYPE)
fprintf(fd, " ");
if (info->first_in_list) {
switch (list->type) {
case CONS_TYPE:
case NULL_TYPE:
fprintf(fd, "(");
default:
;
}
}
if (info->is_list) {
// Should be CONS_TYPE or NULL_TYPE, or print a .
switch (list->type) {
case CONS_TYPE:
case NULL_TYPE:
break;
default:
fprintf(fd, ". ");
}
}
switch(list->type) {
case INT_TYPE:
fprintf(fd, "%d", list->i);
rax = 1;
break;
case DOUBLE_TYPE:
fprintf(fd, "%lf", list->d);
rax = 1;
break;
case STR_TYPE:
fprintf(fd, "\"");
fprintf(fd, "%s", list->s);
fprintf(fd, "\"");
rax = 1;
break;
case CONS_TYPE:
car_info.first_in_list = 1;
car_info.leading_space = 0;
car_info.is_list = 0;
cdr_info.first_in_list = 0;
cdr_info.leading_space = displayHelper(list->c.car, &car_info, fd);
cdr_info.is_list = 1;
rax = displayHelper(list->c.cdr, &cdr_info, fd);
break;
case NULL_TYPE:
fprintf(fd, ")");
rax = 1;
break;
case PTR_TYPE:
fprintf(fd, "%p", list->p);
rax = 1;
break;
case BOOL_TYPE:
if (list->i == 0)
fprintf(fd, "#f");
else
fprintf(fd, "#t");
rax = 1;
break;
case SYMBOL_TYPE:
fprintf(fd, "%s", list->s);
rax = 1;
break;
case DOT_TYPE:
fprintf(fd, ".");
rax = 1;
break;
case SINGLEQUOTE_TYPE:
fprintf(fd, "'");
rax = 0;
break;
case VOID_TYPE:
rax = 0;
break;
case CLOSURE_TYPE:
fprintf(fd, "#<procedure>");
rax = 1;
break;
case UNSPECIFIED_TYPE:
fprintf(fd, "#<unspecified>");
rax = 1;
break;
default:
fprintf(stderr, "WARNING: Value type %d should not be printable\n", list->type);
}
if (info->is_list) {
switch (list->type) {
case CONS_TYPE:
case NULL_TYPE:
break;
default:
fprintf(fd, ")");
return 1;
}
}
return rax;
}
/* Display the contents of the linked list to the given file descriptor in some
* kind of readable format. */
void display_to_fd(Value *list, FILE *fd) {
struct format_info info = {1, 0, 0};
displayHelper(list, &info, fd);
fprintf(fd, "\n");
}
/* Display the contents of the linked list to the screen in some kind of
* readable format. */
void display(Value *list) {
display_to_fd(list, stdout);
}
/* Return a new list that is the reverse of the one that is passed in. No stored
* data within the linked list should be duplicated; rather, a new linked list
* of CONS_TYPE nodes should be created, that point to items in the original
* list. */
Value *reverse(Value *list) {
Value *newh, *current;
Value *new = makeNull();
assert(list != NULL);
current = list;
while (current->type == CONS_TYPE) {
newh = talloc(sizeof(Value));
newh->type = CONS_TYPE;
newh->c.car = current->c.car;
newh->c.cdr = new;
current = current->c.cdr;
assert(current != NULL);
new = newh;
}
if (current->type != NULL_TYPE) {
fprintf(stderr, "ERROR: In procedure reverse: Wrong type argument: ");
display_to_fd(list, stderr);
texit(1);
}
return new;
}
/* Utility to make it less typing to get car value. Use assertions to make sure
* that this is a legitimate operation. */
Value *car(Value *list) {
assert(list != NULL);
assert(list->type == CONS_TYPE);
assert(list->c.car != NULL);
return list->c.car;
}
/* Utility to make it less typing to get cdr value. Use assertions to make sure
* that this is a legitimate operation. */
Value *cdr(Value *list) {
assert(list != NULL);
assert(list->type == CONS_TYPE);
assert(list->c.cdr != NULL);
return list->c.cdr;
}
/* Utility to check if pointing to a NULL_TYPE value. Use assertions to make sure
* that this is a legitimate operation. */
bool isNull(Value *value) {
assert(value != NULL);
return (value->type == NULL_TYPE);
}
/* Measure length of list. Use assertions to make sure that this is a legitimate
* operation. */
int length(Value *value) {
Value *current = value;
int len = 0;
assert(current != NULL);
while (current->type == CONS_TYPE) {
len++;
current = current->c.cdr;
assert(current != NULL);
}
if (current->type != NULL_TYPE) {
fprintf(stderr, "ERROR: In procedure length: Wrong type argument: ");
display_to_fd(value, stderr);
texit(1);
}
return len;
}
/* Duplicates a list by creating new cons cells for each entry, but preserving
* the original car values. Does not duplicate the final NULL_TYPE list.
* Returns a pointer to the head of the new list. If the cdr of the head is
* NULL_TYPE, then updates the value at tail to be the address of head, which
* is the last non-NULL_TYPE cons cell in the list. */
Value *duplicateList(Value *list, Value **tail) {
Value *new;
assert(list != NULL);
new->type = CONS_TYPE;
switch (list->type) {
case CONS_TYPE:
new = talloc(sizeof(Value));
new->c.car = list->c.car;
new->c.cdr = duplicateList(list->c.cdr, tail);
if (new->c.cdr->type == NULL_TYPE)
*tail = list;
break;
case NULL_TYPE:
return list;
default:
assert(list->type == CONS_TYPE || list->type == NULL_TYPE);
}
return new;
}
/* Takes an integer indicating the total number of lists given as arguments.
* Creates a copy of the first list and appends a copy of each following list
* to the end of the previous list. Creates new cons cells, but not new values. */
Value *append(int num, ...) {
int i;
va_list lists;
Value *list = NULL, *tail = NULL, *new, *new_tail;
va_start(lists, num);
for (i = 0; i < num; i++) {
new = va_arg(lists, Value*);
assert(new != NULL);
switch (new->type) {
case CONS_TYPE:
new = duplicateList(new, &new_tail); // duplicateList cannot return NULL
if (tail == NULL)
list = new;
else
tail->c.cdr = new;
tail = new_tail;
case NULL_TYPE:
break;
default:
assert(new->type == CONS_TYPE || new->type == NULL_TYPE);
}
}
va_end(lists);
if (list == NULL) {
list = makeNull();
}
return list;
}
/* Takes an integer indicating the total number of values given as arguments.
* Creates a linked list via cons cells pointing to each original value. */
Value *list(int num, ...) {
int i;
va_list values;
Value *list, *tail, *new;
if (num == 0)
return makeNull();
va_start(values, num);
new = va_arg(values, Value*);
list = cons(new, NULL); // cons cannot return NULL
tail = list;
for (i = 1; i < num; i++) {
new = va_arg(values, Value*);
tail->c.cdr = cons(new, NULL); // cannot be NULL
tail = tail->c.cdr;
}
tail->c.cdr = makeNull();
return list;
}