class List
Class List
Defines the list @!attribute [r] head
@return [Object] The first element of the list
@!attribute [r] tail
@return [Object] The last element of the list
@!attribute [r] size
@return [Fixnum] Size of the list
Attributes
head[R]
size[R]
tail[R]
Public Class Methods
new()
click to toggle source
Initialization of the list
# File lib/nutrientes/list.rb, line 36 def initialize() @size = 0 @head = nil @tail = nil end
Public Instance Methods
each() { |actual| ... }
click to toggle source
Defines the each method to make use of the Enumerable module
# 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)
click to toggle source
Inserts a node at the end of the list @param nodo [Object] node to insert @return nil
# 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()
click to toggle source
Deletes a node from the end of the list @return nil
# 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)
click to toggle source
Inserts a node at the beginning of the list @param [Object] nodo node to insert @return nil
# 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()
click to toggle source
Deletes a node from the beginning of the list @return nil
# 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