Class: Lista

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

Overview

Esta clase representa una lista doblemente enlazada

Author:

  • Miriam Rodríguez Méndez

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLista

Returns a new instance of Lista.



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

def initialize
	@head = nil
	@tail = nil
end

Instance Attribute Details

#headObject (readonly)

Esto permite la lectura de la cola y la cabeza de la lista



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

def head
  @head
end

#nodeObject

Returns the value of attribute node.



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

def node
  @node
end

#tailObject (readonly)

Esto permite la lectura de la cola y la cabeza de la lista



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

def tail
  @tail
end

Instance Method Details

#eachObject

Método para que la clase sea Enumerable



66
67
68
69
70
71
72
# File 'lib/prct06/lista.rb', line 66

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

#empty?bool

Comprueba si la lista está vacía

Returns:

  • (bool)

    false si no está vacía, true si está vacía



22
23
24
# File 'lib/prct06/lista.rb', line 22

def empty?
	return (head == nil) && (tail == nil)
end

#extract_headObject

Extrae un nodo por la cabeza de la lista

Returns:

  • Devuelve el primer nodo de la lista



42
43
44
45
46
47
48
49
50
51
# File 'lib/prct06/lista.rb', line 42

def extract_head
	aux = @head.value
	if(@head == @tail) 
	  @head,@tail = nil
	else 
	  @head = @head[:next]
	  @head[:prev] = nil
	end		
	return aux
end

#insert_tail(value) ⇒ Object

Inserta un nodo por la cola

Parameters:

  • value

    Cualquier objeto que se use como valor



29
30
31
32
33
34
35
36
37
# File 'lib/prct06/lista.rb', line 29

def insert_tail(value)
	if(empty?)
		@head = Node.new(value,nil,nil)
		@tail = @head
	else
		@tail[:next] = Node.new(value,nil,@tail)
		@tail = @tail[:next]
	end			
end

#szObject



74
75
76
77
78
79
80
81
82
# File 'lib/prct06/lista.rb', line 74

def sz
	cont = 0
	aux = @head
	while(aux != nil) do
		cont = cont + 1
		aux=aux.next
	end
	return cont
end

#to_array_eachObject



94
95
96
97
98
99
100
101
102
# File 'lib/prct06/lista.rb', line 94

def to_array_each
        arr = []
        aux = @head
        for i in self do
			arr.push(aux.value)
                aux = aux.next
        end
        arr.ordenar_each
end

#to_array_forObject



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

def to_array_for
	arr = []
	aux = @head
	for i in self do
		arr.push(aux.value)
		aux = aux.next
	end
	arr.buclesfor
end

#to_sObject

Convierte a un string la lista



54
55
56
57
58
59
60
61
62
63
# File 'lib/prct06/lista.rb', line 54

def to_s
	string = "("
	aux = @head
	while(aux != nil) do
		string += "#{aux.to_s}"
		@aux = @aux.next
	end	
	string += ")"
	return string
end