-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathlinked_list.jl
177 lines (149 loc) · 5.12 KB
/
linked_list.jl
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""
Linked List
# Brief:
A linked list is a data structure where each element is connected to the next one.
# Complexity of some operations
- insert - O(N)
- insert to front - O(1)
- delete - O(N)
- delete first - O(1)
- get element - O(N)
# Functions
- create_node(val, next=missing) - Create node with value 'val' and the pointer to the next node ( missing by default )
- create_list(n::Int=0, val=missing) - Create root node of the list with n elements with value set to 'val'
- insert(list::Node, new_node::Node, index::Int=1) - Add a new node to the list at the specified index
- insert(list::Node, val, index::Int=1) - Create a new node with value 'val' in the list at the specified index
- push_back(list::Node, node::Node) - Add a new node to the end of the list
- push_back(list::Node, val) - Add a new node with value 'val' to the end of the list
- return_as_array(list::Node) - Return the array representation of the list
- clear(list::Node) - Remove all elements from the list
- remove(list::Node, index::Int) - Remove an element at the specified index
- remove_all(list::Node, val) Remove all elements with the value 'val'
- remove_first(list::Node, val) - Remove the first element in the list with value 'val'
- get_node(list::Node, index::Int) - Get a node at the specified index
- get(list::Node, index::Int) - Get a value from the node at the specified index
- indexOf(list::Node, val) - Return the index of the first element with a value 'val'
- is_empty(list) - Return true if list is empty, false if it has elements
# Contributed by: [Nikola Mircic](https://github.com/Nikola-Mircic)
"""
module LinkedList
mutable struct Node
val::Any # Value of the node
next::Any # Pointer to the next node
Node(val, next = missing) = new(val, next)
end
# Create node with value 'val' and the pointer to the next node ( missing by default )
function create_node(val, next = missing)
return Node(val, next)
end
# Create root node of the list
function create_list(n::Int = 0, val = missing)
root_node = create_node("root") # Root node with value "root"
current_node = root_node
for i in 1:n
new_node = create_node(val, missing)
current_node.next = new_node
current_node = current_node.next
end
return root_node
end
# Add a new node to the list at the specified index
function insert(list::Node, new_node::Node, index::Int = 1)
current_node = get_node(list, index - 1)
next = current_node.next
current_node.next = new_node
return new_node.next = next
end
# Create a new node with value 'val' in the list at the specified index
function insert(list::Node, val, index::Int = 1)
new_node = create_node(val)
return insert(list, new_node, index)
end
# Add a new node to the end of the list
function push_back(list::Node, node::Node)
current_node = list
while !ismissing(current_node.next)
current_node = current_node.next
end
return current_node.next = node
end
# Add a new node with value 'val' to the end of the list
function push_back(list::Node, val)
new_node = create_node(val)
return push_back(list, new_node)
end
function return_as_array(list::Node)
arr = Array{Any}(missing, 0)
node = list.next
while !ismissing(node)
push!(arr, node.val)
node = node.next
end
return arr
end
# Remove all elements from the list
function clear(list::Node)
return list.next = missing
end
# Remove an element at the specified index
function remove(list::Node, index::Int)
node_before = get_node(list, index - 1)
to_remove = node_before.next
return node_before.next = to_remove.next
end
# Remove all elements with the value 'val'
function remove_all(list::Node, val)
current_node = list
while !ismissing(current_node.next)
next_node = current_node.next
if next_node.val == val
current_node.next = next_node.next
else
current_node = next_node
end
end
end
# Remove the first element in the list with value 'val'
function remove_first(list::Node, val)
index = indexOf(list, val)
return remove(list, index)
end
# Get a node at the specified index
function get_node(list::Node, index::Int)
current_node = list
for i in 1:index
if !ismissing(current_node)
current_node = current_node.next
else
return missing
end
end
return current_node
end
# Get a value from the node at the specified index
function get(list::Node, index::Int)
node = get_node(list, index)
if !ismissing(node)
return node.val
end
return missing
end
# Return the index of the first element with a value 'val'
function indexOf(list::Node, val)
current_node = list.next
i = 1
while !ismissing(current_node)
if current_node.val == val
return i
else
current_node = current_node.next
i += 1
end
end
return -1
end
# Return true if list is empty, false if it has elements
function is_empty(list)
return ismissing(list.next)
end
end