Class: List

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(head, tail) ⇒ List

Creamos una lista e inicializamos los valores



11
12
13
14
# File 'lib/prct06/lista.rb', line 11

def initialize (head, tail)
	@head = head
	@tail = tail
end

Instance Attribute Details

#headObject (readonly)

Punteros head y tail



8
9
10
# File 'lib/prct06/lista.rb', line 8

def head
  @head
end

#tailObject (readonly)

Punteros head y tail



8
9
10
# File 'lib/prct06/lista.rb', line 8

def tail
  @tail
end

Instance Method Details

#each(&block) ⇒ Object

Metodo enumerable



111
112
113
114
115
116
117
# File 'lib/prct06/lista.rb', line 111

def each(&block)
	puntero=@head
	while (puntero!=nil) do
		yield puntero.value
		puntero=puntero.nest
	end
end

#extraer_por_cabezaObject

Metodo extraer nodo por cabeza



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/prct06/lista.rb', line 47

def extraer_por_cabeza
		if(@head==nil)
			puts "No hay nada que extraer (lista vacia)"
		else
			aux=@head
			@head=@head.nest
			if(head!=nil)
				@head.prev=nil
			end
			aux.nest=nil
			if(@head==nil)
				@tail=nil
			end
		end			

		return aux

end

#extraer_por_colaObject

Metodo extraer nodo por cola



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/prct06/lista.rb', line 67

def extraer_por_cola
	if(@tail==nil)
		puts "No hay nada que extraer (lista vacia)"
	else
		aux=@tail
		@tail=@tail.prev
		aux.prev=nil
		if(@tail!=nil)
			@tail.nest=nil
		end
	end
		
	return aux
end

#insertar_por_cabeza(value) ⇒ Object

Metodo insertar nodo por cabeza



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/prct06/lista.rb', line 32

def insertar_por_cabeza(value)
	nodo=Node.new(value,nil,nil)
	if(@head==nil)
		@tail=nodo
		@head=nodo
	else
		nodo.nest=@head
		@head.prev=nodo
		@head=nodo
		nodo.prev=nil
	end

end

#insertar_por_cola(value) ⇒ Object

Metodo insertar nodo por cola



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/prct06/lista.rb', line 17

def insertar_por_cola(value)
	nodo=Node.new(value,nil,nil)
               if(@tail==nil)
                       @tail=nodo
                       @head=nodo
               else
                       nodo.prev=@tail
                       @tail.nest=nodo
                       @tail=nodo
		nodo.nest=nil
               end

end

#ordenacion_eachObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/prct06/lista.rb', line 138

def ordenacion_each
        @resultado = self.map { |x| x }
        indice=0
        @resultado.each do |x|
                var = x
                i = indice
                indice2=indice + 1

                @resultado[indice2..@resultado.length-1].each do |y|
                        if var > y
                                var = y
                                i = indice2
                        end
                        indice2+=1
                end

                @resultado[i] = x
                @resultado[indice] = var

                indice+=1
        end
        @resultado
end

#ordenacion_forObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/prct06/lista.rb', line 119

def ordenacion_for
        @resultado = self.map { |x| x }
        for x in 0..self.count-1
                var = @resultado[x]
                i = x

                for y in x + 1..self.count-1
                        if var > @resultado[y]
                                var = @resultado[y]
                                i = y
                        end
                end

                @resultado[i] = @resultado[x]
                @resultado[x] = var
        end
        @resultado
end

#to_sObject

Metodo convertir a cadena



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/prct06/lista.rb', line 92

def to_s

	puntero=@head
	cadena='['
		if(@head!=nil)
			while (puntero!= nil) do
				cadena+=puntero.value.to_s + ']'
				if(puntero.nest!=nil)
					puntero=puntero.nest
					cadena+= '['
				else
					puntero=nil
				end
			end
		end

end

#vacioObject

Metodo comprobar si esta vacío



83
84
85
86
87
88
89
# File 'lib/prct06/lista.rb', line 83

def vacio
		if(@tail==nil)
			return true
		else
			return false
		end
end