Implementation

Class: LinkedList.

Constants

ZERO private

A 0 Integer literal.

ONE private

A 1 Integer literal.

Attributes

size

The list's element quantity, type Integer.

base

The list's base NodeAdapter. base is either nil or a NodeAdapter instance.

Methods

initialize(d_or_n = nil)

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

Initializes a LinkedList instance. Takes a DataType type, Node, or NodeAdapter instance. Sets the instance's base the argument NodeAdapter, or, instantiates a NodeAdapter using the argument data or Node instance, and sets base the instantiation. Returns the LinkedList.

shallow_clone()

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

Shallowly clones self. Attribute references are the same. Returns the LinkedList clone.

clone_df()

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

Deeply clones. Returns the LinkedList clone. No NodeAdapter references are identical. The data references are identical.

size()

def size()
return @size
end

Gets size. Returns the Integer.

exists(n = nil)

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

Predicate. Verifies a list element exists. Takes an argument, n. Returns true in the case n is a list NodeAdapter. false otherwise.

empty()

def empty()
return size().zero?()
end

Predicate. Returns true in the case size is 0. false otherwise.

==(object = nil)

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

Attribute equality operator. Takes any object, object. Returns true in the case the argument is a LinkedList and its attribute references are identical. false otherwise.

inspect()

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

Represents the list diagrammatically. Returns a String containing the list nodes' inspections concatenated. The base node includes a 'base' label.

remove(n = nil)

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

Removes the list's NodeAdapter , n. Traverses the list, and in the case n exists in the list, removes the reference. In the case n is not a list element, raises an ArgumentError. Returns nil.

insert(n1 = nil, n2 = nil)

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

Inserts a NodeAdapter after a list element. Takes a Node or NodeAdapter n1 and a NodeAdapter n2. n1 is an insertion Node or NodeAdapter. n2 is a NodeAdapter list element. Following the operation n1 is n2 's front reference, and n2 is n1's back reference. In the case n2 is not a list element, the list remains unchanged. Returns nil.

base() protected

def base()
return @base
end

Gets base's reference. Returns the NodeAdapter or NilClass instance.

size=(i = nil) protected

def size=(i = nil)
@size = i
end

Sets size. Takes an Integer list size, i. Assigns size i. Returns the argument Integer.

initialize_node(dti = nil) private

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

In the case the public initialize receives a DataType type or Node instance, the argument becomes a NodeAdapter. The conversion then becomes base's setting. Returns the NodeAdapter instance.

base=(n = nil) private

def base=(n = nil)
@base = node
end

Sets base. Takes a NodeAdapter argument, n. n becomes base's setting. Returns the NodeAdapter argument.

increment_s() private

def increment_s()
self.size = (size() + 1)
end

Increment operator. Increments size. Sets size the list's size plus one. Returns the list's size Integer.

decrement_s() private

def decrement_s()
self.size = (size() - 1)
end

Decrement operator. Decrements size. Sets size the list size less one. Returns the list's Integer size.

attach(n1 = nil, n2 = nil) private

def attach(n1 = nil, n2 = nil)
n1.attach_front(n2)
n2.attach_back(n1)
return nil
end

Attaches two NodeAdapters. Takes two NodeAdapter arguments, n1 and n2. n1 is a precession NodeAdapter. n1 precedes n2. n2 is a consequent NodeAdapter. n2 and n1 become doubly-linked. Returns nil.

detach(n1 = nil, n2 = nil) private

def detach(n1 = nil, n2 = nil)
n1.detach_front()
n2.detach_back()
return nil
end

Detaches two linked NodeAdapters. Takes two NodeAdapter arguments, n1 and n2. n1 is a precession NodeAdapter. n1 precedes n2. n2 is a consequent NodeAdapter. n2 follows n1. Following the operation n1's front reference is nil, and n2's back reference is nil. All other references remain unchanged. Returns nil.