Class: Lista

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

Overview

Clase que implementa una lista doblemente enlazada.

Author:

  • alu0100895179

Defined Under Namespace

Classes: Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLista

Método que inicializa la lista vacía con first=nil y last=nil.



14
15
16
17
18
19
# File 'lib/lista.rb', line 14

def initialize
	
	@last=nil 
	@first=nil
	
end

Instance Attribute Details

#firstNode (readonly)

Primer nodo de la lista

Returns:

  • (Node)

    the current value of first



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

def first
  @first
end

#lastNode (readonly)

Último nodo de la lista

Returns:

  • (Node)

    the current value of last



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

def last
  @last
end

Instance Method Details

#eachObject

Método each necesario para que la lista sea enumerable, indicando como se debe recorrer la lista.



105
106
107
108
109
110
111
# File 'lib/lista.rb', line 105

def each
aux_node=@first
	while aux_node != nil
		yield aux_node[:value]
		aux_node=aux_node[:next]
	end
end

#extractvalue

Método para extraer el último nodo de la lista.

Returns:

  • (value)

    Retornamos el valor contenido en el último nodo.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lista.rb', line 41

def extract
	if @last == nil
		
		puts "La lista está vacía! Es necesario insertar algún elemento antes de extraer."

	elsif @last == @first
		aux=@first[:value]
		@first=nil
		@last=@first
		aux
	else
		to_extract=@last[:value]
		
		@last=@last[:prev]
		@last[:next]=nil
		to_extract
	end
end

#insert(new_val) ⇒ Object

Método para insertar un nuevo nodo en la lista.

Parameters:

  • new_val (Node)

    Recibimos un nuevo dato y creamos un nodo para insertarlo en la lista



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lista.rb', line 23

def insert(new_val)
	
	if @first == nil
		new_node=Node.new(new_val, nil, nil)
		@first=new_node
		@last=@first
	else
		
		prev=@last
		new_node=Node.new(new_val, prev, nil)
		@last[:next]=new_node
		@last=new_node
		
	end
end

#mostrar_inversostring

Método para mostrar por pantalla todos los elementos de la lista en orden inverso.

Returns:

  • (string)

    Devuelve una cadena con todo el texto de la lista



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/lista.rb', line 84

def mostrar_inverso
	
	aux_node=@last
	i=1
	string = ""
	if aux_node != nil
		while aux_node != nil
			
			string << "#{i}) "
			string << aux_node[:value].to_s
			string << "\n"
			i+=1
			aux_node=aux_node[:prev]
		end
	else
		string = "La lista está vacía así que es imposible mostrar!\n"
	end
	string
end

#to_sstring

Método para mostrar por pantalla todos los elementos de la lista en orden.

Returns:

  • (string)

    Devuelve una cadena con todo el texto de la lista



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

def to_s
	
	aux_node=@first
	i=1
	string = ""
	if aux_node != nil
		while aux_node != nil
			
			string << "#{i}) "
			string << aux_node[:value].to_s
			string << "\n"
			i+=1
			aux_node=aux_node[:next]
		end
	else
		string = "La lista está vacía así que es imposible mostrar!\n"
	end
	string
end