Class: SknUtils::Lists::LinkedList

Inherits:
LinkedCommons show all
Defined in:
lib/skn_utils/lists/linked_list.rb

Overview

LinkedCommons provides;

  • #initialize, #first, #next, #current, #last, #at_index, #insert, #prepend, #append, #empty?, #clear, #each, #to_a, and #sort!

Instance Attribute Summary

Attributes inherited from LinkedCommons

#size

Instance Method Summary collapse

Methods inherited from LinkedCommons

#at_index, call, #clear, #current, #each, #empty?, #first, #initialize, #last, #next, #nodes, #prepend, #sort!, #to_a

Constructor Details

This class inherits a constructor from SknUtils::Lists::LinkedCommons

Instance Method Details

#insert(value) ⇒ Object Also known as: append

Modifications



59
60
61
62
63
64
65
# File 'lib/skn_utils/lists/linked_list.rb', line 59

def insert(value)
  node = @current
  @current = LinkNode.call(value, node, :single, self,  &@match_value)
  self.head = @current if self.head.nil?
  self.tail = @current if self.tail.equal?(node)
  self.size += 1
end

#insert_after(position_value, value) ⇒ Object

return new size



80
81
82
83
84
85
86
87
# File 'lib/skn_utils/lists/linked_list.rb', line 80

def insert_after(position_value, value)
  prior, target = find_by_value(position_value)
  node = LinkNode.call(value, target, :single, self,  &@match_value)
  self.head = node if self.head.nil?
  self.tail = node if self.tail.equal?(target)
  @current = node
  self.size += 1
end

#insert_before(position_value, value) ⇒ Object

return new size



69
70
71
72
73
74
75
76
77
# File 'lib/skn_utils/lists/linked_list.rb', line 69

def insert_before(position_value, value)
  prior, target = find_by_value(position_value)
  node = LinkNode.call(value, prior, :single, self,  &@match_value)
  node.next = target if target
  self.head = node if self.head.equal?(target)
  self.tail = node if self.tail.nil?
  @current = node
  self.size += 1
end

#nth(index) ⇒ Object

+int position from current node



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

def nth(index)
  node = @current
  while index > 1 and node and node.next
    node = node.next
    index -= 1
    @current = node
  end
  # no reverse or prev for Single List
  current
end

#remove(value) ⇒ Object

return remaining size



90
91
92
93
94
95
96
97
# File 'lib/skn_utils/lists/linked_list.rb', line 90

def remove(value)
  prior, target_node = find_by_value(value)
  @current = prior.nil? ? target_node.next : prior
  @current.next = target_node.remove! if @current && target_node
  self.tail = @current.next if @current && self.tail.equal?(target_node)
  self.head = @current.next if @current && self.head.equal?(target_node)
  self.size -= 1
end