Module: Ds101::LinkedList::Common

Included in:
DoubleLinkedList, SingleLinkedList
Defined in:
lib/ds_101/linked_list/common.rb

Instance Method Summary collapse

Instance Method Details

#clearObject



44
45
46
# File 'lib/ds_101/linked_list/common.rb', line 44

def clear
  remove_head while length.positive?
end

#concat(other_list) ⇒ Object



30
31
32
33
34
# File 'lib/ds_101/linked_list/common.rb', line 30

def concat(other_list)
  other_list.each do |node|
    append(node.value)
  end
end

#eachObject



22
23
24
25
26
27
28
# File 'lib/ds_101/linked_list/common.rb', line 22

def each
  current = head
  while current
    yield current
    current = current.next
  end
end

#find(value) ⇒ Object



6
7
8
9
10
# File 'lib/ds_101/linked_list/common.rb', line 6

def find(value)
  current = head
  current = current.next while current && current.value != value
  current
end

#find_by(&block) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/ds_101/linked_list/common.rb', line 12

def find_by(&block)
  current = head
  while current
    return current if block.call(current)

    current = current.next
  end
  nil
end

#to_aObject



36
37
38
39
40
41
42
# File 'lib/ds_101/linked_list/common.rb', line 36

def to_a
  arr = []
  each do |node|
    arr << node.value
  end
  arr
end