forked from AdaGold/linked-list
-
Notifications
You must be signed in to change notification settings - Fork 47
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
Tatiana Linked List #17
Open
tatsqui
wants to merge
6
commits into
Ada-C11:master
Choose a base branch
from
tatsqui:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
12db63f
add_first and get_first are passing
tatsqui 4aeae14
max and min, length tests passing
tatsqui 86e015e
currently working on delete method
tatsqui 82d1604
delete tests passing
tatsqui e6d3bd0
all tests green
tatsqui 1823e80
almost done, last test not passing again
tatsqui File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,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) | ||
def initialize(value, next_node=nil) | ||
@data = value | ||
@next = next_node | ||
end | ||
|
@@ -18,73 +18,156 @@ def initialize | |
|
||
# 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 | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O | ||
# Space Complexity O | ||
def add_first(value) | ||
raise NotImplementedError | ||
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 | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(n) | ||
# Space Complexity: O | ||
def search(value) | ||
raise NotImplementedError | ||
current = @head | ||
|
||
while current | ||
if current.data == value | ||
return true | ||
else | ||
current = current.next | ||
end | ||
end | ||
return false | ||
end | ||
|
||
# method to return the max value in the linked list | ||
# returns the data value and not the node | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(n) | ||
# Space Complexity: O | ||
def find_max | ||
raise NotImplementedError | ||
visited_node = @head | ||
return nil if visited_node == nil | ||
|
||
max = visited_node.data | ||
|
||
while visited_node != nil | ||
if visited_node.data > max | ||
max = visited_node.data | ||
end | ||
visited_node = visited_node.next | ||
end | ||
return max | ||
end | ||
|
||
# method to return the min value in the linked list | ||
# returns the data value and not the node | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(n) | ||
# Space Complexity: O | ||
def find_min | ||
raise NotImplementedError | ||
return nil if @head == nil | ||
|
||
visited_node = @head | ||
min = visited_node.data | ||
|
||
while visited_node != nil | ||
if visited_node.data < min | ||
min = visited_node.data | ||
end | ||
visited_node = visited_node.next | ||
end | ||
return min | ||
end | ||
|
||
|
||
# method that returns the length of the singly linked list | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(n) - must count all nodes | ||
# Space Complexity: O - constant value returned | ||
def length | ||
raise NotImplementedError | ||
count = 0 | ||
current = @head | ||
|
||
if @head == nil | ||
return 0 | ||
else | ||
until current == nil | ||
current = current.next | ||
count += 1 | ||
end | ||
end | ||
return count | ||
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 | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(n) - worst case | ||
# Space Complexity: O - constant value returned | ||
def get_at_index(index) | ||
raise NotImplementedError | ||
current = @head | ||
|
||
return nil if @head == nil | ||
count = 0 | ||
|
||
while count < index && current.next != nil | ||
current = current.next | ||
count += 1 | ||
end | ||
|
||
return current.data | ||
end | ||
|
||
# method to print all the values in the linked list | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(n) | ||
# Space Complexity O - just printing to console, no new memory needed | ||
def visit | ||
raise NotImplementedError | ||
current = @head | ||
|
||
while current.next != nil | ||
puts @current.data | ||
current = current.next | ||
end | ||
# for last node | ||
puts current.data | ||
end | ||
|
||
# method to delete the first node found with specified value | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(n) | ||
# Space Complexity O | ||
def delete(value) | ||
raise NotImplementedError | ||
current = @head | ||
|
||
if @head == nil | ||
return 0 | ||
end | ||
|
||
# if head node has the value to delete | ||
if current.data == value | ||
@head = current.next | ||
end | ||
|
||
while current!= nil | ||
if current.next != nil && current.next.data == value | ||
current.next = current.next.next | ||
end | ||
current = current.next | ||
end | ||
end | ||
|
||
# method to reverse the singly linked list | ||
# note: the nodes should be moved and not just the values in the nodes | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(n) | ||
# Space Complexity O(n) | ||
def reverse | ||
raise NotImplementedError | ||
current = @head | ||
reversed_list = LinkedList.new | ||
|
||
while current.next != nil | ||
reversed_list.add_last(current) | ||
current = current.next | ||
end | ||
return reversed_list | ||
end | ||
|
||
|
||
|
@@ -98,10 +181,33 @@ def find_middle_value | |
|
||
# find the nth node from the end and return its value | ||
# assume indexing starts at 0 while counting to n | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(n) | ||
# Space Complexity O | ||
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. Space complexity O(1) |
||
def find_nth_from_end(n) | ||
raise NotImplementedError | ||
current = @head | ||
|
||
return nil if @head == nil | ||
|
||
# need to know length | ||
length = 0 | ||
while current != nil | ||
current = current.next | ||
length += 1 | ||
end | ||
|
||
# return position from the end | ||
position = length - n - 1 | ||
return nil if position < 0 | ||
|
||
search_node = @head | ||
starting_index = 0 | ||
while search_node != nil | ||
if starting_index == position | ||
return search_node.data | ||
end | ||
starting_index += 1 | ||
search_node = search_node.next | ||
end | ||
end | ||
|
||
# checks if the linked list has a cycle. A cycle exists if any node in the | ||
|
@@ -120,22 +226,44 @@ def has_cycle | |
# Time Complexity: | ||
# Space Complexity | ||
def get_first | ||
raise NotImplementedError | ||
return @head if @head.nil? | ||
|
||
first_node = @head | ||
return first_node.data | ||
end | ||
|
||
# method that inserts a given value as a new last node in the linked list | ||
# Time Complexity: | ||
# Time Complexity:O(n) | ||
# Space Complexity | ||
def add_last(value) | ||
raise NotImplementedError | ||
last_node = Node.new(value) | ||
current = @head | ||
|
||
if @head == nil | ||
@head = last_node | ||
else | ||
while current.next != nil | ||
current = current.next | ||
end | ||
current.next = last_node | ||
end | ||
end | ||
|
||
# method that returns the value of the last node in the linked list | ||
# returns nil if the linked list is empty | ||
# Time Complexity: | ||
# Space Complexity | ||
def get_last | ||
raise NotImplementedError | ||
count = 0 | ||
current = @head | ||
if !current | ||
return nil | ||
end | ||
|
||
while current.next != nil | ||
current = current.next | ||
end | ||
return current.data | ||
end | ||
|
||
# method to insert a new node with specific data value, assuming the linked | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This isn't working because it is adding the node to the new list, rather than the node's data. It's also adding them all to the last, rather than the front.
Lastly this loop is skipping the last node in the list (check the while condition).