-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.rb
188 lines (149 loc) · 3.32 KB
/
linked_list.rb
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
178
179
180
181
182
183
184
185
186
187
188
class LinkedList
class Node
attr_reader :value, :next_node
attr_accessor :next_node
def initialize(value, next_node)
@value = value
@next_node = next_node
end
end
attr_reader :head, :tail, :size
def initialize
@head = nil
@tail = nil
@size = 0
end
# O(1)
def insert(value)
new_node = Node.new(value, nil)
if @head.nil?
@head = new_node
else
@tail.next_node = new_node
end
@tail = new_node
@size += 1
new_node
end
# O(n)
def find(value)
return if @head.nil?
return @head if @head.value == value
return @tail if @tail.value == value
current_node = @head
while current_node do
break current_node if current_node.value == value
current_node = current_node.next_node
end
current_node
end
# O(n)
# O(1) if deleting from head
def delete(value)
if @head.value == value && @tail.value == value
@head = nil
@tail = nil
@size -= 1
return
end
if @head.value == value
@head = @head.next_node
@size -= 1
return
end
current_node = @head
while current_node && current_node.next_node do
if current_node.next_node.value == value
if current_node.next_node == @tail
current_node.next_node = nil
@tail = current_node
@size -= 1
break
else
current_node.next_node = current_node.next_node.next_node
@size -= 1
break
end
end
current_node = current_node.next_node
end
end
# O(n)
def each
current_node = @head
while current_node do
yield current_node
current_node = current_node.next_node
end
end
end
require 'test/unit'
class TestLinkedList < Test::Unit::TestCase
def test_insert_and_size
list = LinkedList.new
assert_equal(0, list.size)
list.insert(10)
assert_equal(1, list.size)
list.insert(20)
assert_equal(2, list.size)
end
def test_empty_list
list = LinkedList.new
assert_nil(list.head)
assert_nil(list.tail)
assert_equal(0, list.size)
end
def test_delete
list = LinkedList.new
list.insert(10)
list.insert(20)
list.insert(30)
assert_equal(3, list.size)
assert_equal(20, list.head.next_node.value)
# delete from middle
list.delete(20)
assert_equal(2, list.size)
assert_equal(10, list.head.value)
assert_equal(30, list.tail.value)
# delete from head
list.delete(10)
assert_equal(1, list.size)
assert_equal(30, list.head.value)
assert_equal(30, list.tail.value)
# delete from tail
list.delete(30)
assert_equal(0, list.size)
assert_nil(list.head)
assert_nil(list.tail)
end
def test_find
list = LinkedList.new
list.insert(10)
list.insert(20)
list.insert(30)
assert_equal(10, list.find(10).value)
assert_equal(20, list.find(20).value)
assert_equal(30, list.find(30).value)
end
def test_each
list = LinkedList.new
list.insert(10)
list.insert(20)
list.insert(30)
values = []
list.each { |node| values << node.value }
assert_equal([10, 20, 30], values)
end
end
list = LinkedList.new
list.insert(1)
list.insert(2)
list.insert(3)
p list.size
p list.head
p list.tail
list.delete(3)
puts "\n"
p list.size
p list.head
p list.tail