Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 195 additions & 0 deletions lib/double_linked_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
# Defines a node in the double linked list
class Node
attr_reader :data
attr_accessor :next
attr_accessor :prev

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice you're implementing a Doubly Linked List!


def initialize(value, next_node = nil, previous_node = nil)
@data = value
@next = next_node
@prev = previous_node
end
end

class DoubleLinkedList

def initialize
@head = nil
end

def add_first(value)
current = @head
temp = Node.new(value)
if current == nil
@head = temp
else
temp.next = current
current.prev = temp
@head = temp
end
end

def get_first

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

current = @head
if current == nil
return nil
else
return current.data
end
end

def length

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

current = @head
if current == nil
return 0
end

length = 1

until current.next == nil do
current = current.next
length += 1
end

return length

end

def add_last(value)
# create a new node
temp = Node.new(value)

# set current
current = @head

# shortcut for an empty list
if current == nil
add_first(value)
return
elsif current.next == nil
current.next = temp
temp.prev = current
return
end
Comment on lines +69 to +73

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need this elsif?


# increment current to pentultimate node
until current.next == nil do
current = current.next
end

current.next = temp
temp.prev = current

end

def get_last

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

current = @head

if current == nil
return nil
end

until current.next == nil do
current = current.next
end

return current.data
end

def get_at_index(index)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 This definitely works, but could be simplified a bit.

current = @head

if current == nil
return nil
elsif index == 0
return current.data
end

counter = 0

until current.next == nil do
if counter == index
return current.data
else
current = current.next
counter += 1
end
end

if index > counter
return nil
else
return current.data
end

end

def find_nth_from_end(index)
current = @head

if current == nil
return nil
end

length = 0
# get current to the end
until current.next == nil do
current = current.next
length += 1
end
Comment on lines +134 to +139

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're finding the length of the list here, why not use the length method?


# see if the index value is longer than the list
if index > length
return nil
elsif index == 0
return current.data
end

#hop back
counter = 0
until counter == index do
current = current.prev
counter += 1
end

return current.data
end

def reverse

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ This doesn't actually reverse the list because it doesn't re-arrange the previous and next pointers.

current = @head

if current == nil || current.next == nil
return
end

hopper = @head

until hopper.next == nil do
hopper = hopper.next
end

@head = current

return

end

def delete(value)
end
Comment on lines +177 to +178

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet implemented


def view

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

current = @head

if current == nil
return
end

until current.next == nil
puts current.data
current = current.next
end
puts current.data

end

end
Loading