class RubyStructures::LinkedList

Public Class Methods

new() click to toggle source

Public: Create a new instance of LinkedList

Examples

@linked_list = RubyStructures::LinkedList.new
# => LinkedList

Returns a new instance of LinkedList.

# File lib/rubystructures/linked_list.rb, line 11
def initialize
        @head = RubyStructures::Node.new(self, nil)
end

Public Instance Methods

append(value) click to toggle source

Public: Add a value to the end of the LinkedList

value - Ruby object to be inserted into the LinkedList

Examples

@linked_list.append(42)
# => Node

Returns the Node added to the LinkedList.

# File lib/rubystructures/linked_list.rb, line 66
def append(value)
        if self.empty?
                self.prepend(value)
                return
        end

        element = self.head
        # loop through each element in the LinkedList, until element.next
        # equals @head, signifying we have reached the end of the list.
        while(element != self.head)
                element = element.next
        end

        new_element = RubyStructures::Node.new(self, value)
        # insert the new element at the end of the LinkedList.
        new_element.next = element.next
        element.next = new_element
end
empty?() click to toggle source

Public: Checks to see if the LinkedList is empty.

Examples

@linked_list.empty?
# => false

Returns true if the LinkedList is empty, returns false otherwise.

# File lib/rubystructures/linked_list.rb, line 36
def empty?
        @head.next == nil
end
head() click to toggle source

Public: Returns the value at the head of the LinkedList.

Examples

@linked_list.head
# => 42

Returns the value held by the Node at the head of the list, otherwise if the list is empty, returns nil.

# File lib/rubystructures/linked_list.rb, line 24
def head
        @head.next
end
item_at(index) click to toggle source

Public: Returns the Node at the desired :index.

index - the Integer index value of Node to return.

Examples

@linked_list.item_at(42)
# => #<Node: >

Returns a Node located at position :index in the LinkedList.

# File lib/rubystructures/linked_list.rb, line 131
def item_at(index)
        element = self.head
        count = 0
        while count < index
                return nil if element.nil?
                element = element.next
                count += 1
        end
        element
end
item_at_head() click to toggle source

Public: Gets the Node at the head of the LinkedList.

Examples

@linked_list.item_at_head
# => #<Node: >

Returns the Node at the head of the LinkedList.

# File lib/rubystructures/linked_list.rb, line 117
def item_at_head
        @head.next
end
prepend(value) click to toggle source

Public: Add a value to the beginning of the LinkedList

value - Ruby object to be inserted into the LinkedList

Examples

@linked_list.prepend(42)
# => Node

Returns the Node added to the LinkedList.

# File lib/rubystructures/linked_list.rb, line 50
def prepend(value)
        element = RubyStructures::Node.new(self, value)
        element.next = @head.next
        @head.next = element
end
remove(value) click to toggle source

Public: Finds and removes the first occurrence of a Node with the desired value.

value - the Ruby object value to find and remove from the LinkedList

Examples

@linked_list.remove(42)
# => #<Node: >

Returns the node that was removed from the LinkedList

# File lib/rubystructures/linked_list.rb, line 153
def remove(value)
        element = self.head
        previous_element = @head
        while element.value != value
                if element.next.nil?
                        return nil
                else
                        previous_element = element
                        element = element.next
                end
        end

        previous_element.next = element.next
        element
end
remove_at_head() click to toggle source

Public: Removes the Node at the head of the LinkedList.

Examples

@linked_list.remove_at_head
# => #<Node: >

Returns the Node at the head of the list.

# File lib/rubystructures/linked_list.rb, line 177
def remove_at_head
        return nil if self.empty?
        element = self.head
        @head.next = nil || element.next
        element
end
remove_at_tail() click to toggle source

Public: Removes the Node at the tail of the LinkedList.

Examples

@linked_list.remove_at_tail
# => #<Node: >

Returns

# File lib/rubystructures/linked_list.rb, line 192
def remove_at_tail
        return nil if self.empty?
        element = self.head
        previous_element = @head

        until element.next.nil?
                previous_element = element
                element = element.next
        end

        previous_element.next = nil
        element
end