Class: List

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

Overview

lista.rb

Autor

Himar Manuel Barquín Carrasco

Web

github.com/ULL-ESIT-LPP-1920/tdd-alu0101119373

Descripción

Clase que representa una lista, utilizando una estrucutura llamada Node que contiene la información del valor de ese nodo, junto a su nodo predecesor y su nodo antecesor. Además, la lista dispone de una cabeza, una cola, y un valor que representa el tamaño. De esta manera, la clase List es tipo LIFO (cola).

Métodos

  • empty?

  • to_s

  • to_rs

  • insert

  • insert_tail

  • insert_more

  • extract

  • extract_tail

  • each

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeList

Constructor de la clase List



40
41
42
43
44
# File 'lib/tddAlimentos/lista.rb', line 40

def initialize
    @head = nil
    @tail = nil
    @size = 0
end

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



37
38
39
# File 'lib/tddAlimentos/lista.rb', line 37

def head
  @head
end

#sizeObject (readonly)

Returns the value of attribute size.



37
38
39
# File 'lib/tddAlimentos/lista.rb', line 37

def size
  @size
end

#tailObject (readonly)

Returns the value of attribute tail.



37
38
39
# File 'lib/tddAlimentos/lista.rb', line 37

def tail
  @tail
end

Instance Method Details

#eachObject

Permite que la clase List sea enumerable, lo que nos deja utilizar métodos como max(), min(), reverse_each(), collect(), etc



165
166
167
168
169
170
171
# File 'lib/tddAlimentos/lista.rb', line 165

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

#empty?Boolean

Comprueba si la lista está vacía

Devuelve

‘true’ si la lista está vacía. ‘false’ en otro caso

Returns:

  • (Boolean)


49
50
51
# File 'lib/tddAlimentos/lista.rb', line 49

def empty?
    @size == 0
end

#extractObject

Extrae el elemento en la cabeza

Devuelve

Valor extraído



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/tddAlimentos/lista.rb', line 131

def extract ()
    aux = nil
    if @head == @tail
        aux = @head
        @head = nil
        @tail = nil
    else
        aux = @head
        @head = @head.next
        @head.prev = nil
        aux.next = nil
    end
    aux.value
end

#extract_tailObject

Extrae el elemento en la cola de la lista

Devuelve

Valor extraído



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/tddAlimentos/lista.rb', line 149

def extract_tail ()
    aux = nil
    if @head == @tail
        aux = @head
        @head = nil
        @tail = nil
    else
        aux = @tail
        @tail = @tail.prev
        @tail.next = nil
        aux.prev = nil
    end
    aux.value
end

#insert(valor) ⇒ Object

Inserta un valor por la cabeza

Parámetro
  • valor: Valor a insertar



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/tddAlimentos/lista.rb', line 90

def insert (valor)
    n = Node.new(valor, nil, nil)
    if empty?
        @head = n
        @tail = n
    else
        n.next = @head
        @head.prev = n
        @head = n
    end
    @size += 1
end

#insert_more(arr) ⇒ Object

Inserta un conjunto de elementos en la cabeza

Parámetro
  • arr: Array de elementos a insertar



122
123
124
125
126
# File 'lib/tddAlimentos/lista.rb', line 122

def insert_more (arr)
    arr.reverse_each  do |value|
        self.insert(value)
    end
end

#insert_tail(valor) ⇒ Object

Inserta un valor por la cola

Parámetro
  • valor: Valor a insertar



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/tddAlimentos/lista.rb', line 106

def insert_tail (valor)
    n = Node.new(valor, nil, nil)
    if empty?
        @head = n
        @tail = n
    else
        n.prev = @tail
        @tail.next = n
        @tail = n
    end
    @size += 1
end

#to_rsObject

Muestra la lista desde la cola

Devuelve

Cadena de texto con los elementos de la lista en orden inverso



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/tddAlimentos/lista.rb', line 73

def to_rs
    str = "["
    aux = @tail
    while aux != nil do
        str += aux.value.to_s
        aux = aux.prev
        if aux != nil
            str += ","
        end
    end
    str += "]"
    str
end

#to_sObject

Muestra la lista desde la cabeza

Devuelve

Cadena de texto con los elementos de la lista



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tddAlimentos/lista.rb', line 56

def to_s
    str = "["
    aux = @head
    while aux != nil do
        str += aux.value.to_s
        aux = aux.next
        if aux != nil
            str += ","
        end
    end
    str += "]"
    str
end