-
Notifications
You must be signed in to change notification settings - Fork 49
Leaves_Ga-Young #46
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?
Leaves_Ga-Young #46
Changes from all commits
c39aff5
201e22d
b446fb5
6a92e64
cd7c13d
0855b8a
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
class Node | ||
attr_reader :data # allow external entities to read value but not write | ||
attr_accessor :next # allow external entities to read or write next node | ||
|
||
def initialize(value, next_node = nil) | ||
@data = value | ||
@next = next_node | ||
|
@@ -12,120 +12,275 @@ def initialize(value, next_node = nil) | |
|
||
# Defines the singly linked list | ||
class LinkedList | ||
def initialize | ||
@head = nil # keep the head private. Not accessible outside this class | ||
def initialize | ||
@head = nil # keep the head private. Not accessible outside this class | ||
end | ||
|
||
# method to add a new node with the specific data value in the linked list | ||
# insert the new node at the beginning of the linked list | ||
def add_first(value) | ||
new_node = Node.new(value, @head) | ||
@head = new_node | ||
end | ||
|
||
# method to find if the linked list contains a node with specified value | ||
# returns true if found, false otherwise | ||
def search(value) | ||
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 | ||
|
||
until current == nil | ||
if current.data == value | ||
return true | ||
else | ||
current = current.next | ||
end | ||
end | ||
|
||
# method to add a new node with the specific data value in the linked list | ||
# insert the new node at the beginning of the linked list | ||
def add_first(value) | ||
raise NotImplementedError | ||
|
||
return false | ||
end | ||
|
||
# method to return the max value in the linked list | ||
# returns the data value and not the node | ||
def find_max | ||
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 | ||
|
||
# method to find if the linked list contains a node with specified value | ||
# returns true if found, false otherwise | ||
def search(value) | ||
raise NotImplementedError | ||
|
||
max = current.data | ||
|
||
until current == nil | ||
if current.data > max | ||
max = current.data | ||
end | ||
current = current.next | ||
end | ||
|
||
# method to return the max value in the linked list | ||
# returns the data value and not the node | ||
def find_max | ||
raise NotImplementedError | ||
|
||
return max | ||
end | ||
|
||
# method to return the min value in the linked list | ||
# returns the data value and not the node | ||
def find_min | ||
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 | ||
|
||
# method to return the min value in the linked list | ||
# returns the data value and not the node | ||
def find_min | ||
raise NotImplementedError | ||
|
||
min = current.data | ||
|
||
until current == nil | ||
if current.data < min | ||
min = current.data | ||
end | ||
current = current.next | ||
end | ||
|
||
|
||
# method that returns the length of the singly linked list | ||
def length | ||
raise NotImplementedError | ||
|
||
return min | ||
end | ||
|
||
|
||
# method that returns the length of the singly linked list | ||
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 | ||
length = 0 | ||
|
||
if @head == nil | ||
return length | ||
else | ||
until current == nil | ||
current = current.next | ||
length += 1 | ||
end | ||
return length | ||
end | ||
|
||
# method that returns the value at a given index in the linked list | ||
# index count starts at 0 | ||
# returns nil if there are fewer nodes in the linked list than the index value | ||
def get_at_index(index) | ||
raise NotImplementedError | ||
end | ||
|
||
# method that returns the value at a given index in the linked list | ||
# index count starts at 0 | ||
# returns nil if there are fewer nodes in the linked list than the index value | ||
def get_at_index(index) | ||
Comment on lines
+101
to
+104
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 | ||
counter = 0 | ||
|
||
if current == nil | ||
return nil | ||
end | ||
|
||
# method to print all the values in the linked list | ||
def visit | ||
raise NotImplementedError | ||
|
||
while counter <= index | ||
if current == nil | ||
return nil | ||
elsif counter == index | ||
return current.data | ||
end | ||
|
||
current = current.next | ||
counter += 1 | ||
end | ||
|
||
# method to delete the first node found with specified value | ||
def delete(value) | ||
raise NotImplementedError | ||
end | ||
|
||
# method to print all the values in the linked list | ||
def visit | ||
raise NotImplementedError | ||
end | ||
|
||
# method to delete the first node found with specified value | ||
def delete(value) | ||
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 | ||
previous = nil | ||
|
||
until current == nil | ||
if current.data == value | ||
if previous == nil | ||
@head = current.next | ||
else | ||
previous.next = current.next | ||
end | ||
end | ||
|
||
previous = current | ||
current = current.next | ||
end | ||
|
||
# method to reverse the singly linked list | ||
# note: the nodes should be moved and not just the values in the nodes | ||
def reverse | ||
raise NotImplementedError | ||
end | ||
|
||
# method to reverse the singly linked list | ||
# note: the nodes should be moved and not just the values in the nodes | ||
def reverse | ||
Comment on lines
+148
to
+150
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_node = @head | ||
previous_node = nil | ||
next_node = nil | ||
|
||
if current_node == nil | ||
return nil | ||
end | ||
|
||
|
||
## Advanced Exercises | ||
# returns the value at the middle element in the singly linked list | ||
def find_middle_value | ||
raise NotImplementedError | ||
|
||
until current_node == nil | ||
next_node = current_node.next | ||
if previous_node == nil | ||
current_node.next = nil | ||
else | ||
current_node.next = previous_node | ||
end | ||
previous_node = current_node | ||
current_node = next_node | ||
end | ||
|
||
# find the nth node from the end and return its value | ||
# assume indexing starts at 0 while counting to n | ||
def find_nth_from_end(n) | ||
raise NotImplementedError | ||
|
||
@head = previous_node | ||
end | ||
|
||
|
||
## Advanced Exercises | ||
# returns the value at the middle element in the singly linked list | ||
def find_middle_value | ||
raise NotImplementedError | ||
end | ||
|
||
# find the nth node from the end and return its value | ||
# assume indexing starts at 0 while counting to n | ||
def find_nth_from_end(n) | ||
Comment on lines
+180
to
+182
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 is one way to do it, but you can do it with less space complexity. |
||
current = @head | ||
counter = 0 | ||
all_data = [] | ||
|
||
if current == nil | ||
return nil | ||
end | ||
|
||
# checks if the linked list has a cycle. A cycle exists if any node in the | ||
# linked list links to a node already visited. | ||
# returns true if a cycle is found, false otherwise. | ||
def has_cycle | ||
raise NotImplementedError | ||
|
||
until current == nil | ||
all_data << current.data | ||
current = current.next | ||
counter += 1 | ||
end | ||
|
||
|
||
# Additional Exercises | ||
# returns the value in the first node | ||
# returns nil if the list is empty | ||
def get_first | ||
raise NotImplementedError | ||
|
||
if n >= all_data.length | ||
return nil | ||
end | ||
|
||
# method that inserts a given value as a new last node in the linked list | ||
def add_last(value) | ||
raise NotImplementedError | ||
|
||
return all_data[all_data.length - (n + 1)] | ||
end | ||
|
||
# checks if the linked list has a cycle. A cycle exists if any node in the | ||
# linked list links to a node already visited. | ||
# returns true if a cycle is found, false otherwise. | ||
def has_cycle | ||
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. 👍 |
||
fast = @head.next | ||
slow = @head | ||
|
||
until fast.nil? || slow.nil? | ||
if fast == slow | ||
return true | ||
end | ||
|
||
fast = fast.next | ||
fast = fast.next unless fast.nil? | ||
slow = slow.next | ||
end | ||
|
||
# method that returns the value of the last node in the linked list | ||
# returns nil if the linked list is empty | ||
def get_last | ||
raise NotImplementedError | ||
|
||
return false | ||
end | ||
|
||
|
||
# Additional Exercises | ||
# returns the value in the first node | ||
# returns nil if the list is empty | ||
def get_first | ||
if @head == nil | ||
return nil | ||
else | ||
return @head.data | ||
end | ||
|
||
# method to insert a new node with specific data value, assuming the linked | ||
# list is sorted in ascending order | ||
def insert_ascending(value) | ||
raise NotImplementedError | ||
end | ||
|
||
# method that inserts a given value as a new last node in the linked list | ||
def add_last(value) | ||
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. 👍 |
||
new_node = Node.new(value, nil) | ||
current = @head | ||
|
||
if current == nil | ||
@head = new_node | ||
else | ||
until current.next == nil | ||
current = current.next | ||
end | ||
current.next = new_node | ||
end | ||
|
||
# Helper method for tests | ||
# Creates a cycle in the linked list for testing purposes | ||
# Assumes the linked list has at least one node | ||
def create_cycle | ||
return if @head == nil # don't do anything if the linked list is empty | ||
|
||
# navigate to last node | ||
current = @head | ||
while current.next != nil | ||
current = current.next | ||
end | ||
|
||
# method that returns the value of the last node in the linked list | ||
# returns nil if the linked list is empty | ||
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 | ||
else | ||
until current.next == nil | ||
current = current.next | ||
end | ||
|
||
current.next = @head # make the last node link to first node | ||
return current.data | ||
end | ||
end | ||
|
||
# method to insert a new node with specific data value, assuming the linked | ||
# list is sorted in ascending order | ||
def insert_ascending(value) | ||
raise NotImplementedError | ||
end | ||
|
||
# Helper method for tests | ||
# Creates a cycle in the linked list for testing purposes | ||
# Assumes the linked list has at least one node | ||
def create_cycle | ||
return if @head == nil # don't do anything if the linked list is empty | ||
|
||
# navigate to last node | ||
current = @head | ||
while current.next != nil | ||
current = current.next | ||
end | ||
|
||
current.next = @head # make the last node link to first node | ||
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.
👍