Class: Lista

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(valor) ⇒ Lista

Constructor initialize



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

def initialize(valor)
   @head = Node.new(valor,nil,nil)
   @tail = @head
end

Instance Attribute Details

#headObject

Returns the value of attribute head.



4
5
6
# File 'lib/Dieta/lista.rb', line 4

def head
  @head
end

#tailObject

Returns the value of attribute tail.



4
5
6
# File 'lib/Dieta/lista.rb', line 4

def tail
  @tail
end

Instance Method Details

#eachObject

Each



14
15
16
17
18
19
20
# File 'lib/Dieta/lista.rb', line 14

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

#emptyObject

Empty



23
24
25
# File 'lib/Dieta/lista.rb', line 23

def empty
   @head.value==nil
end

#extract_headObject

Head



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

def extract_head
   if empty
       nil
   else
      aux = @head.value
      @head = @head.next
      @head.prev = nil
      aux
   end
end

#extract_tailObject

Tail



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

def extract_tail
   if empty
       nil
   else
      aux = @tail.value
      @tail = @tail.prev
      @tail.next = nil
      aux
   end
end

#insert_head(valor) ⇒ Object

Head



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

def insert_head(valor)
   if empty
       aux = Node.new(valor, nil, nil)
       @head = @tail = aux
   else    
       aux = Node.new(valor, nil, @head)
       @head.prev = aux
       @head = aux
   end
end

#insert_tail(valor) ⇒ Object

Tail



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

def insert_tail(valor)
   if empty
       aux = Node.new(valor, nil, nil)
       @head = @tail = aux
   else   
       aux = Node.new(valor, @tail, nil)
       @tail.next = aux
       @tail = aux
   end
end