Class: Lista

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/prct06/lista.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject

Returns Lista type of the class.



18
19
20
21
22
# File 'lib/prct06/lista.rb', line 18

def initialize
  @head = nil
  @queue = nil
  @tamanio = 0
end

Instance Attribute Details

#headObject

Includes the module Enumerable It have tres values, head of the list, queue of the list and size of the list



13
14
15
# File 'lib/prct06/lista.rb', line 13

def head
  @head
end

#queueObject

Includes the module Enumerable It have tres values, head of the list, queue of the list and size of the list



13
14
15
# File 'lib/prct06/lista.rb', line 13

def queue
  @queue
end

#tamanioObject

Includes the module Enumerable It have tres values, head of the list, queue of the list and size of the list



13
14
15
# File 'lib/prct06/lista.rb', line 13

def tamanio
  @tamanio
end

Instance Method Details

#convertirArray(lista) ⇒ Object

return array param lista



135
136
137
# File 'lib/prct06/lista.rb', line 135

def convertirArray(lista)
  lista.map{ |y| y}
end

#eachObject

return node



202
203
204
205
206
207
208
# File 'lib/prct06/lista.rb', line 202

def each
    aux = @head
    while aux != nil
        yield aux.valor
        aux = aux.siguiente
    end 
end

#emptyObject

return true or false



193
194
195
196
197
198
199
# File 'lib/prct06/lista.rb', line 193

def empty
  if tamanio == 0
    true
  else
    false
  end
end

#insert_head(nodo) ⇒ Object

Returns head of the node.

Parameters:

Returns:

  • head of the node



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/prct06/lista.rb', line 26

def insert_head(nodo)
  if empty
    nodo[:siguiente] = nil
    nodo[:anterior] = nil
    @head = nodo
    @queue = nodo
  else
    nodo[:siguiente] = @head
    @head[:anterior] = nodo
    nodo[:anterior] = nil
    @head = nodo
  end
  
  @tamanio = @tamanio + 1
  
end

#insert_plus(nodos, index) ⇒ Object

Returns nodo.

Parameters:

  • nodo,

    index [Nodo], [Number]

Returns:

  • nodo



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/prct06/lista.rb', line 65

def insert_plus(nodos, index)
  #si el indice es 1, insertara por la cabeza
  if(index == 1)
    for i in 0..(nodos.length-1)
	insert_head(nodos[i])
    end
  end
    #Si el indice es 2, insertara por la cola
  else if(index == 2)
    for i in 0..(nodos.length-1)
	insert_queue(nodos[i])
    end
  end
end

#insert_queue(nodo) ⇒ Object

Returns queue of the node.

Parameters:

Returns:

  • queue of the node



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/prct06/lista.rb', line 46

def insert_queue(nodo)
  if empty
    nodo[:siguiente] = nil
    nodo[:anterior] = nil
    @head = nodo
    @queue = nodo
  else
    nodo[:siguiente] = nil
    nodo[:anterior] = @queue
    @queue[:siguiente] = nodo
    @queue = nodo
  end
  
  @tamanio = @tamanio + 1
end

#ordenandoEach(lista) ⇒ Object

return array param lista



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/prct06/lista.rb', line 163

def ordenandoEach(lista)
  auxiliar = lista.convertirArray(lista)
  indice = 0
  auxiliar.each do |x|
	auxiliar.each do |y|
	  if (indice < auxiliar.length-1)
 if (auxiliar[indice] > auxiliar[indice+1])
   temporal = auxiliar[indice]
   auxiliar[indice] = auxiliar[indice+1]
   auxiliar[indice+1] = temporal
 end
	  end
	  
	  indice = indice+1
	end
	
	indice = 0
  end
        
  return auxiliar
end

#ordenandoSort(lista) ⇒ Object



185
186
187
188
# File 'lib/prct06/lista.rb', line 185

def ordenandoSort(lista)
  auxiliar = lista.convertirArray(lista)
  auxiliar.sort
end

#ordenarArray(lista) ⇒ Object

return array param lista



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/prct06/lista.rb', line 143

def ordenarArray(lista)
  auxiliar = lista.convertirArray(lista)
  for i in 0..(auxiliar.length)do
	for j in 0..(auxiliar.length-2) do
	  if auxiliar[j] > auxiliar[j+1]
   temporal = auxiliar[j]
   auxiliar[j] = auxiliar[j+1]
   auxiliar[j+1] = temporal
 end
	end
  end
  
  return auxiliar
end

#pop_headObject

Returns head of the list.

Returns:

  • head of the list



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/prct06/lista.rb', line 82

def pop_head
  if !empty
    if @head == @queue
	aux = @head
	@head = nil
	@queue = nil
    else
	aux = @head
	@head = @head.siguiente
	@head[:anterior] = nil
    end
    
    @tamanio = @tamanio -1
    aux
  end
end

#pop_queueObject

Returns queue of the list.

Returns:

  • queue of the list



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/prct06/lista.rb', line 102

def pop_queue
  if !empty
    if @queue == @head
	aux = @queue
	@head = nil
	@queue = nil
    else
	aux = @queue
	@queue =aux.anterior
	@queue[:siguiente] = nil
    end
   
    @tamanio = @tamanio - 1
    aux
  end
end

#to_sString

Returns the resulting of join all the information, value, next and previous.

Returns:

  • (String)

    the resulting of join all the information, value, next and previous



120
121
122
123
124
125
126
127
128
129
# File 'lib/prct06/lista.rb', line 120

def to_s
    aux = @head
    s = ""
    while aux.siguiente != nil
        s += aux.valor.to_s + "\n"
        aux =  aux.siguiente
    end
    s += aux.valor.to_s
    
end