Class: List

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

Overview

Clase que implementa una lista

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeList

Returns a new instance of List.



5
6
7
8
9
# File 'lib/pract07/List.rb', line 5

def initialize
  @top = nil #cabeza de la lista
  @end = nil #cola de la lista
  @tam = nil #tamaño de la lista
end

Instance Attribute Details

#endObject

Returns the value of attribute end.



3
4
5
# File 'lib/pract07/List.rb', line 3

def end
  @end
end

#tamObject

Returns the value of attribute tam.



3
4
5
# File 'lib/pract07/List.rb', line 3

def tam
  @tam
end

#topObject

Returns the value of attribute top.



3
4
5
# File 'lib/pract07/List.rb', line 3

def top
  @top
end

Instance Method Details

#<=>(other) ⇒ Object



56
57
58
# File 'lib/pract07/List.rb', line 56

def <=>(other)
    tam <=> other.tam
end

#eachObject

Función para ir sacando el valor de los nodos y compararlos con las funciones Enumerable



45
46
47
48
49
50
51
52
53
54
# File 'lib/pract07/List.rb', line 45

def each
  if @top.nil?
    return nil
  end
  entry = @top
  until entry.nil?
    yield entry.value
    entry = entry.next
  end
end

#popObject

sacar el nodo que se encuentre en la cabeza de la lista



35
36
37
38
39
40
41
# File 'lib/pract07/List.rb', line 35

def pop #sacar el nodo que se encuentre en la cabeza de la lista
return nil if @top.nil?
entry = @top
@top = @top.next
@tam -= 1
return entry
end

#pushend(entry) ⇒ Object

introducir nodo por la cola de la lista



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pract07/List.rb', line 23

def pushend(entry) #introducir nodo por la cola de la lista
if @top.nil?
  @top = entry
  @end = entry
  @tam = 1
else
  @end.next = entry
  @end = entry
  @tam += 1
end
end

#pushtop(entry) ⇒ Object

introducir nodo por la cabeza de la lista



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pract07/List.rb', line 11

def pushtop(entry) #introducir nodo por la cabeza de la lista
if @top.nil?
  @top = entry
  @end = entry
  @tam = 1
else
  entry.next = @top
  @top = entry
  @tam += 1
end
end