Class: Project

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/Project.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(head) ⇒ Project

Creates a new simple/linked list.

Parameters:

  • head

    the first node to insert



8
9
10
11
12
# File 'lib/Project.rb', line 8

def initialize (head)
        
    @head = head
    @tail = head
end

Instance Attribute Details

#headObject

Returns the value of attribute head.



2
3
4
# File 'lib/Project.rb', line 2

def head
  @head
end

#tailObject

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.

Parameters:

  • node

    node to add.



17
18
19
20
21
# File 'lib/Project.rb', line 17

def add(node)
    @tail.next = node
    node.prev=@tail
    @tail = @tail.next
end

#eachObject

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_sString

prints the simple/linked list.

Returns:

  • (String)

    every single node to string.



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