Class: Lista

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLista

Returns a new instance of Lista



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

def initialize
  @head = nil #Cabecera de la lista
  @tail = nil #Cola de la lista
  @size = 0 #TamaƱo de la lista
end

Instance Attribute Details

#headObject

Returns the value of attribute head



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

def head
  @head
end

#sizeObject

Returns the value of attribute size



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

def size
  @size
end

#tailObject

Returns the value of attribute tail



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

def tail
  @tail
end

Instance Method Details

#eachObject



153
154
155
156
157
158
159
# File 'lib/prct08/lista.rb', line 153

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

#emptyObject



149
150
151
# File 'lib/prct08/lista.rb', line 149

def empty
  return size==0
end

#extract_headObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/prct08/lista.rb', line 86

def extract_head
  if @head == nil
     @tail = nil
  else
    aux = @head
    @head = @head.next
    @size = @size - 1
  end
  return aux
end

#extract_pos(index) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/prct08/lista.rb', line 112

def extract_pos(index)
  pos = 0
  aux = @head
  if pos == index - 1
    extract_head()
  end
  if index == @size
    extract_tail()
  else
    while (pos != (index - 1)) && (aux.next != nil) do
        pos = pos + 1
        aux = aux.next
    end
    auxBef = aux.before
    auxNext =  aux.next
    auxBef[:next] = auxNext
    auxNext[:before] = auxBef
    return aux
    @size = @size - 1
  end
end

#extract_tailObject



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

def extract_tail
  if @head == @tail
    return @tail
    @head = nil
    @tail = nil
    @size = @size - 1
  else
    aux = @tail
    @tail = aux[:before]
    @tail[:next] = nil
    @size = @size - 1
    return aux
  end
end

#get_pos(index) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/prct08/lista.rb', line 42

def get_pos(index)
  pos = 0
  aux = @head
  while pos != (index - 1)  do
    pos = pos + 1
    aux = aux.next
  end
  return aux
end

#insert_head(nodo) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/prct08/lista.rb', line 12

def insert_head(nodo)
  if empty
    nodo[:next] = nil
    nodo[:before] = nil
    @head = nodo
    @tail = nodo
  else
    nodo[:next] = @head
    @head[:before] = nodo
    nodo[:before] = nil
    @head = nodo
  end
    @size = @size + 1
end

#insert_mul(nodos, index) ⇒ Object



79
80
81
82
83
84
# File 'lib/prct08/lista.rb', line 79

def insert_mul (nodos, index)
  for i in 0..(nodos.length - 1)
    insert_pos(nodos[i], index)
    index = index + 1
  end
end

#insert_pos(nodo, index) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/prct08/lista.rb', line 52

def insert_pos(nodo, index)
  pos = 0
  aux = @head
  if pos == 1
    insert_head(nodo)
  end
  if pos == size
    insert_tail(nodo)
  else
    while(pos != (index - 1) && (aux.next != nil)) do
      if pos == index - 2
        nodo[:next] = aux.next
        nodo[:before] = aux
        aux[:next] = nodo
      end
      if pos == index - 1
        aux = nodo.next
        aux[:before] = nodo
      else
        pos = pos + 1
        aux = aux.next
      end
      @size = @size + 1
    end
  end
end

#insert_tail(nodo) ⇒ Object



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

def insert_tail(nodo)
  if empty
    nodo[:next] = nil
    nodo[:before] = nil
    @head = nodo
    @tail = nodo
  else
    @tail[:next] = nodo
    nodo[:next] = nil
    nodo[:before] = @tail
    @tail = nodo
  end
  @size = @size + 1
end

#to_sObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/prct08/lista.rb', line 134

def to_s
  aux = @head
  if empty != true
    string = ""
    while aux.next != nil
      string += "\n\n#{aux.value}"
      aux = aux.next
    end
    string += "\n\n#{aux.value}"
    return string
  else
    return "Lista vacia"
  end
end