-
Notifications
You must be signed in to change notification settings - Fork 47
Branches, C. Gutierrez #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
|
||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not yet implemented |
||
|
|
||
| def view | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment.
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!