-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.h
75 lines (64 loc) · 1.43 KB
/
list.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
#pragma once
#include "sexpr.h"
#define list_head(list) ((list)->head)
#define list_tail(list) ((list)->tail)
static inline
void list_init(list_sexpr_t* list)
{
list->type = SEXPR_LIST;
list->head = NIL;
list->foot = NIL;
list->tail = LNIL;
}
static inline
list_sexpr_t* list_create()
{
list_sexpr_t* list = (list_sexpr_t*)malloc(sizeof(list_sexpr_t));
list_init(list);
return list;
}
static inline void list_set_head(list_sexpr_t* list, sexpr_t* head)
{
list->head = head;
}
static inline
void list_set_tail(list_sexpr_t* list, sexpr_t* elem)
{
list_sexpr_t* new_list = list_create();
list_set_head(new_list, elem);
list->tail = new_list;
}
static inline
list_sexpr_t* list_get_last(list_sexpr_t* list)
{
list_sexpr_t* p = list;
while(p != LNIL && p->tail != LNIL) {
p = p->tail;
}
return p;
}
static inline
int list_is_pair_notation(list_sexpr_t* list)
{
return (list_get_last(list)->foot != NULL);
}
static inline
void list_set_foot(list_sexpr_t* list, sexpr_t* elem)
{
list_get_last(list)->foot = elem;
}
static inline
void list_append(list_sexpr_t* list, sexpr_t* elem)
{
if(list_head(list) == NIL) {
list_set_head(list, elem);
}
else if(list_tail(list) == LNIL) {
list_set_tail(list, elem);
}
else {
list_sexpr_t* p = list->tail;
while(p->tail != LNIL) p = p->tail;
list_set_tail(p, elem);
}
}