-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.wl
84 lines (70 loc) · 1.07 KB
/
list.wl
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
use "importc"
import(C) "/usr/include/stdlib.h"
struct Node
{
void^ next
void^ prev
void^ val
}
struct List
{
Node^ first
Node^ ptr
}
List^ list_new()
{
List^ l = malloc(16)
l.first = null
l.ptr = null
return l
}
void list_add(List^ l, void^ v)
{
Node ^n = malloc(32)
n.prev = null
n.next = l.first
if(l.first != null)
l.first.prev = n
n.val = v
l.first = n
}
void list_begin(List^ l)
{
if(l != null)
l.ptr = l.first
}
bool list_end(List ^l)
{
return l.ptr == null
}
void^ list_next(List ^l)
{
if(l.ptr != null) {
l.ptr = l.ptr.next
if(l.ptr != null)
return l.ptr.val
}
return int8^: null
}
void^ list_get(List^ l)
{
return l.ptr.val
}
void^ list_remove(List ^l)
{
void^ val = l.ptr.val
Node^ prev = l.ptr.prev
Node^ next = l.ptr.next
Node^ n = l.ptr
if(prev != null)
{
prev.next = next
} else
{
l.first = next
}
if(next != null)
next.prev = prev
l.ptr = next
return val
}