Libraries
Search…
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)

1
def initialize(d_or_n = nil)
2
3
arg_class = d_or_n.class()
4
case
5
when arg_class.equal?(Node)
6
n_a = NodeAdapter.new(d_or_n)
7
self.base = n_a
8
increment_s()
9
when arg_class.equal?(NodeAdapter)
10
self.base = d_or_n
11
increment_s()
12
when DataType.type?(arg_class)
13
self.base = initialize_node(d_or_n)
14
increment_s()
15
else
16
raise(ArgumentError,
17
"#{d_or_n} is neither a DataType or Node family type instance.")
18
end
19
20
end
Copied!
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()

1
def shallow_clone()
2
3
n_a = NodeAdapter.new(base())
4
n_a.size = size()
5
if (frozen?())
6
n_a.freeze()
7
end
8
return n_a
9
10
end
Copied!
Shallowly clones self. Attribute references are the same. Returns the LinkedList clone.

clone_df()

1
def clone_df()
2
3
clone = shallow_clone()
4
c_iter = LinkedListIterator.new(clone.base())
5
iter = LinkedListIterator.new(base())
6
list_element = iter.element()
7
8
if (list_element.equal?(base()))
9
10
while (!list_element.pioneer())
11
12
cdf_node = list_element.clone_df()
13
clone.insert(list_element, cdf_node)
14
c_iter.next()
15
16
end
17
cdf_node = list_element.clone_df()
18
clone.insert(cdf_node, c_iter.element())
19
20
end
21
if (frozen?())
22
clone.freeze()
23
end
24
return clone
25
26
end
Copied!
Deeply clones. Returns the LinkedList clone. No NodeAdapter references are identical. The data references are identical.

size()

1
def size()
2
return @size
3
end
Copied!
Gets size. Returns the Integer.

exists(n = nil)

1
def exists(n = nil)
2
3
unless (n.instance_of?(NodeAdapter))
4
return false
5
else
6
7
iter = LinkedListIterator.new(base())
8
iter_element = iter.element()
9
while ((!iter_element.pioneer()) && (!iter_element.lone()))
10
11
if (iter_element.equal?(n))
12
return true
13
end
14
iter.next()
15
16
end
17
if (iter_element.equal?(n))
18
return true
19
else
20
return false
21
end
22
23
end
24
25
end
Copied!
Predicate. Verifies a list element exists. Takes an argument, n. Returns true in the case n is a list NodeAdapter. false otherwise.

empty()

1
def empty()
2
return size().zero?()
3
end
Copied!
Predicate. Returns true in the case size is 0. false otherwise.

==(object = nil)

1
def ==(object = nil)
2
3
unless (object.instance_of?(NodeAdapter))
4
return false
5
else
6
return ((back().equal?(object.back()) &&
7
(data().equal?(object.data())) && (front().equal?(object.front()))))
8
end
9
10
end
Copied!
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()

1
def inspect()
2
3
diagram = ""
4
unless (empty?())
5
diagram += inspect_upper()
6
diagram += inspect_lower()
7
else
8
diagram = '| nil |'
9
end
10
return diagram
11
12
end
Copied!
Represents the list diagrammatically. Returns a String containing the list nodes' inspections concatenated. The base node includes a 'base' label.

remove(n = nil)

1
def remove(n = nil)
2
3
case
4
when (!n.instance_of?(NodeAdapter))
5
raise(ArgumentError, "#{n} is not a NodeAdapter instance.")
6
when (!exists(n))
7
raise(ArgumentError, "#{n} is not a list element.")
8
end
9
pre_n = n.back()
10
post_n = n.front()
11
attach(pre_n, post_n)
12
n.detach_back()
13
n.detach_front()
14
decrement_s()
15
return nil
16
17
end
Copied!
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)

1
def insert(n1 = nil, n2 = nil)
2
3
case
4
when (!n1.kind_of?(Node))
5
raise(ArgumentError, "#{n1} is not a Node family instance.")
6
when (!n2.instance_of?(NodeAdapter))
7
raise(ArgumentError, "#{n2} is not a NodeAdapter instance.")
8
when (!exists(n2))
9
raise(ArgumentError, "#{n2} is not a list element.")
10
when (n1.instance_of?(Node))
11
n1 = NodeAdapter.new(n1)
12
end
13
14
na_kind = n2.kind()
15
k1 = 'lone'.freeze()
16
k2 = 'pioneer'.freeze()
17
k3 = 'base'.freeze()
18
k4 = 'common'.freeze()
19
case na_kind
20
when k1, k2
21
attach(n2, n1)
22
when k3, k4
23
24
conseq_n = n2.front()
25
attach(n2, conseq_n)
26
attach(n1, n2)
27
28
end
29
increment_s()
30
return nil
31
32
end
Copied!
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

1
def base()
2
return @base
3
end
Copied!
Gets base's reference. Returns the NodeAdapter or NilClass instance.

size=(i = nil) protected

1
def size=(i = nil)
2
@size = i
3
end
Copied!
Sets size. Takes an Integer list size, i. Assigns size i. Returns the argument Integer.

initialize_node(dti = nil) private

1
def initialize_node(dti = nil)
2
3
b_node = Node.new(nil, dti, nil)
4
n_a = NodeAdapter.new(b_node)
5
return n_a
6
7
end
Copied!
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

1
def base=(n = nil)
2
@base = node
3
end
Copied!
Sets base. Takes a NodeAdapter argument, n. n becomes base's setting. Returns the NodeAdapter argument.

increment_s() private

1
def increment_s()
2
self.size = (size() + 1)
3
end
Copied!
Increment operator. Increments size. Sets size the list's size plus one. Returns the list's size Integer.

decrement_s() private

1
def decrement_s()
2
self.size = (size() - 1)
3
end
Copied!
Decrement operator. Decrements size. Sets size the list size less one. Returns the list's Integer size.

attach(n1 = nil, n2 = nil) private

1
def attach(n1 = nil, n2 = nil)
2
3
n1.attach_front(n2)
4
n2.attach_back(n1)
5
return nil
6
7
end
Copied!
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

1
def detach(n1 = nil, n2 = nil)
2
3
n1.detach_front()
4
n2.detach_back()
5
return nil
6
7
end
Copied!
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.
Last modified 1yr ago