Class: Lista::Lista_doble

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLista_doble

Returns a new instance of Lista_doble.



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

def initialize
    @head = @tail = nil
    @num_nodos = 0
end

Instance Attribute Details

#headObject

Returns the value of attribute head.



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

def head
  @head
end

#num_nodosObject

Returns the value of attribute num_nodos.



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

def num_nodos
  @num_nodos
end

Instance Method Details

#at(indice) ⇒ Object

Método de acceso a un elemento dependiendo del índice



100
101
102
103
104
105
106
107
108
# File 'lib/lista_doble.rb', line 100

def at(indice)
    cont = 0
    aux = @head
    while cont < indice do
        aux = aux.next
        cont += 1
    end
    return aux
end

#eachObject

Para :Enumerable



116
117
118
119
120
121
122
# File 'lib/lista_doble.rb', line 116

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

#extract_firstObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lista_doble.rb', line 70

def extract_first
   
  if @head == nil
      puts "\nLista vacía, nada que extraer\n"
  else
      aux = @head
      @head = aux.next
      return aux
  end
  @num_nodos -= 1
end

#get_cabezaObject



23
24
25
# File 'lib/lista_doble.rb', line 23

def get_cabeza
    @head
end

#get_colaObject



27
28
29
# File 'lib/lista_doble.rb', line 27

def get_cola
    @tail
end

#get_num_nodosObject



19
20
21
# File 'lib/lista_doble.rb', line 19

def get_num_nodos
    @num_nodos
end

#insert_beginning(*val) ⇒ Object

Esperamos val como un numero variable de nodos



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/lista_doble.rb', line 52

def insert_beginning(*val)
    
    val.each do |nuevo_nodo|
        
        if @head != nil
            
            @head.previous = nuevo_nodo
            nuevo_nodo.next = @head
            @head = nuevo_nodo
        else
            @head = nuevo_nodo
        end
        @num_nodos += 1
        
    end
end

#insert_final(*val) ⇒ Object

# :no_required

# Esperamos val como un numero variable de nodos


36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/lista_doble.rb', line 36

def insert_final(*val)
     
     val.each do |nuevo_nodo|
    
    if @tail != nil
        @tail.next = nuevo_nodo
        nuevo_nodo.previous = @tail
        @tail = nuevo_nodo
    else
        @head = @tail = nuevo_nodo
    end
    @num_nodos += 1
    end
end

#resetObject



110
111
112
113
# File 'lib/lista_doble.rb', line 110

def reset
   @head = @tail = nil
   @num_nodos = 0
end

#to_sObject



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/lista_doble.rb', line 125

def to_s
    aux = @head
    if aux == nil
        puts "Lista vacía"
    else
        while aux.next != nil
            puts aux.value
            puts 
            aux = aux.next
        end
    puts aux.value
    end
end