-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.c
176 lines (167 loc) · 4.31 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
/*
* proj1: Linked list
* Due: Friday, 04/27 at 22:00
* Yitong Chen, Yingying Wang, Megan Zhao
*
* This project implements the Scheme list as an linkedlist
* using C.
*/
#include "linkedlist.h"
#include "talloc.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "talloc.h"
/*
* Create an empty list (a new Value object of type NULL_TYPE).
*
* Returns a pointer to an empty list.
* If memory allocation fails, returns a null pointer.
*/
Value *makeNull() {
Value *nullList = talloc(sizeof(Value));
if (!nullList) {
printf("Out of memory!\n");
return nullList;
}
nullList->type = NULL_TYPE;
return nullList;
}
/*
* Create a nonempty list (a new Value object of type CONS_TYPE).
*
* Returns a pointer to a non-empty list.
* If memory allocation fails, returns a null pointer.
* Asserts that car is not an empty list.
*/
Value *cons(Value *car, Value *cdr) {
assert(car != NULL && cdr != NULL);
struct ConsCell cell;
cell.car = car;
cell.cdr = cdr;
Value *newValue =talloc(sizeof(Value));
if (!newValue) {
printf("Out of memory!\n");
return newValue;
}
newValue->type = CONS_TYPE;
newValue->c = cell;
return newValue;
}
/*
* Print a representation of the contents of a linked list.
*
* Asserts that input is a list (Value of type CONS_TYPE or
* NULL_TYPE).
*/
void display(Value *list){
assert(list != NULL
&& (list->type == CONS_TYPE || list->type == NULL_TYPE));
if (list->type == NULL_TYPE) {
printf("()");
}
Value *cur = list;
printf("(");
while(cur->type != NULL_TYPE){
switch(cur->c.car->type){
case INT_TYPE:
printf("%i ",cur->c.car->i);
break;
case DOUBLE_TYPE:
printf("%f ",cur->c.car->d);
break;
case STR_TYPE:
printf("%s ",cur->c.car->s);
break;
case SYMBOL_TYPE:
printf("%s ",cur->c.car->s);
break;
case CONS_TYPE:
printf("(");
display(cur->c.car);
printf(")");
break;
default:
printf(" ");
break;
}
cur = cur->c.cdr;
}
printf(")");
}
/*
* Get the car value of a given list.
*
* Asserts that this function can only be called on a non-empty list
* (Value of type CONS_TYPE).
*/
Value *car(Value *list){
assert(list != NULL && list->type == CONS_TYPE);
return list->c.car;
}
/*
* Get the cdr value of a given list.
*
* Asserts that this function can only be called on a non-empty list
* (Value of type CONS_TYPE).
*/
Value *cdr(Value *list){
assert(list != NULL && list->type == CONS_TYPE);
return list->c.cdr;
}
/*
* Test if the given value is a NULL_TYPE value.
*
* Asserts that the value has been allocated.
*/
bool isNull(Value *value){
assert(value != NULL);
if (value->type == NULL_TYPE){
return true;
} else{
return false;
}
}
/*
* Compute the length of the given list.
*
* Asserts that value has been allocated and that
* value must be a list (Value of type CONS_TYPE
* or NULL_TYPE)
*/
int length(Value *value){
assert(value != NULL &&
(value->type == CONS_TYPE || value->type == NULL_TYPE));
int length = 0;
Value *cur;
cur = value;
while (cur->type != NULL_TYPE){
length++;
cur = cur->c.cdr;
}
return length;
}
/*
* Create a new linked list whose entries correspond to the given list's
* entries, but in reverse order. The resulting list is a shallow copy of the
* original.
*
* Returns pointer to the reversed list.
* If memory allocation fails, returns a null pointer.
* Asserts that the reverse function can only be called on a list.
*/
Value *reverse(Value *list) {
// Reverse can only be applied to an empty list or a non-empty list
assert(list != NULL &&
(list->type == NULL_TYPE || list->type == CONS_TYPE));
// Create new linked list
Value *reversed = makeNull();
if (list->type == NULL_TYPE) {
return reversed;
}
for (Value *cur = list; cur->type != NULL_TYPE; cur = cur->c.cdr) {
reversed = cons(cur->c.car, reversed);
}
return reversed;
}