Class: Listadll

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

Overview

Clase Lista doblemente enlazada

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeListadll

Constructor



12
13
14
15
# File 'lib/gema/listadll.rb', line 12

def initialize
      @head = nil
  @tail = nil
end

Instance Attribute Details

#headObject (readonly)

La cabecera de la Listadll



8
9
10
# File 'lib/gema/listadll.rb', line 8

def head
  @head
end

#tailObject (readonly)

La cola de la Listadll



10
11
12
# File 'lib/gema/listadll.rb', line 10

def tail
  @tail
end

Instance Method Details

#eachObject

Método necesario para que el módulo Enumerable se pueda utilizar(el módulo Enumerable define iteradores útiles que se implementan en base a un iterador each)



115
116
117
118
119
120
121
# File 'lib/gema/listadll.rb', line 115

def each
  node=@tail
  while node != nil
    yield node.value
    node=node.next
  end
end

#emptyObject

Método que te devuelve la cabecera de la Listadll vacía



123
124
125
126
127
# File 'lib/gema/listadll.rb', line 123

def empty 
          @head = nil
          return @head
   
end

#is_empty?Boolean

Método que comprueba si la Listadll está vacía

Returns:

  • (Boolean)


107
108
109
110
111
112
113
# File 'lib/gema/listadll.rb', line 107

def is_empty?
    if(@head==nil)
      return true
    else return false
    
  end
end

#popbackObject

Método que saca por detrás un valor de la Listadll



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gema/listadll.rb', line 83

def popback 
    if(@tail!=nil)  
      other = @tail
      @tail = @tail.next
    if(@tail!=nil)  
      @tail.prev=nil
      other.prev=nil 
    else @head = nil
      
    end
    end
    return other.value
end

#popfrontObject

Método que saca por delante un valor de la Listadll



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

def popfront
    if(@head!=nil)  
      other = @head
      @head = @head.prev
    if(@head!=nil)  
      @head.next=nil
      other.prev=nil 
    else @tail = nil
      
    end
    end
    return other.value
end

#pushback(valor) ⇒ Object

Método que inserta por detrás un valor o un array de valores en la Listadll



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gema/listadll.rb', line 46

def pushback (valor)
  if(@tail==nil)
    @tail=Nodedll.new(valor[0],nil,nil)
    @tail=@head
  end
  if(valor.instance_of? Array )
    for i in valor
      aux=@tail
          @tail=Nodedll.new(i,aux,@head)
      aux.prev=@tail

    end
  else
  if(@tail==nil)
    @tail=Nodedll.new(valor,nil,nil)
    @tail=@head
  end
    @tail=Nodedll.new(valor,nil,@head)
  end
  @tail.value

end

#pushfront(valor) ⇒ Object

Método que inserta por delante un valor o un array de valores en la Listadll



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gema/listadll.rb', line 17

def pushfront (valor)
  if(valor.instance_of? Array )
  if(@tail==nil)
    @tail=Nodedll.new(valor[0],nil,nil)
    @head = @tail
    valor.shift()
  end
    for i in valor 
      aux=@head
          @head=Nodedll.new(i,aux,nil)
      aux.next =@head

    end
  else
  if(@tail==nil)
    @tail=Nodedll.new(valor,nil,nil)
    @head = @tail
  else
     aux = @head            
     @head = Nodo.new(valor,aux,nil)            
     aux.next = @head 

  end
  end
  @head.value
  
end

#to_sObject

Método que imprime la Listadll



97
98
99
100
101
102
103
104
105
# File 'lib/gema/listadll.rb', line 97

def to_s
        other = @tail
          total=""
        while(other!=nil)
            total+= "#{other.value}\t"
            other = other.next
        end
          total += "\n" 
end