class List

Attributes

head[RW]
tail[RW]

Public Class Methods

new(first_value=nil) click to toggle source

Inicializa la lista con un primer valor si existe

# File lib/my_gem/list.rb, line 8
def initialize(first_value=nil)
        @head = Node.new(first_value,nil,nil) if first_value
        @tail = @head
end

Public Instance Methods

each() { |value| ... } click to toggle source

@return hace la lista enumerable

# File lib/my_gem/list.rb, line 80
def each
        aux = @tail
        while aux != nil
                yield aux.value
                aux = aux.next
        end
end
pophead() click to toggle source

@return Saca el nodo que está en la cabeza

# File lib/my_gem/list.rb, line 38
def pophead
        if @head
                if @head[:previous]==nil
                        salida = @head[:value]
                        @tail=@head=nil
                        return salida;
                else
                        salida = @head[:value]
                        @head=@head[:previous]
                        return salida;
                end
        else
                return "No elements left"
        end
end
poptail() click to toggle source

@return Saca el nodo que está en la cola

# File lib/my_gem/list.rb, line 54
def poptail
        if @tail
                if @tail[:next]==nil
                        salida = @tail[:value]
                        @head=@tail=nil
                        return salida;
                else
                        salida = @tail[:value]
                        @tail=@tail[:next]
                        return salida;
                end
        else
                return "No elements left"
        end
end
pushhead(value) click to toggle source

@return Añade un nodo a la cabeza de la lista

# File lib/my_gem/list.rb, line 14
def pushhead(value)
        if @head
                new_node = Node.new(value,nil,nil)
                @head[:next] = new_node
                new_node[:previous] = @head
                @head = new_node
        else
                @head = Node.new(value,nil,nil)
                @tail = @head
        end
end
pushtail(value) click to toggle source

@return Añade un nodo a la cola de la lista

# File lib/my_gem/list.rb, line 26
def pushtail(value)
        if @tail
                new_node = Node.new(value,nil,nil)
                @tail[:previous] = new_node
                new_node[:next] = @tail
                @tail = new_node
        else
                @head = Node.new(value,nil,nil)
                @tail = @head
        end
end
size() click to toggle source

@return Devuelve el tamaño de la lista

# File lib/my_gem/list.rb, line 70
def size
        aux1=@head.previous
        aux2=1
        while(aux1!=nil)
                aux1=aux1.previous
                aux2+=1
        end
        return aux2
end