Class: DLinkedList::List

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

Overview

Lista de referencias bibliográficas.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeList

Inicializa la lista vacía.



229
230
231
232
233
# File 'lib/dLinkedList/dLinkedList.rb', line 229

def initialize()
  @head = nil
  @tail = nil
  @size = 0
end

Instance Attribute Details

#headObject (readonly)

Nodo cabeza de la lista.



222
223
224
# File 'lib/dLinkedList/dLinkedList.rb', line 222

def head
  @head
end

#sizeObject (readonly)

Tamaño de la lista.



226
227
228
# File 'lib/dLinkedList/dLinkedList.rb', line 226

def size
  @size
end

#tailObject (readonly)

Nodo cola de la lista.



224
225
226
# File 'lib/dLinkedList/dLinkedList.rb', line 224

def tail
  @tail
end

Instance Method Details

#eachObject

Itera la lista, ejecutando el bloque pasado pasándole como parámetro el valor de cada nodo.



293
294
295
296
297
298
299
300
301
# File 'lib/dLinkedList/dLinkedList.rb', line 293

def each
  
  nodo = @head
  until nodo.nil?
    yield nodo.value
    nodo = nodo.next_node
  end
  
end

#empty?Boolean

Devuelve si la lista está vacía.



236
237
238
# File 'lib/dLinkedList/dLinkedList.rb', line 236

def empty?()
  return (@head == nil)
end

#popObject

Extrae y devuelve la referencia del principio de la lista.

Raises:

  • (RuntimeError)


273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/dLinkedList/dLinkedList.rb', line 273

def pop()
  
  raise RuntimeError, "[List.pop]: No se puede extraer elementos de una lista vacía" if empty?()
  
  nodo = @head
  if @head.equal?(@tail)
    @head = nil
    @tail = nil
  else
    @head = @head.next_node
    @head.prev_node = nil
  end
  
  @size -= 1
  
  return nodo.value
  
end

#push(ref) ⇒ Object

Inserta la referencia ref al final de la lista.



255
256
257
258
259
260
261
262
263
264
265
# File 'lib/dLinkedList/dLinkedList.rb', line 255

def push(ref)
  if empty?()
    return push_empty(ref)
  else
    nodo = Node.new(ref, nil, @tail)
    @tail.next_node = nodo
    @tail = nodo
    @size += 1
    return self
  end
end

#push_multi(*refs) ⇒ Object

Inserta múltiples elementos al final de la lista.



268
269
270
# File 'lib/dLinkedList/dLinkedList.rb', line 268

def push_multi(*refs)
  refs.each { |ref| push(ref)}
end

#to_sObject

Devuelve la lista de referencias ordenadas en líneas distintas como cadena.



304
305
306
307
# File 'lib/dLinkedList/dLinkedList.rb', line 304

def to_s
  s_arr = self.sort()
  return s_arr.join("\n")
end