Class: List

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeList

inicializamos la lista doblemente enlazada con un head y un tail



6
7
8
9
10
# File 'lib/practica6/practica7.rb', line 6

def initialize()
	@head=Node.new()
	@tail=@head
	
end

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



4
5
6
# File 'lib/practica6/practica7.rb', line 4

def head
  @head
end

#tailObject (readonly)

Returns the value of attribute tail.



4
5
6
# File 'lib/practica6/practica7.rb', line 4

def tail
  @tail
end

Instance Method Details

#add(other) ⇒ Object

metodo que inserta un elemento al principio de la lista



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/practica6/practica7.rb', line 26

def add(other)
	nuevo=Node.new(other,nil,nil)
	if(@head==nil)
		@head=nuevo
		@head.next=nil
		@head.prev=nil
		@tail=@head
	
	else
		nuevo.next=@head
		@head.prev=nuevo
		@head=nuevo 
	
	end
end

#add_end(other) ⇒ Object

metodo que inserta un elemento al final de la lista



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

def add_end(other)
	nuevo=Node.new(other,nil,nil)
	if(@head==@tail)
		if(@head==nil)
			@tail=nuevo
			@head=@tail
		else
			@tail.next=nuevo
			nuevo.prev=@tail
			@tail=nuevo
			@head.next=@tail
		end
	else
		@tail.next=nuevo
		nuevo.prev=@tail
		@tail=nuevo
	end
end

#add_end_mult(other) ⇒ Object

metodo que inserta varios elementos al final de la lista



85
86
87
88
89
# File 'lib/practica6/practica7.rb', line 85

def add_end_mult(other)
	for i in 0..other.length-1
		add_end(other[i])
	end
end

#add_mult(other) ⇒ Object

metodo que añade varios elementos al principio de la lista



42
43
44
45
46
# File 'lib/practica6/practica7.rb', line 42

def add_mult(other)
	for i in 0..other.length-1
		add(other[i])		
	end
end

#eachObject

metodo each que recorre cada nodo del vector para poder usar funciones de Enumerable



12
13
14
15
16
17
18
# File 'lib/practica6/practica7.rb', line 12

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

#get_firstObject

metodo que extrae el primer elemento de la lista



20
21
22
23
24
# File 'lib/practica6/practica7.rb', line 20

def get_first
        other=@head
			@head=@head.next
        other.value
end

#get_lastObject

metodo que extrae el ultimo elemento de la lista



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/practica6/practica7.rb', line 67

def get_last
	result=""
	if(@head==@tail)
		if(@head==nil)
			result=nil
		else
			result=@tail.value
			@tail=nil
			@head=nil
		end
	else
		result=@tail.value
		@tail=@tail.prev
		@tail.next=nil
	end
	result
end