Sha256: 93d7ec0d797bc3df5eb208cec2303027066031eaebb94148bb5b35902459ff6d

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require_relative 'version'
#Estructura del nodo: valor, siguiente y anterior
Node = Struct.new(:value, :next, :prev)

#Clase lista
class LinkedList

  include Enumerable

  #Variables
  attr_reader :head, :tail
  attr_writer :head, :tail

  #Constructor
  def initialize
    @head = @tail = nil
  end

  #AƱadir nodos a la lista
  def add (value, *more)

    node = Node.new(value)

    @tail = node if @tail.nil?

    @head.next = node unless @head.nil?
    node.prev = @head unless @head.nil?

    @head = node

    more.each do |i|
      node2 = Node.new(i)
      @head.next = node2
      node2.prev = @head
      @head = @head.next
    end
  end

  #Saca la cabeza de la lista
  def pop()
    node = @head
    @head = @head.next
    return node
  end

  def empty()
    emptyy = false
    emptyy = true if @head.nil?

    return emptyy
  end

  #Mostrar la lista
  def to_s
    $i = @tail

    while ($i != nil) do
      puts "#{$i.value}"
      $i = $i.next
    end

    puts "CABEZA: #{@head.value}"
    puts "COLA: #{@tail.value}"
  end

  def each
    aux = @tail

    while aux != nil do
      yield aux.value
      aux = aux.next
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
prct_sergio-0.3.0 lib/prct06/list.rb