Class: List

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

Overview

Class List Defines the list

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeList

Initialization of the list



36
37
38
39
40
# File 'lib/nutrientes/list.rb', line 36

def initialize()
    @size = 0
    @head = nil
    @tail = nil
end

Instance Attribute Details

#headObject (readonly)

Returns The first element of the list.

Returns:

  • (Object)

    The first element of the list



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/nutrientes/list.rb', line 31

class List
    attr_reader :head, :tail, :size
    include Enumerable
    
    #   Initialization of the list
    def initialize()
        @size = 0
        @head = nil
        @tail = nil
    end
    
    # Defines the each method to make use of the Enumerable module
    def each
        actual = @head
        while actual != nil do
            yield actual
            actual = actual.nodo[:next_]
        end
    end
    
    # Inserts a node at the beginning of the list
    # @param [Object] nodo node to insert
    # @return nil
    def push(nodo) 
        if @size == 0 then
            @head = nodo
            @tail = nodo
        else
            @tail.nodo[:next_] = nodo
            nodo.nodo[:prev_] = @tail
            @tail = nodo
        end
        @size += 1
    end
    
    # Inserts a node at the end of the list
    # @param nodo [Object] node to insert
    # @return nil
    def insert(nodo)
        if @size == 0 then
            @head = nodo
            @tail = nodo
        else
            @head.nodo[:prev_] = nodo
            nodo.nodo[:next_] = @head
            @head = nodo
        end
        @size += 1
    end
    
    # Deletes a node from the beginning of the list
    # @return nil
    def truncate
        if size > 1
            @head = @head.nodo[:next_]
            @head.nodo[:prev_] = nil
            @size -= 1
        elsif size == 1
            @head = nil
            @tail = nil
            @size -= 1
        end
    end
    
    # Deletes a node from the end of the list
    # @return nil
    def pop
        if size > 1
            @tail = @tail.nodo[:prev_]
            @tail.nodo[:next_] = nil
            @size -= 1
        elsif size == 1
            @tail = nil
            @head = nil
            @size -= 1
        end
    end
end

#sizeFixnum (readonly)

Returns Size of the list.

Returns:

  • (Fixnum)

    Size of the list



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/nutrientes/list.rb', line 31

class List
    attr_reader :head, :tail, :size
    include Enumerable
    
    #   Initialization of the list
    def initialize()
        @size = 0
        @head = nil
        @tail = nil
    end
    
    # Defines the each method to make use of the Enumerable module
    def each
        actual = @head
        while actual != nil do
            yield actual
            actual = actual.nodo[:next_]
        end
    end
    
    # Inserts a node at the beginning of the list
    # @param [Object] nodo node to insert
    # @return nil
    def push(nodo) 
        if @size == 0 then
            @head = nodo
            @tail = nodo
        else
            @tail.nodo[:next_] = nodo
            nodo.nodo[:prev_] = @tail
            @tail = nodo
        end
        @size += 1
    end
    
    # Inserts a node at the end of the list
    # @param nodo [Object] node to insert
    # @return nil
    def insert(nodo)
        if @size == 0 then
            @head = nodo
            @tail = nodo
        else
            @head.nodo[:prev_] = nodo
            nodo.nodo[:next_] = @head
            @head = nodo
        end
        @size += 1
    end
    
    # Deletes a node from the beginning of the list
    # @return nil
    def truncate
        if size > 1
            @head = @head.nodo[:next_]
            @head.nodo[:prev_] = nil
            @size -= 1
        elsif size == 1
            @head = nil
            @tail = nil
            @size -= 1
        end
    end
    
    # Deletes a node from the end of the list
    # @return nil
    def pop
        if size > 1
            @tail = @tail.nodo[:prev_]
            @tail.nodo[:next_] = nil
            @size -= 1
        elsif size == 1
            @tail = nil
            @head = nil
            @size -= 1
        end
    end
end

#tailObject (readonly)

Returns The last element of the list.

Returns:

  • (Object)

    The last element of the list



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/nutrientes/list.rb', line 31

class List
    attr_reader :head, :tail, :size
    include Enumerable
    
    #   Initialization of the list
    def initialize()
        @size = 0
        @head = nil
        @tail = nil
    end
    
    # Defines the each method to make use of the Enumerable module
    def each
        actual = @head
        while actual != nil do
            yield actual
            actual = actual.nodo[:next_]
        end
    end
    
    # Inserts a node at the beginning of the list
    # @param [Object] nodo node to insert
    # @return nil
    def push(nodo) 
        if @size == 0 then
            @head = nodo
            @tail = nodo
        else
            @tail.nodo[:next_] = nodo
            nodo.nodo[:prev_] = @tail
            @tail = nodo
        end
        @size += 1
    end
    
    # Inserts a node at the end of the list
    # @param nodo [Object] node to insert
    # @return nil
    def insert(nodo)
        if @size == 0 then
            @head = nodo
            @tail = nodo
        else
            @head.nodo[:prev_] = nodo
            nodo.nodo[:next_] = @head
            @head = nodo
        end
        @size += 1
    end
    
    # Deletes a node from the beginning of the list
    # @return nil
    def truncate
        if size > 1
            @head = @head.nodo[:next_]
            @head.nodo[:prev_] = nil
            @size -= 1
        elsif size == 1
            @head = nil
            @tail = nil
            @size -= 1
        end
    end
    
    # Deletes a node from the end of the list
    # @return nil
    def pop
        if size > 1
            @tail = @tail.nodo[:prev_]
            @tail.nodo[:next_] = nil
            @size -= 1
        elsif size == 1
            @tail = nil
            @head = nil
            @size -= 1
        end
    end
end

Instance Method Details

#eachObject

Defines the each method to make use of the Enumerable module



43
44
45
46
47
48
49
# File 'lib/nutrientes/list.rb', line 43

def each
    actual = @head
    while actual != nil do
        yield actual
        actual = actual.nodo[:next_]
    end
end

#insert(nodo) ⇒ Object

Inserts a node at the end of the list

Parameters:

  • nodo (Object)

    node to insert

Returns:

  • nil



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/nutrientes/list.rb', line 69

def insert(nodo)
    if @size == 0 then
        @head = nodo
        @tail = nodo
    else
        @head.nodo[:prev_] = nodo
        nodo.nodo[:next_] = @head
        @head = nodo
    end
    @size += 1
end

#popObject

Deletes a node from the end of the list

Returns:

  • nil



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/nutrientes/list.rb', line 97

def pop
    if size > 1
        @tail = @tail.nodo[:prev_]
        @tail.nodo[:next_] = nil
        @size -= 1
    elsif size == 1
        @tail = nil
        @head = nil
        @size -= 1
    end
end

#push(nodo) ⇒ Object

Inserts a node at the beginning of the list

Parameters:

  • nodo (Object)

    node to insert

Returns:

  • nil



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/nutrientes/list.rb', line 54

def push(nodo) 
    if @size == 0 then
        @head = nodo
        @tail = nodo
    else
        @tail.nodo[:next_] = nodo
        nodo.nodo[:prev_] = @tail
        @tail = nodo
    end
    @size += 1
end

#truncateObject

Deletes a node from the beginning of the list

Returns:

  • nil



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/nutrientes/list.rb', line 83

def truncate
    if size > 1
        @head = @head.nodo[:next_]
        @head.nodo[:prev_] = nil
        @size -= 1
    elsif size == 1
        @head = nil
        @tail = nil
        @size -= 1
    end
end