-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlist.blink
145 lines (116 loc) · 3.5 KB
/
list.blink
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
/// A simple linked-list implementation in Blink.
class List {
var head: Node = new Node("head")
func add(e: Object): List = {
head.add(e)
this
}
func size(): Int = if (head.next() == null) 0 else head.next().size()
func isEmpty(): Bool = head.next() == null
func get(index: Int): Object = if (head.next() == null) null else head.next().get(index)
func set(index: Int, e: Object) = if (head.next() != null) head.next().set(index, e)
func indexOf(e: Object): Int = if (head.next() == null) -1 else head.next().indexOf(e)
func removeAt(index: Int): Object = if (index == 0) removeFront() else head.next().removeAt(index)
private func removeFront(): Object = {
if (head.next() == null) null
else {
let data = head.next().data() in {
head.setNext(head.next().next())
data
}
}
}
override func toString(): String = if (head.next() == null) "[]" else head.next().toString()
}
class Node(data: Object) {
var next: Node = null
func data(): Object = data
func setData(e: Object) = data = e
func next(): Node = next
func setNext(n: Node) = next = n
func add(e: Object): Node = {
let node = this in {
while (node.next() != null)
node = node.next()
node.setNext(new Node(e))
}
this
}
func get(index: Int): Object = {
if (index < 0) null
else {
let i = 0, node = this in {
while (i < index && node != null) {
i += 1
node = node.next()
}
if (node != null) node.data() else null
}
}
}
func set(index: Int, e: Object) = {
if (index >= 0) {
let i = 0, node = this in {
while (i < index && node != null) {
i += 1
node = node.next()
}
if (node != null)
node.setData(e)
}
}
}
func removeAt(index: Int): Object = {
if (index < 0) null
else {
let i = 0, node = this in {
while (i < index - 1 && node != null) {
i += 1
node = node.next()
}
removeNext(node)
}
}
}
func indexOf(e: Object): Int = {
if (e == null) -1
else {
let index = 0, node = this in {
while (node != null && node.data() != e) {
index += 1
node = node.next()
}
if (node == null) -1 else index
}
}
}
func size(): Int = {
let size = 0, node = this in {
while (node != null) {
size += 1
node = node.next()
}
size
}
}
private func removeNext(node: Node): Object = {
if (node == null) null
else {
if (node.next() != null) {
let data = node.next().data() in {
node.setNext(node.next().next())
data
}
}
}
}
override func toString(): String = {
let str = "[", node = this in {
while (node.next() != null) {
str += node.data() + ", "
node = node.next()
}
str + node.data() + "]"
}
}
}