Class: Lista
Instance Attribute Summary collapse
-
#n_elementos ⇒ Object
readonly
Returns the value of attribute n_elementos.
-
#nodo_final ⇒ Object
readonly
Returns the value of attribute nodo_final.
-
#nodo_inicial ⇒ Object
readonly
Returns the value of attribute nodo_inicial.
Instance Method Summary collapse
- #delete(pos) ⇒ Object
- #each {|value| ... } ⇒ Object
-
#initialize ⇒ Lista
constructor
A new instance of Lista.
- #insert(pos, valor) ⇒ Object
- #ordenar ⇒ Object
- #pop_final ⇒ Object
- #pop_inicio ⇒ Object
- #push_final(valor) ⇒ Object
- #push_inicio(valor) ⇒ Object
Constructor Details
Instance Attribute Details
#n_elementos ⇒ Object (readonly)
Returns the value of attribute n_elementos.
8 9 10 |
# File 'lib/bibliogem/Lista.rb', line 8 def n_elementos @n_elementos end |
#nodo_final ⇒ Object (readonly)
Returns the value of attribute nodo_final.
8 9 10 |
# File 'lib/bibliogem/Lista.rb', line 8 def nodo_final @nodo_final end |
#nodo_inicial ⇒ Object (readonly)
Returns the value of attribute nodo_inicial.
8 9 10 |
# File 'lib/bibliogem/Lista.rb', line 8 def nodo_inicial @nodo_inicial end |
Instance Method Details
#delete(pos) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/bibliogem/Lista.rb', line 68 def delete(pos) if(pos < @n_elementos) aux = @nodo_inicial for i in 0..(pos-1) aux=aux[:siguiente] end delete = aux[:siguiente] aux2=delete[:siguiente] aux2[:anterior]=aux aux[:siguiente]=aux2 @n_elementos-=1 return delete else puts "No hay tantos elementos." return nil end end |
#each {|value| ... } ⇒ Object
14 15 16 17 18 |
# File 'lib/bibliogem/Lista.rb', line 14 def each value = @nodo_inicial yield value value = value[:siguiente] end |
#insert(pos, valor) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/bibliogem/Lista.rb', line 53 def insert(pos, valor) if(pos < @n_elementos) aux = @nodo_inicial for i in 0..pos aux=aux[:siguiente] end insert = Nodo.new(valor, aux[:siguiente], aux) aux2=aux[:siguiente] aux2[:anterior]=insert aux[:siguiente]=insert @n_elementos+=1 else puts "No hay tantos elementos." end end |
#ordenar ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/bibliogem/Lista.rb', line 125 def ordenar if n_elementos <= 1 return end fin=false while fin == false fin=true x1=@nodo_inicial x2=x1[:siguiente] while x2 != nil if(x1[:valor] > x2[:valor]) x1[:valor], x2[:valor] = x2[:valor], x1[:valor] fin=false end x1=x2 x2=x2[:siguiente] end end end |
#pop_final ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/bibliogem/Lista.rb', line 105 def pop_final if(@n_elementos == 0) puts "La lista esta vacia" return nil elsif(@n_elementos == 1) ret = Nodo.new(@nodo_inicial[:valor], nil, nil) @nodo_incial[:valor]=nil @nodo_final = @nodo_inicial @n_elementos-=1 return ret elsif(@n_elementos > 1) ret = Nodo.new(@nodo_final[:valor], nil, @nodo_final[:anterior]) aux=@nodo_final @nodo_final=@nodo_final[:anterior] @nodo_final[:siguiente]=nil aux[:anterior]=nil @n_elementos-=1 return ret end end |
#pop_inicio ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/bibliogem/Lista.rb', line 85 def pop_inicio if(@n_elementos == 0) puts "La lista esta vacia" return nil elsif(@n_elementos == 1) ret = Nodo.new(@nodo_inicial[:valor], nil, nil) @nodo_inicial[:valor]=nil @nodo_final = @nodo_inicial @n_elementos-=1 return ret elsif(@n_elementos > 1) ret = Nodo.new(@nodo_inicial[:valor], @nodo_inicial[:siguiente], nil) aux=@nodo_inicial @nodo_inicial=@nodo_inicial[:siguiente] @nodo_inicial[:anterior]=nil aux[:siguiente]=nil @n_elementos-=1 return ret end end |
#push_final(valor) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/bibliogem/Lista.rb', line 36 def push_final(valor) if(@n_elementos == 0) @nodo_inicial[:valor]=valor @nodo_final = @nodo_inicial @n_elementos+=1 elsif(@n_elementos == 1) aux = Nodo.new(valor, nil, @nodo_inicial) @nodo_inicial[:siguiente]=aux @nodo_final = aux @n_elementos+=1 elsif(@n_elementos >= 1) aux = Nodo.new(valor,nil,@nodo_final) @nodo_final[:siguiente]=aux @nodo_final=aux @n_elementos+=1 end end |
#push_inicio(valor) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/bibliogem/Lista.rb', line 19 def push_inicio(valor) if @n_elementos == 0 @nodo_inicial[:valor]=valor @nodo_final = @nodo_inicial @n_elementos+=1 elsif @n_elementos == 1 aux = Nodo.new(valor, @nodo_final, nil) @nodo_inicial = aux @nodo_final[:anterior]=@nodo_inicial @n_elementos+=1 elsif @n_elementos >= 1 aux = Nodo.new(valor, @nodo_final, nil) @nodo_inicial[:anterior]=aux @nodo_inicial=aux @n_elementos+=1 end end |