Class: List

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeList

Returns a new instance of List.



9
10
11
12
13
# File 'lib/biblio/lista.rb', line 9

def initialize()
    @head = nil
    @tail = nil
    
end

Instance Attribute Details

#headObject

Returns the value of attribute head.



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

def head
  @head
end

#tailObject

Returns the value of attribute tail.



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

def tail
  @tail
end

Instance Method Details

#eachObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/biblio/lista.rb', line 94

def each 
    
   if (@head == nil) and (@tail == nil)
       
        yield nil
        
    elsif (@head == @tail)
    
        yield @head.value
        
    else
        while (@head != nil)
        yield @head.value
        @head = @head.next
        end
   end
end

#empty?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
# File 'lib/biblio/lista.rb', line 15

def empty? 
  
  if @head == nil
      return true
  else
      return false
  end
end

#extract_beginningObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/biblio/lista.rb', line 68

def extract_beginning()

  if @head == nil
      return nil
  else
      temporal_node = @head
      @head = @head.next
      return temporal_node
  end
end

#extract_endObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/biblio/lista.rb', line 79

def extract_end()
    
     if @tail == nil
     return nil
    
     else
     
     temporal_node = @tail
     @tail = @tail.prev
     return temporal_node
     
     end
   
end

#insert_beginning(x) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/biblio/lista.rb', line 24

def insert_beginning(x)
   
   node = Node.new(x,nil,nil)
   
   if (@head == nil) 
       @head = node
       @tail = node
   else
       
     temporal_node = @head
     @head = node
      @head.next = temporal_node
      temporal_node.prev = @head
       
   end
  
end

#insert_end(x) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/biblio/lista.rb', line 43

def insert_end(x)
   
   node = Node.new(x,nil,nil)
   
    
   if (@head == nil)
       @head = node
       @tail = node
   else
       
    temporal_node = @tail
     @tail = node
      @tail.prev = temporal_node
      temporal_node.next = @tail
       
   end
end

#insert_multi(nodes) ⇒ Object



61
62
63
64
65
66
# File 'lib/biblio/lista.rb', line 61

def insert_multi(nodes)
    
    nodes.each do |nodo| 
        insert_beginning(nodo)
    end
end