Sha256: f13e26e07f716446afad65ed2c64923e51b28ce5dac7a08b8401d97d060f88d7

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

Nodo = Struct.new(:value, :next, :prev)

class Lista

include Enumerable

    attr_accessor :head, :cola, :nodo_actual

    def initialize(head,cola)
        @head = head
        @cola = cola
    end

    def insert(x)
        node = Nodo.new(x,nil,nil)

        if @head==nil && @cola==nil
                node.prev = @cola
                node.next = @head
                @nodo_actual = node
                @head = node
                @cola = node
        else
                node.prev = @head
                node.next = nil
                @nodo_actual = node
                @head.next = node
                @head = node
        end
    end

    def insert_multiple(nodos)
        nodos.each do |nd|
                insert(nd)
        end
    end

   #eliminar el nodo y cambiar los valores al nodo anterior 
    def extract()
       @cola=@cola.next
    end

    def extract_final()
       @head = @head.prev
    end

    def to_s
         @head.to_s
    end

    def each
        while @head != @cola
            yield @head.value
            @head=@head.prev
        end

        if @head == @cola
           if @head == nil
            nil
           else
            yield @cola.value
           end
        end
    end
end
 

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dieta_alu0100648782-0.1.0 lib/dieta/lista.rb