class LinkedList

LinkedList. @description

A doubly-linked LinkedList data structure library's implementation.
Implements the LinkedList interface.

@attr base [NodeAdapter]

A base.

@attr size [Integer]

The list's element quantity.

LinkedList. @description

A doubly-linked LinkedList data structure library's implementation.
Implements the LinkedList interface.

@attr base [NodeAdapter]

A base.

@attr size [Integer]

The list's element quantity.

Constants

ONE
VERSION
ZERO

Private constants.

Public Class Methods

new(d_or_n = nil) click to toggle source

initialize(d_or_n = nil). @description

Initializes a LinkedList instance.

@param d_or_n [Node, DataType, NodeAdapter]

Any instance.

@return [LinkedList]

A LinkedList instance.

@raise [ArgumentError]

In the case the argument is neither a DataType type or Node instance.
# File lib/linked_list_impl.rb, line 30
def initialize(d_or_n = nil)

  arg_class = d_or_n.class()
  case
  when arg_class.equal?(Node)
    n_a = NodeAdapter.new(d_or_n)
    self.base = n_a
    increment_s()
  when arg_class.equal?(NodeAdapter)
    self.base = d_or_n
    increment_s()
  when DataType.type?(arg_class)
    self.base = initialize_node(d_or_n)
    increment_s()
  else
    raise(ArgumentError,
          "#{d_or_n} is neither a DataType or Node family type instance.")
  end

end

Public Instance Methods

==(object = nil) click to toggle source

(object = nil).

@description

Equality operator.

@param object [.]

Any object. A comparison.

@return [TrueClass, FalseClass]

True in the case the lhs and rhs' attribute references are identical.
False otherwise.
# File lib/linked_list_impl.rb, line 159
def ==(object = nil)

  unless (object.instance_of?(NodeAdapter))
    return false
  else
    return ((back().equal?(object.back()) && (data().equal?(object.data())) &&
        (front().equal?(object.front()))))
  end

end
clone_df() click to toggle source

clone_df(). @description

Deeply clones.

@return [LinkedList]

A deep clone. No Node references are identical. The data references are
identical.
# File lib/linked_list_impl.rb, line 73
def clone_df()

  clone = shallow_clone()
  c_iter = LinkedListIterator.new(clone.base())
  iter = LinkedListIterator.new(base())
  list_element = iter.element()

  if (list_element.equal?(base()))

    while (!list_element.pioneer())

      cdf_node = list_element.clone_df()
      clone.insert(list_element, cdf_node)
      c_iter.next()

    end
    cdf_node = list_element.clone_df()
    clone.insert(cdf_node, c_iter.element())

  end
  if (frozen?())
    clone.freeze()
  end
  return clone

end
empty() click to toggle source

empty(). @description

Predicate. Verifies size is 0

@return [TrueClass, FalseClass]

True in the case the size is zero. False otherwise.
# File lib/linked_list_impl.rb, line 147
def empty()
  return size().zero?()
end
exists(n = nil) click to toggle source

exists(n = nil). @description

Predicate. Verifies an object is a list element.

@param n [.]

Any object.

@return [TrueClass, FalseClass]

True in the case 'n' is a list element. False otherwise.
# File lib/linked_list_impl.rb, line 116
def exists(n = nil)

  unless (n.instance_of?(NodeAdapter))
    return false
  else

    iter = LinkedListIterator.new(base())
    iter_element = iter.element()
    while ((!iter_element.pioneer()) && (!iter_element.lone()))

      if (iter_element.equal?(n))
        return true
      end
      iter.next()

    end
    if (iter_element.equal?(n))
      return true
    else
      return false
    end

  end

end
insert(n1 = nil, n2 = nil) click to toggle source

insert(n1 = nil, n2 = nil). @description

Inserts a Node after a specific Node.

@param n1 [Node, NodeAdapter]

A consequent. The insertion.

@param n2 [NodeAdapter]

A precession. An existing list Node.

@return [NilClass]

nil.
# File lib/linked_list_impl.rb, line 225
def insert(n1 = nil, n2 = nil)

  case
  when (!n1.kind_of?(Node))
    raise(ArgumentError, "#{n1} is not a Node family instance.")
  when (!n2.instance_of?(NodeAdapter))
    raise(ArgumentError, "#{n2} is not a NodeAdapter instance.")
  when (!exists(n2))
    raise(ArgumentError, "#{n2} is not a list element.")
  when (n1.instance_of?(Node))
    n1 = NodeAdapter.new(n1)
  end

  na_kind        = n2.kind()
  k1             = 'lone'.freeze()
  k2             = 'pioneer'.freeze()
  k3             = 'base'.freeze()
  k4 = 'common'.freeze()
  case na_kind
  when k1, k2
    attach(n2, n1)
  when k3, k4

    conseq_n = n2.front()
    attach(n2, conseq_n)
    attach(n1, n2)

  end
  increment_s()
  return nil

