class List

Clase para las listas doblemente enlazadas

Attributes

head[RW]
tail[RW]

Public Class Methods

new(o) click to toggle source

Inicializacion de una instancia de la clase Lista a partir de una Question pregunta).

# File lib/Exam/list.rb, line 16
def initialize (o)
        raise TypeError, "Se esperaba que se pasara una pregunta como parámetro" unless o.instance_of? (Question)
        @head = Node.new(o, nil,nil)
        @tail = @head
end

Public Instance Methods

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

Metodo para le manejo de metodos del modulo Enumerable

# File lib/Exam/list.rb, line 69
def each               
        aux =@head    
        while (aux != nil) do
                yield aux.value 
                aux = aux.next
        end
end
inverter(&block) click to toggle source

Metodo que devuelve la lista actual invertida.

# File lib/Exam/list.rb, line 80
def inverter (&block)
        block = ->(x) {true} if !block_given?
        list = invert(@head, &block)
        list.pop
        return nil  if list.count == 0
        list
end
pop() click to toggle source

Metodo para extraer “por la cabeza” de la lista acutalmente.

# File lib/Exam/list.rb, line 24
def pop
        raise IndexError, "La lista se encuentra vacía" unless @head.is_a? (Node)             
        aux = @head

        if (@head == @tail)
                @head = @tail = nil
                aux.next = aux.prev = nil
        else   
                @head = @head.next
                @head.prev = nil             
                aux.next = aux.prev = nil
        end 
        aux.value             
end
push(*input) click to toggle source

Metodo para instertar varias preguntas sucesivamente

# File lib/Exam/list.rb, line 41
def push (*input)
        input.each do |x| 
                aux = Node.new(x, nil, nil)
                @tail.next = aux
                aux.prev = @tail     
                @tail = @tail.next
                @tail.value
        end
        input
end
to_s() click to toggle source

Metodo #to_s

# File lib/Exam/list.rb, line 54
def to_s
        aux = @head
        s = ' '
        i = 1
        while (aux != nil) do
                s += "#{i}) #{aux.value}\n"
                aux = aux.next
                i += 1
        end
        s
end

Private Instance Methods

invert(actual, &block) click to toggle source

Metodo auxiliar que se utiliza para invertir la lista (recursividad).

# File lib/Exam/list.rb, line 90
def invert(actual, &block)
        return List.new(Question.new(:qt => "dummy", :r1 => "dummy", :wrong => ["dummy"])) if (actual == nil)
        list = invert(actual.next, &block)
        list.push(actual.value) if block[actual.value]
        list
end