-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllist.h
100 lines (85 loc) · 1.6 KB
/
llist.h
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
#ifndef COMP_348_A1_Q5_LLIST_H
#define COMP_348_A1_Q5_LLIST_H
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
typedef enum { ATOM, LIST } eltype;
typedef char atom;
struct _listnode;
typedef struct {
eltype type;
union {
atom a;
struct _listnode* l;
};
} element;
typedef struct _listnode {
element el;
struct _listnode* next;
} * list;
const element NIL = { .type = LIST, .l = NULL };
element aasel(atom a){
element element1 = {.type = ATOM, .a = a};
return element1;
}
element lasel(list l){
element element1 = {.type = LIST, .l = l};
return element1;
}
element car(element e){
if (e.type != LIST){
return NIL;
}
else {
return e.l->el;
}
}
list cdr(element e){
if (e.type != LIST){
return NULL;
}
else {
e.l->el = NIL;
return e.l;
}
}
list cddr(element e){
return cdr(cdr(e)->el);
}
list cons(element e, list l){
list list1 = malloc(sizeof(list));
list1->el = e;
list1->next = l;
return list1;
}
list append(list l1, list l2){
struct _listnode* lptr = l1->next;
while (lptr->next != NULL){
lptr = lptr->next;
}
lptr->next = l2->next;
return l1;
}
void lfreer(list l){
if (l->next != NULL){
lfreer(l->next);
}
else{
free(l);
}
}
void print(element e){
if (e.type == ATOM){
printf("%c", e.a);
}
else if (e.l == NULL){
printf("NIL");
}
else {
printf("(");
print(e.l->el);
print(e.l->next->el);
}
printf(")");
}
#endif //COMP_348_A1_Q5_LLIST_H