-
Notifications
You must be signed in to change notification settings - Fork 5
/
single_linked_list_extra.rb
93 lines (79 loc) · 1.68 KB
/
single_linked_list_extra.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
class Node
attr_accessor :value, :next_node
def initialize(value, next_node = nil)
@value = value
@next_node = next_node
end
end
class LinkedList
#setup head and tail
def initialize(val=nil)
@head = Node.new(val)
@tail = Node.new(val)
end
def add(number)
node = Node.new(number)
if @head.value.nil?
@head = node
@tail = node
else
@tail.next_node = node
@tail = node
end
end
def get(index)
current = @head
count = 0
while count < index && !current.next_node.nil?
current = current.next_node
count += 1
puts count
return current.value if count == index
end
puts count
return nil if count < index
return current.value if count = index
end
def get_node(index)
current = @head
count = 0
while count < index && !current.next_node.nil?
current = current.next_node
count += 1
return current if count == index
end
return nil if count < index
return current if count = index
end
def add_at(index, val)
new_node = Node.new(val,get_node(index))
if index == 0
@head = new_node
end
unless get_node(index).value.nil?
get_node(index-1).next_node = new_node if index-1 >= 0
end
end
def delete(index)
if index == 0
@head = @head.next_node
return
end
unless get_node(index).value.nil?
get_node(index-1).next_node = get_node(index).next_node if index-1 >= 0
end
end
end
list = LinkedList.new
list.add(3)
list.add(5)
list.add_at(1, 11)
list.add_at(0, 13)
puts list.get(0)
puts list.get(1)
puts list.get(2)
# => 11
puts list.get(3)
# => 5
list.delete(2)
puts list.get(2)