Class: List

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/Exam/list.rb

Overview

Clase para las listas doblemente enlazadas

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(o) ⇒ List

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

Raises:

  • (TypeError)


17
18
19
20
21
# File 'lib/Exam/list.rb', line 17

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

Instance Attribute Details

#headObject

Returns the value of attribute head.



12
13
14
# File 'lib/Exam/list.rb', line 12

def head
  @head
end

#tailObject

Returns the value of attribute tail.



12
13
14
# File 'lib/Exam/list.rb', line 12

def tail
  @tail
end

Instance Method Details

#eachObject

Metodo para le manejo de metodos del modulo Enumerable



74
75
76
77
78
79
80
# File 'lib/Exam/list.rb', line 74

def each    
  aux =@head 
  while (aux != nil) do
    yield aux.value 
    aux = aux.next
  end
end

#inverter(&block) ⇒ Object

Metodo que devuelve la lista actual invertida.



85
86
87
88
89
90
91
# File 'lib/Exam/list.rb', line 85

def inverter (&block)
  block = ->(x) {true} if !block_given?
  list = invert(@head, &block)
  list.pop
  return nil  if list.count == 0
  list
end

#popObject

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

Raises:

  • (IndexError)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/Exam/list.rb', line 25

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) ⇒ Object

Metodo para instertar varias preguntas sucesivamente



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/Exam/list.rb', line 42

def push (*input)
  input.each do |x| 
    if @head == nil
      @head = @tail = Node.new(x, nil, nil)  
    else 
      aux = Node.new(x, nil, nil)
      @tail.next = aux
      aux.prev = @tail 
      @tail = @tail.next
      @tail.value
    end
  end
  input
end

#to_sObject

Metodo to_s



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/Exam/list.rb', line 59

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