Class: Project
Instance Attribute Summary collapse
-
#head ⇒ Object
Returns the value of attribute head.
-
#tail ⇒ Object
Returns the value of attribute tail.
Instance Method Summary collapse
-
#add(node) ⇒ Object
Adds a node to a simple/linked list.
-
#each ⇒ Object
Definition of each for Enumerable operations.
-
#initialize(head) ⇒ Project
constructor
Creates a new simple/linked list.
-
#to_s ⇒ String
prints the simple/linked list.
Constructor Details
#initialize(head) ⇒ Project
Creates a new simple/linked list.
8 9 10 11 12 |
# File 'lib/Project.rb', line 8 def initialize (head) @head = head @tail = head end |
Instance Attribute Details
#head ⇒ Object
Returns the value of attribute head.
2 3 4 |
# File 'lib/Project.rb', line 2 def head @head end |
#tail ⇒ Object
Returns the value of attribute tail.
2 3 4 |
# File 'lib/Project.rb', line 2 def tail @tail end |
Instance Method Details
#add(node) ⇒ Object
Adds a node to a simple/linked list.
17 18 19 20 21 |
# File 'lib/Project.rb', line 17 def add(node) @tail.next = node node.prev=@tail @tail = @tail.next end |
#each ⇒ Object
Definition of each for Enumerable operations.
41 42 43 44 45 46 47 48 49 |
# File 'lib/Project.rb', line 41 def each current_node = @head while current_node != nil yield current_node.value current_node = current_node.next end end |
#to_s ⇒ String
prints the simple/linked list.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/Project.rb', line 26 def to_s() out = String.new current_node = @head while current_node != nil out << "#{current_node.value.to_s}" current_node = current_node.next out << "\n" end return out end |