Class: Mores::LinkedList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mores/linked_list.rb

Defined Under Namespace

Classes: Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLinkedList

Returns a new instance of LinkedList.



12
13
14
# File 'lib/mores/linked_list.rb', line 12

def initialize
  @length = 0
end

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



9
10
11
# File 'lib/mores/linked_list.rb', line 9

def head
  @head
end

#lengthObject (readonly) Also known as: size

Returns the value of attribute length.



9
10
11
# File 'lib/mores/linked_list.rb', line 9

def length
  @length
end

#tailObject (readonly)

Returns the value of attribute tail.



9
10
11
# File 'lib/mores/linked_list.rb', line 9

def tail
  @tail
end

Instance Method Details

#<<(value) ⇒ Object



55
56
57
# File 'lib/mores/linked_list.rb', line 55

def <<(value)
  insert nil, @head, value
end

#>>(value) ⇒ Object



59
60
61
# File 'lib/mores/linked_list.rb', line 59

def >>(value)
  insert @tail, nil, value
end

#clearObject



63
64
65
66
67
68
# File 'lib/mores/linked_list.rb', line 63

def clear
  each_node { |n| n.send :clear }
  @head = @tail = nil
  @length = 0
  self
end

#count(*args) ⇒ Object



20
21
22
23
# File 'lib/mores/linked_list.rb', line 20

def count(*args)
  return @length if args.empty? && !block_given?
  super
end

#delete(value) {|value| ... } ⇒ Object

Yields:

  • (value)


70
71
72
73
74
# File 'lib/mores/linked_list.rb', line 70

def delete(value)
  last = each_node.reduce(NOMATCH) { |v, n| n.value == value ? n.delete : v }
  return last unless last == NOMATCH
  yield value if block_given?
end

#delete_ifObject



76
77
78
79
# File 'lib/mores/linked_list.rb', line 76

def delete_if
  return to_enum(__callee__) { @length } unless block_given?
  each_node { |n| n.delete if yield n.value }
end

#eachObject



25
26
27
28
# File 'lib/mores/linked_list.rb', line 25

def each
  return to_enum(__callee__) { @length } unless block_given?
  each_node { |n| yield n.value }
end

#each_nodeObject



35
36
37
38
39
40
41
42
43
# File 'lib/mores/linked_list.rb', line 35

def each_node
  return to_enum(__callee__) { @length } unless block_given?
  node = @head
  until node.nil?
    node = (cur = node).next
    yield cur
  end
  self
end

#empty?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/mores/linked_list.rb', line 16

def empty?
  !@head
end

#reverse_eachObject



30
31
32
33
# File 'lib/mores/linked_list.rb', line 30

def reverse_each
  return to_enum(__callee__) { @length } unless block_given?
  reverse_each_node { |n| yield n.value }
end

#reverse_each_nodeObject



45
46
47
48
49
50
51
52
53
# File 'lib/mores/linked_list.rb', line 45

def reverse_each_node
  return to_enum(__callee__) { @length } unless block_given?
  node = @tail
  until node.nil?
    node = (cur = node).prev
    yield cur
  end
  self
end