Class: Alimento::List
- Inherits:
-
Object
- Object
- Alimento::List
- Includes:
- Enumerable
- Defined in:
- lib/alimento/list.rb
Instance Attribute Summary collapse
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#tail ⇒ Object
readonly
Returns the value of attribute tail.
Instance Method Summary collapse
- #each ⇒ Object
-
#extract_head ⇒ Node
Nodo de la lista extraido.
-
#extract_tail ⇒ Node
Nodo de la lista extraido.
-
#initialize(value) ⇒ List
constructor
A new instance of List.
- #insert_head(value) ⇒ Object
- #insert_sundry(value_array) ⇒ Object
- #insert_tail(value) ⇒ Object
Constructor Details
Instance Attribute Details
#head ⇒ Object (readonly)
Returns the value of attribute head
11 12 13 |
# File 'lib/alimento/list.rb', line 11 def head @head end |
#tail ⇒ Object (readonly)
Returns the value of attribute tail
11 12 13 |
# File 'lib/alimento/list.rb', line 11 def tail @tail end |
Instance Method Details
#each ⇒ Object
Note:
Recorrer la lista
22 23 24 25 26 27 28 29 |
# File 'lib/alimento/list.rb', line 22 def each return nil unless block_given? actual = self.tail while actual yield actual actual = actual.next end end |
#extract_head ⇒ Node
Note:
Extraer el elemento en la cabeza de la lista
Returns Nodo de la lista extraido
66 67 68 69 70 71 72 |
# File 'lib/alimento/list.rb', line 66 def extract_head actual_head = @head new_head = actual_head.prev actual_head.prev = nil new_head.next = nil return new_head end |
#extract_tail ⇒ Node
Note:
Extraer elemento en la cabeza de la lista
Returns Nodo de la lista extraido
77 78 79 80 81 82 83 |
# File 'lib/alimento/list.rb', line 77 def extract_tail actual_tail = @tail new_tail = actual_tail.next actual_tail.next = nil new_tail.prev = nil return new_tail end |
#insert_head(value) ⇒ Object
Note:
Insertar elemento en la cabeza de la lista
34 35 36 37 38 39 |
# File 'lib/alimento/list.rb', line 34 def insert_head(value) actual_head = @head new_head = Node.new(value, nil,actual_head) actual_head.next = new_head @head = new_head end |
#insert_sundry(value_array) ⇒ Object
Note:
Insertar varios elementos en la lista
54 55 56 57 58 59 60 |
# File 'lib/alimento/list.rb', line 54 def insert_sundry(value_array) i = 0 while i < value_array.length do insert_head(value_array[i]) i += 1 end end |
#insert_tail(value) ⇒ Object
Note:
Insertar elemento por la cola de la lista
44 45 46 47 48 49 |
# File 'lib/alimento/list.rb', line 44 def insert_tail(value) actual_tail = @tail new_tail = Node.new(value, actual_tail, nil) actual_tail.prev = new_tail @tail = new_tail end |