Class: LinkedList

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

Overview

lista doblemente enlazada

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, *more) ⇒ LinkedList

initialize que crea una lista con tantos nodos como argumentos le pasemos



16
17
18
19
20
21
22
23
# File 'lib/prct06/LinkedList.rb', line 16

def initialize (value, *more)
	@head = Node.new(value)
	aux = @head
	more.each do |i|
		aux.next = Node.new(i, nil, aux)
		aux = aux.next
	end
end

Instance Attribute Details

#headObject (readonly)

attr



13
14
15
# File 'lib/prct06/LinkedList.rb', line 13

def head
  @head
end

Instance Method Details

#access(pos) ⇒ Object

método que accede a los valores de cada nodo según el iterador pos



37
38
39
40
41
42
43
44
# File 'lib/prct06/LinkedList.rb', line 37

def access (pos)
	p = pos-1
	aux = @head
	for i in 0..p-1 do
		aux = aux.next
	end
	return aux[:value]
end

#access_node(pos) ⇒ Object

método que accede a los nodos de la lista según el iterador pos



47
48
49
50
51
52
53
54
# File 'lib/prct06/LinkedList.rb', line 47

def access_node (pos)
	p = pos-1
	aux = @head
	for i in 0..p-1 do
		aux = aux.next
	end
	return aux
end

#eachObject

invalidación de each para el correcto funcionamiento del módulo enumerable



118
119
120
121
122
123
124
# File 'lib/prct06/LinkedList.rb', line 118

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

#lengthObject

método que devuelve el número de elementos en la lista



26
27
28
29
30
31
32
33
34
# File 'lib/prct06/LinkedList.rb', line 26

def length
	aux = @head
	cnt = 0
	while aux != nil do
		aux = aux.next
		cnt = cnt + 1
	end
	return cnt
end

#pop(pos) ⇒ Object

saca un elemento de la lista, eliminando el nodo, según el iterador p



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/prct06/LinkedList.rb', line 58

def pop (pos)
	if pos == 1 then
		aux = @head
		@head = @head.next
		aux.prev = nil
		aux.next = nil
		return aux[:value]
	end

	p = pos-2
	aux = @head
	for i in 0..p-1 do
		prev = aux
		aux = aux.next
	end
	popping = aux.next
	aux.next = popping.next
	(popping.next).prev = aux unless popping.next == nil
	return popping[:value]
end

#push(value, pos = 0) ⇒ Object

introduce un elemento en un nuevo nodo de la lista si no se especifica la posición deseada, hace push al final de la lista



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

def push (value, pos = 0)
	if pos == 0 then
		aux = @head
		for i in 0..length-2 do
			aux = aux.next
		end
		aux.next = Node.new(value, nil, aux)
	elsif pos == 1 then
		aux = @head
		@head = Node.new(value)
		@head.next = aux unless aux == nil
	else
		p = pos-2
		aux = @head
		for i in 0..p-1 do
			aux = aux.next
		end

		save = aux.next
		aux.next = Node.new(value, save, aux)
	end
end

#push_multiple(*values) ⇒ Object

crea tantos nodos nuevos al final de la lista como elementos se pase como argumentos



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/prct06/LinkedList.rb', line 105

def push_multiple (*values)
	aux = @head
	for i in 0..length-2 do
		aux = aux.next
	end

	values.each do |i|
		aux.next = Node.new(i, nil, aux)
		aux = aux.next
	end
end

#to_sObject

formateo a string de la lista completa



129
130
131
132
133
134
135
136
137
# File 'lib/prct06/LinkedList.rb', line 129

def to_s
	output = "{  "
	aux = @head
	while aux != nil do
		output << "#{aux[:value]}  "
		aux = aux[:next]
	end
	output << "}"
end