-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.rb
More file actions
155 lines (133 loc) · 3.05 KB
/
Copy pathlinked_list.rb
File metadata and controls
155 lines (133 loc) · 3.05 KB
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
require "./node.rb"
require 'pry'
class LinkedList
def initialize
@root = Node.new
end
def empty?
@root.next_node == nil
end
def head
@root.next_node && @root.next_node.data
end
def tail
find_tail.data
end
def find_tail
current_node = @root
until current_node.next_node == nil
current_node = current_node.next_node
end
current_node
end
def append(data)
node = Node.new(data)
find_tail.next_node = node
end
def prepend(data)
node = Node.new(data)
if @root.next_node
node.next_node = @root.next_node
end
@root.next_node = node
end
def insert(index, data)
if index > count
unreached_index(index, data)
elsif index <= count
included_index(index, data)
end
end
def unreached_index(index, data)
append(data)
index = count
"List only has #{count} items. #{Data} appended at position #{index}."
end
def included_index(index, data)
node = Node.new(data)
previous = find_node(index - 1)
if previous.next_node
node.next_node = previous.next_node
end
previous.next_node = node
end
def includes?(data)
current_node = @root
while current_node.data != data && current_node.next_node
current_node = current_node.next_node
end
current_node.data == data
end
def pop
tail_data = tail
current_node = @root
until current_node.next_node == nil
previous_node = current_node
current_node = current_node.next_node
end
previous_node.next_node = nil
current_node.data
end
def count
current_node = @root
counter = 0
if current_node.next_node == nil
counter
else
until current_node.next_node == nil
current_node = current_node.next_node
counter += 1
end
counter
end
end
def find_node(index)
current_node = @root
current_node_index = -1
until current_node_index == index
current_node = current_node.next_node
current_node_index += 1
end
current_node
end
def find_by_index(index)
find_node(index).data
end
def find_by_value(data)
current_node = @root
current_node_index = -1
until current_node.data == data
current_node = current_node.next_node
current_node_index += 1
end
current_node_index
end
def remove_by_index(index)
previous_node = find_node(index - 1)
current_node = find_node(index)
previous_node.next_node = current_node.next_node
current_node.data
end
def remove_by_value(data)
current_node = @root
until current_node.data == data
previous_node = current_node
current_node = current_node.next_node
end
previous_node.next_node = current_node.next_node
current_node.data
end
def distance_between(data1, data2)
index1 = find_by_value(data1)
index2 = find_by_value(data2)
(index1 - index2).abs
end
end
if __FILE__ == $0
list = LinkedList.new
list.append("popcorn")
list.append("almonds")
list.append("peanuts")
list.append("apples")
puts list.insert(7, "candy")
end