1
+ abstract type AbstractLinkedList end
2
+
3
+ mutable struct ForwardList{T} <: AbstractLinkedList
4
+ dummy:: DummyNode{T}
5
+ current:: Union{DummyNode{T}, ForwardNode{T}}
6
+ length:: Int
7
+ nodeConstructor:: Function
8
+ end
9
+
10
+ function ForwardList (:: Type{T} ) where T
11
+ dummy = DummyNode {T} (nothing )
12
+
13
+ return ForwardList {T} (dummy, dummy, 0 , (data:: T ) -> ForwardNode (data))
14
+ end
15
+
16
+ mutable struct List{T} <: AbstractLinkedList
17
+ dummy:: DummyNode{T}
18
+ current:: Union{DummyNode{T}, ListNode{T}}
19
+ length:: Int
20
+ nodeConstructor:: Function
21
+ end
22
+
23
+ function List (:: Type{T} ) where T
24
+ dummy = DummyNode {T} (nothing )
25
+ return List {T} (dummy, dummy, 0 , (data:: T ) -> ListNode (data))
26
+ end
27
+
28
+ dummy (list:: AbstractLinkedList ) = list. dummy
29
+ current (list:: AbstractLinkedList ) = list. current
30
+ length (list:: AbstractLinkedList ) = list. length
31
+
32
+ keys (list:: AbstractLinkedList ) = next (dummy (list))
33
+ isempty (list:: AbstractLinkedList ) = length (list) == 0
34
+
35
+ nodeConstructor (list:: AbstractLinkedList ) = list. nodeConstructor
36
+
37
+ function push! (list:: AbstractLinkedList , data:: T ) where T
38
+ list. length += 1
39
+ newnode = nodeConstructor (list)(data)
40
+
41
+ insertNext! (list. current, newnode)
42
+ list. current = next (list. current)
43
+ end
44
+
45
+ function pushfirst! (list:: ForwardList , data:: T ) where T
46
+ # TODO
47
+ end
48
+
49
+ function pushfirst! (list:: List , data:: T ) where T
50
+ list. length += 1
51
+ newnode:: ListNode = nodeConstructor (list)(data)
52
+ unlink:: Union{Nothing, ListNode} = next (dummy (list))
53
+
54
+ if ! isnothing (unlink)
55
+ insertNext! (newnode, unlink)
56
+ else
57
+ newnode. next = nothing
58
+ newnode. prev = dummy (list)
59
+ list. current = newnode
60
+ end
61
+
62
+ insertNext! (dummy (list), newnode)
63
+ end
64
+
65
+ function pop! (list:: T ) where T <: AbstractLinkedList
66
+ if isempty (list)
67
+ throw (BadOperationException (" the list is empty" ))
68
+ end
69
+
70
+ list. length -= 1
71
+
72
+ prevnode = prev (current (list), dummy (list))
73
+ target = next (prevnode)
74
+ removeNext! (prevnode)
75
+ list. current = prevnode
76
+
77
+ return dataof (target)
78
+ end
79
+
80
+ function popfirst! (list:: T ) where T <: AbstractLinkedList
81
+ if isempty (list)
82
+ throw (BadOperationException (" the list is emtpy" ))
83
+ end
84
+
85
+ list. length -= 1
86
+ prevnode = dummy (list)
87
+ target = next (prevnode)
88
+ removeNext! (prevnode)
89
+
90
+ if isempty (list)
91
+ list. current = dummy (list)
92
+ end
93
+
94
+ return dataof (target)
95
+ end
96
+
97
+ function popat! (list:: L , position:: T ) where {L <: AbstractLinkedList , T <: AbstractConsNode }
98
+ list. length -= 1
99
+
100
+ prevnode = prev (position, dummy (list))
101
+ removeNext! (prevnode)
102
+
103
+ if isempty (list)
104
+ list. current = dummy (list)
105
+ end
106
+
107
+ dataof (position)
108
+ end
109
+
110
+ function pushnext! (list:: L , position:: E , data:: T ) where {L <: AbstractLinkedList , E <: AbstractConsNode , T}
111
+ list. length += 1
112
+ newnode = nodeConstructor (list)(data)
113
+ unlink = next (position)
114
+ insertNext! (newnode, unlink)
115
+ insertNext! (position, newnode)
116
+ end
117
+
118
+ function iterate (list:: T ) where T <: AbstractLinkedList
119
+ firstNode = next (dummy (list))
120
+ return if isnothing (firstNode)
121
+ firstNode
122
+ else
123
+ dataof (firstNode), next (firstNode)
124
+ end
125
+ end
126
+
127
+ function iterate (:: T , state:: E ) where {T <: AbstractLinkedList , E <: Union{AbstractConsNode, Nothing} }
128
+ return if isnothing (state)
129
+ nothing
130
+ else
131
+ dataof (state), next (state)
132
+ end
133
+ end
134
+
135
+ replace! (node:: Union{ForwardNode{T}, ListNode{T}} , data:: T ) where T = node. data = data
136
+
137
+ function first (list:: AbstractLinkedList )
138
+ firstnode = next (dummy (list))
139
+
140
+ if isnothing (firstnode)
141
+ throw (BadOperationException (" there is no elements in the list" ))
142
+ end
143
+
144
+ return dataof (firstnode)
145
+ end
146
+
147
+ function last (list:: AbstractLinkedList )
148
+ lastnode = current (list)
149
+
150
+ if isnothing (lastnode)
151
+ throw (BadOperationException (" there is no elements in the list" ))
152
+ end
153
+
154
+ return dataof (lastnode)
155
+ end
156
+
157
+ function contains (list:: AbstractLinkedList , data:: T ):: Bool where T
158
+ for value in list
159
+ if value == data
160
+ return true
161
+ end
162
+ end
163
+
164
+ return false
165
+ end
166
+
167
+ function show (io:: IO , list:: AbstractLinkedList )
168
+ print (io, " list: " )
169
+
170
+ for value in list
171
+ print (io, value, ' ' )
172
+ end
173
+ end
174
+
175
+ function filter (pred:: Function , list:: ForwardList{T} ):: ForwardList{T} where T
176
+ result = ForwardList (T)
177
+
178
+ for value in list
179
+ if pred (value)
180
+ push! (result)
181
+ end
182
+ end
183
+
184
+ result
185
+ end
186
+
187
+ function filter (pred:: Function , list:: List{T} ):: List{T} where T
188
+ result = ForwardList (T)
189
+
190
+ for value in list
191
+ if pred (value)
192
+ push! (result)
193
+ end
194
+ end
195
+
196
+ result
197
+ end
0 commit comments