end
inspect() click to toggle source

inspect(). @description

Represents the String diagrammatically.

@return [String]

The list nodes' inspections concatenated. The base node inspection
appends the first pipe the label "base".
# File lib/linked_list_impl.rb, line 176
def inspect()

  diagram = ""
  unless (empty?())
    diagram += inspect_upper()
    diagram += inspect_lower()
  else
    diagram = '| nil |'
  end
  return diagram

end
remove(n = nil) click to toggle source

remove(n = nil). @description

Removes the list's NodeAdapter 'n'.

@param n [NodeAdapter]

A list Node. The removal.

@return [NilClass]

nil.

@raise [ArgumentError]

In the case the argument was not found in the list.
# File lib/linked_list_impl.rb, line 198
def remove(n = nil)

  case
  when (!n.instance_of?(NodeAdapter))
    raise(ArgumentError, "#{n} is not a NodeAdapter instance.")
  when (!exists(n))
    raise(ArgumentError, "#{n} is not a list element.")
  end
  pre_n = n.back()
  post_n = n.front()
  attach(pre_n, post_n)
  n.detach_back()
  n.detach_front()
  decrement_s()
  return nil

end
shallow_clone() click to toggle source

shallow_clone(). @description

Shallowly clones self.

@return [LinkedList]

Returns the LinkedList clone.
# File lib/linked_list_impl.rb, line 56
def shallow_clone()

  n_a = NodeAdapter.new(base())
  n_a.size = size()
  if (frozen?())
    n_a.freeze()
  end
  return n_a

end
size() click to toggle source

size(). @description

Gets the list's size.

@return [Integer]

The element quantity.
# File lib/linked_list_impl.rb, line 105
def size()
  return @size
end

Protected Instance Methods

base() click to toggle source

base(). @description

Gets base's reference.

@return [NodeAdapter]

'base'.
# File lib/linked_list_impl.rb, line 265
def base()
  return @base
end
size=(i = nil) click to toggle source

size=(i = nil). @description

Sets 'size'.

@param i [Integer]

An integer list size.

@return [Integer]

The argument.
# File lib/linked_list_impl.rb, line 276
def size=(i = nil)
  @size = i
end

Private Instance Methods

attach(n1 = nil, n2 = nil) click to toggle source

attach(n1 = nil, n2 = nil). @description

Attaches two NodeAdapters.

@param n1 [NodeAdapter]

A precession.

@param n2 [NodeAdapter]

A consequent.

@return [NilClass]

nil.
# File lib/linked_list_impl.rb, line 333
def attach(n1 = nil, n2 = nil)

  n1.attach_front(n2)
  n2.attach_back(n1)
  return nil

end
base=(node = nil) click to toggle source

base=(n = nil). @description

Sets 'base'.

@param n [NodeAdapter]

The instance becoming 'base'.
# File lib/linked_list_impl.rb, line 302
def base=(node = nil)
  @base = node
end
decrement_s() click to toggle source

decrement_s(). @description

Decrement operator. Decrements size.

@return [Integer]

The list's size less one.
# File lib/linked_list_impl.rb, line 320
def decrement_s()
  self.size = (size() - 1)
end
detach(n1 = nil, n2 = nil) click to toggle source

detach(n1 = nil, n2 = nil). @description

Detaches two linked NodeAdapters.

@param n1 [NodeAdapter]

A precession.

@param n2 [NodeAdapter]

A consequent.

@return [NilClass]

nil.
# File lib/linked_list_impl.rb, line 350
def detach(n1 = nil, n2 = nil)

  n1.detach_front()
  n2.detach_back()
  return nil

end
increment_s() click to toggle source

increment_s(). @description

Increment operator. Increments size.

@return [Integer]

The list's size plus one.
# File lib/linked_list_impl.rb, line 311
def increment_s()
  self.size = (size() + 1)
end
initialize_node(dti = nil) click to toggle source

initialize_node(dti = nil). @description

Initializes a NodeAdapter instance.

@param dti [DataType]

The NodeAdapter's data setting.

@return [NodeAdapter]

The NodeAdapter instance.
# File lib/linked_list_impl.rb, line 289
def initialize_node(dti = nil)

  b_node = Node.new(nil, dti, nil)
  n_a = NodeAdapter.new(b_node)
  return n_a

end