Class: List

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/P08/list.rb,
lib/P07/list.rb

Overview

Representa una lista doblemente enlazada con una cabeza y una cola, pudiéndose insertar un array de alimentos o alimentos individuales

Author

Alberto González ([email protected])

Copyright

Cretive Commons

License

Distributes under the same terms as Ruby

Instance Method Summary collapse

Constructor Details

#initializeList

Se asigna la cabeza y cola a nil (inicialmente)



21
22
23
# File 'lib/P08/list.rb', line 21

def initialize
	@head = @tail = nil
end

Instance Method Details

#eachObject

Método que nos permite recorrer cada uno de los nodos de nuestra lista



99
100
101
102
103
104
105
# File 'lib/P08/list.rb', line 99

def each
        next_val = @tail
          while next_val != nil do
                yield next_val.value
                next_val = next_val.next
          end
end

#headObject

Devuelve la cabeza de la lista a menos que no tenga cabeza



58
59
60
# File 'lib/P08/list.rb', line 58

def head
	@head.value unless @head.nil?
end

#push(value) ⇒ Object

Método para insertar valores en la lista, se pueden insertar varios valores o un único valor.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/P08/list.rb', line 26

def push(value)
	##Si es un array
    if value.is_a?(Array)
      value.each do |val|
        prev = @tail unless @tail.nil?
        while prev != @head do
          prev = prev.next
       	end
   
        node = Node.new(val, nil, prev)

        @tail = node if @tail.nil?
        @head.next = node unless @head.nil?
   
       	@head = node
      end
    else  #Si es un único valor
      prev = @tail unless @tail.nil?
        while prev != @head do
          prev = prev.next
        end
  
        node = Node.new(value, nil, prev)

        @tail = node if @tail.nil?
        @head.next = node unless @head.nil?
   
        @head = node
           end		
end

#remove_headObject

Borra la cabeza de la lista



70
71
72
73
# File 'lib/P08/list.rb', line 70

def remove_head
	@head = @head.prev
	@head.next = nil
end

#remove_tailObject

Borra la cola de la lista a menos que no tenga cola



66
67
68
# File 'lib/P08/list.rb', line 66

def remove_tail
 	@tail = @tail.next
end

#sizeObject

Devuelve la cantidad de nodos que tiene nuestra lista



89
90
91
92
93
94
95
96
97
# File 'lib/P08/list.rb', line 89

def size
	count = 0 
	next_val = @tail unless @tail.nil?
	 while next_val != nil do
		next_val = next_val.next
		count +=1
	 end
	"#{count}"
end

#tailObject

Devuelve la cola de la lista a menos que no tenga cola



62
63
64
# File 'lib/P08/list.rb', line 62

def tail
	@tail.value unless @tail.nil?
end

#to_sObject

Muestra los valores que se han almacenado en la lista



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/P08/list.rb', line 75

def to_s
           array = "["
    next_val = @tail unless @tail.nil?
      while next_val != nil do
        array = array + next_val.value.to_s + ", "
        next_val = next_val.next
       
        if next_val == nil #For las element
          array = array.chomp(', ') + "]"
        end
      end
     	 "#{array}"
end