Class: List

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(head, tail) ⇒ List

Función initialize de la clase List



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

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

Instance Attribute Details

#headObject (readonly)

Atributos de clase List



7
8
9
# File 'lib/prct06/list.rb', line 7

def head
  @head
end

#tailObject (readonly)

Atributos de clase List



7
8
9
# File 'lib/prct06/list.rb', line 7

def tail
  @tail
end

Instance Method Details

#each(&block) ⇒ Object

Funciones enumerables de la clase



109
110
111
112
113
114
115
# File 'lib/prct06/list.rb', line 109

def each(&block)
  aux = @head
  while aux!=nil do
    yield aux.value
    aux = aux.nexst
  end
end

#emptyObject

Función que retorna si la lista es vacia o no



47
48
49
50
51
52
53
# File 'lib/prct06/list.rb', line 47

def empty
  if(@tail==nil)
    return true
  else
    return false
  end
end

#extractObject

Método de extracción



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

def extract
  if(@head==nil)
    puts "Lista vacia"
  else
    aux=@head
    @head=@head.nexst
    if(@head!=nil)
      @head.prev=nil
    end
    aux.nexst=nil
    if(@head==nil)
      @tail=nil
    end
    return aux
  end
end

#extract_tailObject

Método de extracción por la cola



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/prct06/list.rb', line 70

def extract_tail
  if(@tail==nil)
    puts "Lista vacia"
  else
    aux=tail
    @tail=@tail.prev
    if(@tail!=nil)
      @tail.nexst=nil
    end
    aux.prev=nil

    if(@tail==nil)
      @head=nil
    end
    return aux
  end
end

#insert(value) ⇒ Object

Método de inserción



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

def insert(value)
  node = Node.new(value, nil, nil)
  if(@tail==nil)
    @tail=node
    @head=node
  else
    node.prev=@tail
    @tail.nexst=node
    @tail=node
  end
end

#insert_head(value) ⇒ Object

Metodo de inserción por la cabeza



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/prct06/list.rb', line 56

def insert_head(value)
  node = Node.new(value, nil, nil)

  if(@head==nil)
    @tail=node
    @head=node
  else
    node.nexst=@head
    @head.prev=node
    @head=node
  end
end

#sort_eachObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/prct06/list.rb', line 132

def sort_each

  @aux = self.map { |x| x}
  @pos = 0

  @aux.each do |x|
    @pos = @pos + 1
    @aux[@pos..@aux.length-1] do |y|
      if (x>y)
        x, y = y, x
      end
    end
  end
end

#sort_forObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/prct06/list.rb', line 117

def sort_for
  @aux = self.map { |x| x }
  
  for x in 0..@aux.length-1
    for y in 0..@aux.length-2-x
      if ( @aux[y] > @aux[y+1] )
        @aux[y], @aux[y+1] = @aux[y+1], @aux[y]
      end
    end
  end

  return @aux
  
end

#to_sObject

Método to_s que combierte la clase a string



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/prct06/list.rb', line 89

def to_s
  aux=@head
  string="["
  
  if(@head!=nil)
    while aux!=nil do
      string+=aux.value.to_s+","
      if(aux.nexst!=nil)
        aux=aux.nexst
      else
        aux=nil
      end
    end
    string+="]"
  end

  return string
end