Class: Lista

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pr06/lista_d.rb

Overview

Representación de una lista doblemente enlazada de nodos

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



9
10
11
# File 'lib/pr06/lista_d.rb', line 9

def head
  @head
end

#tailObject (readonly)

Returns the value of attribute tail.



9
10
11
# File 'lib/pr06/lista_d.rb', line 9

def tail
  @tail
end

Instance Method Details

#eachNode

Método para la enumeración de los nodos de la lista

Returns:

  • (Node)

    devuelve los nodos que conforman la lista



94
95
96
97
98
99
100
# File 'lib/pr06/lista_d.rb', line 94

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

#pop_headany?

Extracción del primer nodo de la lista

Returns:

  • (any, nil)

    devuelve el valor guardado en el primer nodo o nil si no hay nodos en la lista



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pr06/lista_d.rb', line 56

def pop_head()
    if(@head == nil)
        return nil
    end
    
    x = @head
    @head = @head.next
    if(@head == nil)
        @tail = nil
    else
        @head.prev = nil
        x.next = nil
    end
    x.value
end

#pop_tailany?

Extracción del último nodo de la lista

Returns:

  • (any, nil)

    devuelve el valor guardado en el último nodo o nil si no hay nodos en la lista



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pr06/lista_d.rb', line 75

def pop_tail()
    if(@tail == nil)
        return nil
    end
    
    x = @tail
    @tail = @tail.prev
    if(@tail == nil)
        @head = nil
    else
        @tail.next = nil
        x.prev = nil
    end
    x.value
end

#push(x) ⇒ Node

Inserción por la cola de la lista de varios nodos

Parameters:

  • x (Array<any>)

    valores de los nuevos nodos

Returns:

  • (Node)

    Devuelve el tail



47
48
49
50
51
# File 'lib/pr06/lista_d.rb', line 47

def push(x)
    x.each{
        |i| push_tail(i)
    }
end

#push_head(x) ⇒ Node

Inserción por la cabeza de la lista

Parameters:

  • x (any)

    valor del nuevo nodo

Returns:

  • (Node)

    Devuelve el head



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pr06/lista_d.rb', line 15

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

#push_tail(x) ⇒ Node

Inserción por la cola de la lista

Parameters:

  • x (any)

    valor del nuevo nodo

Returns:

  • (Node)

    Devuelve el tail



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pr06/lista_d.rb', line 31

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