Class: SknUtils::Lists::DoublyLinkedList

Inherits:
LinkedCommons show all
Defined in:
lib/skn_utils/lists/doubly_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

return new size



72
73
74
75
76
77
78
# File 'lib/skn_utils/lists/doubly_linked_list.rb', line 72

def insert(value)
  node = @current
  @current = LinkNode.call(value, node, :after, 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



92
93
94
95
96
97
98
99
# File 'lib/skn_utils/lists/doubly_linked_list.rb', line 92

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

#insert_before(position_value, value) ⇒ Object

return new size



82
83
84
85
86
87
88
89
# File 'lib/skn_utils/lists/doubly_linked_list.rb', line 82

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

#nth(index) ⇒ Object

-+ int position from current node



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/skn_utils/lists/doubly_linked_list.rb', line 49

def nth(index)
  node = @current
  if index > 0
    while index > 1 and node and node.next
      node = node.next
      index -= 1
    end
    @current = node
  elsif index < 0
    while index < 0 and node and node.prev
      node = node.prev
      index += 1
    end
    @current = node
  end
  current
end

#prevObject

Navigation



43
44
45
46
# File 'lib/skn_utils/lists/doubly_linked_list.rb', line 43

def prev
  @current = @current.prev if @current and @current.prev
  @current.value rescue nil
end

#remove(value) ⇒ Object

return remaining size



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/skn_utils/lists/doubly_linked_list.rb', line 102

def remove(value)
  target_node = find_by_value(value)
  if target_node
    if self.size == 1                           # will become zero
      @current = nil
      self.head = nil
      self.tail = nil
    elsif target_node.prev.nil?            # top
      @current = target_node.next
      @current.prev = nil
      self.head = @current
    elsif target_node.next.nil?            # bottom
      @current = target_node.prev
      @current.next = nil
      self.tail = @current
    else                                   # middle
      @current = target_node.prev
      @current.next = target_node.next
      target_node.next.prev = @current
    end
    target_node.remove!
    self.size -= 1
  end
end