Class: List

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

Overview

Clase List

Representa las funcionalidades de una Lista Doblemente Enlazada

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeList

Se inicializa la lista con un head y un tail



14
15
16
17
# File 'lib/nutritag/list.rb', line 14

def initialize
	   @head=nil
 	   @tail=nil
end

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



9
10
11
# File 'lib/nutritag/list.rb', line 9

def head
  @head
end

#tailObject (readonly)

Returns the value of attribute tail.



9
10
11
# File 'lib/nutritag/list.rb', line 9

def tail
  @tail
end

Instance Method Details

#eachObject

Método mixin del módulo Enumerable para poder enumerar



71
72
73
74
75
76
77
# File 'lib/nutritag/list.rb', line 71

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

#emptyObject

El método empty indica que la lista está vacía



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

def empty
   @head==nil
   @tail==nil
end

#extractObject

Se extraen elementos de la lista



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/nutritag/list.rb', line 45

def extract       
 if (@head==nil&&@tail==nil)
	 return nil
 else
	 aux = @head
       	 @head.next = @head
       	 @head.prev = nil unless @head.nil?
	 aux.next=nil
	 aux
 end

end

#insert(value) ⇒ Type

Se introducen elementos en la lista

Parameters:

  • value (Type)

    se le pasa un elemento a insertar

Returns:

  • (Type)

    devuele una lista



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/nutritag/list.rb', line 29

def insert(value)

node = Node.new(value, nil, nil)

        if (@head==nil&&@tail==nil)
          @head=node
          @tail=node
        else
          @tail.next=node
          #node.prev=@tail
          @tail=node
        end

end

#sort_imc(lista) ⇒ Type

Clasificación de pacientes según su IM

Parameters:

  • lista (Type)

    se introduce una lista

Returns:

  • (Type)

    devuelve otra lista



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/nutritag/list.rb', line 83

def sort_imc(lista)
	obesidad = List.new()
	normalidad = List.new()

	node = lista.head

	while !(node.nil?)
		if node.value.imc >= 30
			obesidad.insert(node.value)
		else
			normalidad.insert(node.value)
		end
		node = node.next
	end
	
	puts "Pacientes con Obesidad"
	obesidad.to_s
	puts "Pacientes con Normalidad"
	normalidad.to_s
end

#to_sString

Se imprime la lista por pantalla

Returns:

  • (String)

    devuele la lista formateada



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

def to_s
 	   n=@head
	   puts "{"
 	   while (n!=nil)
		"#{n.value.to_s}"
   	n=n.next
 	   end
     " }"
end