Class: List
Overview
Class List Defines the list
Instance Attribute Summary collapse
-
#head ⇒ Object
readonly
The first element of the list.
-
#size ⇒ Fixnum
readonly
Size of the list.
-
#tail ⇒ Object
readonly
The last element of the list.
Instance Method Summary collapse
-
#each ⇒ Object
Defines the each method to make use of the Enumerable module.
-
#initialize ⇒ List
constructor
Initialization of the list.
-
#insert(nodo) ⇒ Object
Inserts a node at the end of the list.
-
#pop ⇒ Object
Deletes a node from the end of the list.
-
#push(nodo) ⇒ Object
Inserts a node at the beginning of the list.
-
#truncate ⇒ Object
Deletes a node from the beginning of the list.
Constructor Details
#initialize ⇒ List
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
#head ⇒ Object (readonly)
Returns 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 |
#size ⇒ Fixnum (readonly)
Returns 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 |
#tail ⇒ Object (readonly)
Returns 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
#each ⇒ Object
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
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 |
#pop ⇒ Object
Deletes a node from the end of the list
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
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 |
#truncate ⇒ Object
Deletes a node from the beginning of the list
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 |