class DobLinkedList

Attributes

head[R]
length[R]
tail[R]

Public Class Methods

new() click to toggle source
# File lib/DobLinkedList.rb, line 28
def initialize
    @head   = nil
    @tail   = nil
    @length = 0
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/DobLinkedList.rb, line 137
def <=>(other)
    return nil unless other.instance_of? DobLinkedList
    
    value <=> other.value 
end
[](index) click to toggle source
# File lib/DobLinkedList.rb, line 172
def [](index)
    case index
        when 0, -@length
            @head
        when length-1, -1
            @tail
        else
            find_node(index)
    end
end
each() { |current| ... } click to toggle source
# File lib/DobLinkedList.rb, line 153
def each
    return nil unless block_given?    
    current = @head
    while current
        yield current
        current = current.next
    end
end
find_first(&predicate) click to toggle source
# File lib/DobLinkedList.rb, line 143
def find_first(&predicate)
    return nil unless block_given?    
    current = @head
    while current
        return current if predicate.call(current)
        current = current.next
    end
end
find_node(index) click to toggle source
# File lib/DobLinkedList.rb, line 162
def find_node (index)
    current = @head
    count = 0
    while current
        return current if count==index
        count += 1
        current = current.next
    end
end
insertHead(value) click to toggle source
# File lib/DobLinkedList.rb, line 47
def insertHead(value)
    node = Node.new(value, nil, nil)
    
    if @head
        node.next = @head
        @head.prev = node
        @head = node
    else
        @head = node
    end
    @length += 1
end
insertMany(valuesArray) click to toggle source
# File lib/DobLinkedList.rb, line 75
def insertMany(valuesArray)

    valuesArray.each {| value |   
  
    node = Node.new(value, nil, nil)
    
    unless @head
        @head = node
    else
        node.prev = @tail
        @tail.next = node
    end
    @tail = node
    @length += 1
    
    }
end
insertTail(value) click to toggle source
# File lib/DobLinkedList.rb, line 61
def insertTail(value)
    node = Node.new(value, nil, nil)
    
    unless @head
        @head = node
    else
        node.prev = @tail
        @tail.next = node
    end
    @tail = node
    @length += 1
end
remove(node) click to toggle source
# File lib/DobLinkedList.rb, line 119
def remove(node)
    return nil unless node    
    
    if node == @head
        if @head.next.nil?
            @head = @tail = nil
        else
            @head = @head.next
        end
    else
        pr = node.prev
        ne = node.next
        pr&.next = ne
        ne&.prev = pr
    end
    @length -= 1
end
remove_head() click to toggle source
# File lib/DobLinkedList.rb, line 93
def remove_head

    if @head.next.nil?
        @head = @tail = nil
    else
        @head = @head.next
        @head.prev = nil
    end

    @length -= 1
end
remove_tail() click to toggle source
# File lib/DobLinkedList.rb, line 105
def remove_tail

    if @tail.prev.nil?
        @tail = @head = nil

    else

        @tail = @tail.prev
        @tail.next = nil
    end

    @length -= 1
end
to_s() click to toggle source
# File lib/DobLinkedList.rb, line 34
def to_s
    listString = ""
    current = @head
    while current

        listString = listString + current.to_s 
        current = current.next
    end

    return listString
end