/
connected_components_linked_list.rb
76 lines (65 loc) · 1.42 KB
/
connected_components_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
class DoublyLinkedList
attr_accessor :head, :tail, :hash, :group_count
def initialize
@head = nil
@tail = nil
@hash = {}
@group_count = 0
end
def makeList(arr)
references = []
arr.each do |val|
new_node = Node.new(val)
if rand(0..1) == 1
references << new_node.val
end
add_node(new_node)
end
return references
end
def add_node(node)
if !@tail && !@head
@tail = node
@head = node
return
end
@tail.next = node
node.prev = @tail
@tail = node
end
def create_hash(nodes)
nodes.each do |node|
@hash[node] = true
end
end
def find_around(node)
current = node
while node && @hash[node]
@hash.delete(node)
node = node.next
end
while current && @hash[current]
@hash.delete(current)
node = node.prev
end
@group_count += 1
end
end
class Node
attr_accessor :prev, :next, :val
def initialize(val)
@prev = nil
@next = nil
@val = val
end
end
dl = DoublyLinkedList.new
references = dl.makeList((1..20).to_a)
p references
dl.create_hash(references)
ref = nil
while !dl.hash.empty?
ref = dl.hash.first[0]
dl.find_around(ref)
end
p dl.group_count