class List

Attributes

head[R]

Atributos de clase List

tail[R]

Atributos de clase List

Public Class Methods

new(head, tail) click to toggle source

Función initialize de la clase List

# File lib/prct06/list.rb, line 10
def initialize(head, tail)
        @head=head
        @tail=tail
end

Public Instance Methods

each() { |value| ... } click to toggle source

Funciones enumerables de la clase

# File lib/prct06/list.rb, line 109
def each(&block)
        aux = @head
        while aux!=nil do
                yield aux.value
                aux = aux.nexst
        end
end
empty() click to toggle source

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

# File lib/prct06/list.rb, line 47
def empty
        if(@tail==nil)
                return true
        else
                return false
        end
end
extract() click to toggle source

Método de extracción

# 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_tail() click to toggle source

Método de extracción por la cola

# 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) click to toggle source

Método de inserción

# 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) click to toggle source

Metodo de inserción por la cabeza

# 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_each() click to toggle source
# 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_for() click to toggle source
# 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_s() click to toggle source

Método to_s que combierte la clase a string

# 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