Class: Lista

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

Overview

Clase lista doblemente enlazada.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLista

Metodo que inicializa los objetos de tipo Lista.



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

def initialize
  @head = @tail = nil
end

Instance Attribute Details

#headObject

Nodo del principio de la lista.



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

def head
  @head
end

#tailObject

Nodo del final de la lista.



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

def tail
  @tail
end

Instance Method Details

#eachObject

Metodo necesario para el uso del modulo Enumerable.



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

def each
  aux=@head
  while(aux!=nil)
    yield aux[:value]
    aux=aux[:next]
  end
end

#is_emptyObject

Metodo que devuelve true si la lista esta vacia.



18
19
20
21
22
23
24
25
26
# File 'lib/prct06/lista.rb', line 18

def is_empty

  if @head==nil && @tail==nil
    return true
  else
    return false
  end

end

#popObject

Metodo que saca un elemento de la lista por el principio.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/prct06/lista.rb', line 59

def pop
 
  if(!is_empty())
    nodo = @head
    @head = @head.next
    return nodo.value
  else
    return nil
  end

end

#pop_tailObject

Metodo que saca un elemento de la lista por el final.



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

def pop_tail
  if(@tail==nil)
    return false
  else
    nodo = @tail
    @tail = @tail.prev
  end
  return nodo.value
end

#push(v) ⇒ Object

Metodo que mete un elemento en la lista por el final.



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

def push(v)

  nodo = Node.new(v,nil,nil)
          
  if (@head == nil)
    @head = nodo
  else
    @tail.next = nodo
    nodo.prev = @tail
  end
  @tail=nodo

end

#push_head(v) ⇒ Object

Metodo que mete un elemento en la lista por el principio.



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/prct06/lista.rb', line 44

def push_head(v)

  nodo = Node.new(v,nil,nil)
  nodo.next = @head
  nodo.prev=nil
  if @tail==nil
    @final=nodo
  else
    @head.prev=nodo
  end
  
  @head = nodo
end

#to_sObject

Metodo que formatea la salida de la lista.



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

def to_s
  aux=@head
  i=1
  str=""
  while(aux!=nil)
    str+="#{i}) #{aux[:value]}\n"
    aux=aux[:next]
    i+=1
  end
  str
end