-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list_delete.rb
76 lines (58 loc) · 1.42 KB
/
linked_list_delete.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
class Node
attr_accessor :data
attr_accessor :next_node
def initialize(data, next_node)
@data = data
@next_node = next_node
end
def self.delete(head, position)
return nil if head == nil || head.next_node == nil
return head.next_node if position == 0
prev = nil
curr = head
position.times do |i|
prev = curr
curr = curr.next_node
end
#curr is the node to be deleted
prev.next_node = curr.next_node
return head
end
end
def assert(actual, expected)
raise "actual: #{actual}, expected: #{expected}" unless expected == actual
end
def delete_it_accepts_nil_parameter
assert(Node.delete(nil, 0), nil)
end
def delete_it_accepts_single_node
head = Node.new(100, nil)
assert(Node.delete(head, 0), nil)
end
def delete_it_deletes_the_head
tail = Node.new(200, nil)
head = Node.new(100, tail)
assert(Node.delete(head, 0), tail)
end
def delete_it_deletes_a_middle_element
tail = Node.new(200, nil)
two = Node.new(50, tail)
one = Node.new(25, two)
head = Node.new(100, one)
Node.delete(head, 2)
assert(one.next_node.data, tail.data)
end
def delete_it_deletes_the_tail
tail = Node.new(200, nil)
two = Node.new(50, tail)
one = Node.new(25, two)
head = Node.new(100, one)
Node.delete(head, 3)
assert(two.next_node, nil)
end
delete_it_accepts_nil_parameter()
delete_it_accepts_single_node()
delete_it_deletes_the_head()
delete_it_deletes_a_middle_element()
delete_it_deletes_the_tail()
puts 'Passed'