Class: List

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/p6/list.rb

Overview

Es una clase que representa una lista doblemente enlazada usando la clase Node

Author:

  • Guillermo Hernández González

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(head_, tail_) ⇒ List

Construye una lista doblemente enlazada tras pasarle dos parámetros, head y tail



21
22
23
24
# File 'lib/p6/list.rb', line 21

def initialize(head_,tail_)
    @head = head_
    @tail = tail_
end

Instance Attribute Details

#headObject (readonly)

Devuelve el nodo cabeza de la Lista

Returns:

  • (Object)

    the current value of head



15
16
17
# File 'lib/p6/list.rb', line 15

def head
  @head
end

#tailObject (readonly)

Devuelve el nodo cola de la Lista

Returns:

  • (Object)

    the current value of tail



15
16
17
# File 'lib/p6/list.rb', line 15

def tail
  @tail
end

Instance Method Details

#eachObject

Definición del método each necesario para que la clase sea enumerable



77
78
79
80
81
82
83
# File 'lib/p6/list.rb', line 77

def each 
    node=@head
    while(node!=nil) do
        yield node.value
        node = node.next
    end
end

#extract_headObject

Extrae el nodo de la cabeza, el nodo siguiente pasa a ser la siguiente cabeza y devuelve el valor de la antigua cabeza.



41
42
43
44
45
46
# File 'lib/p6/list.rb', line 41

def extract_head 
    temp = @head
    @head = @head.next
    @head.prev = nil
    return temp
end

#extract_tailObject

Extrae el nodo de la cola, el anterior pasa a ser la nueva cola y luego devuelve el valor de la cola antigua



57
58
59
60
61
62
63
64
65
66
# File 'lib/p6/list.rb', line 57

def extract_tail
    if @tail!=nil then
        temp = @tail
        @tail = @tail.prev
        if @tail!=nil then
            @tail.next = nil
        end
        return temp
    end
end

#insert_head(value) ⇒ Object

Inserta un nuevo nodo por la cabeza y la cabeza antigua pasa a ser el next



28
29
30
31
32
33
34
35
36
37
# File 'lib/p6/list.rb', line 28

def insert_head(value)
    if (head==nil) then
        @head=Node.new(value,nil,nil)
        @tail=@head
    else
        temp=Node.new(value,@head,nil)
        @head.prev=temp
        @head=temp
    end
end

#value_headObject

Devuelve el valor de la cabeza sin extraerla



50
51
52
53
# File 'lib/p6/list.rb', line 50

def value_head
    temp = @head
    return temp
end

#value_tailObject

Devuelve el valor de la cola sin extraerla



70
71
72
73
# File 'lib/p6/list.rb', line 70

def value_tail
    temp = @tail
    return temp
end