Skip to content
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

Hyunji Kim - Liked-List #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
half way
ricecakemonster committed Aug 31, 2017
commit 4eb5deb7ac9da37303b1bea908ac53853d6da31e
137 changes: 128 additions & 9 deletions linked_list.rb
Original file line number Diff line number Diff line change
@@ -19,52 +19,171 @@ 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
def insert(value)
puts "Not implemented"
current = Node.new(value)
if @head == nil
@head = current
else
current.next = @head
@head = current
end
return current
end

# method to find if the linked list contains a node with specified value
# returns true if found, false otherwise
def search(value)
puts "Not implemented"
if @head == nil
puts "This list is empty"
return
end
current = @head
until current.next == nil
if current == value
return true
end
current = current.next
end
return false
end

# method to return the max value in the linked list
# returns the data value and not the node
def find_max
puts "Not implemented"
if @head == nil
puts "This list is empty"
return
end
current = @head
max = @head
until current.next == nil
if current.next.data > max.data
max = current.next
end
current = current.next
end
return max.data
end

# method to return the min value in the linked list
# returns the data value and not the node
def find_min
puts "Not implemented"
if @head == nil
puts "This list is empty"
return
end
current = @head
min = @head
until current.next == nil
if current.next.data < min.data
min = current.next
end
current = current.next
end
return min.data
end

# method that returns the length of the singly linked list
def length
puts "Not implemented"
if @head == nil
return 0
end

current = @head
count = 1

until current.next == nil
current = current.next
count += 1
end
return count

end

# method to return the value of the nth element from the beginning
# assume indexing starts at 0 while counting to n
def find_nth_from_beginning(n)
puts "Not implemented"
if n < 0
puts "n must be 0 or bigger than 0"
return
elsif @head == nil
puts "This list is empty"
return
end

current = @head
# # while current.next != nil
n.times do
n -= 1
current = current.next
if current.next == nil && n > 0
puts "There's no nth element in this list"
return
end
end
return current.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)
puts "Not implemented"
new_node = Node.new(value)
current = @head

if @head == nil || @head.data > new_node.data
@head = new_node
@head.next = current
return
end

until current.next == nil
if current.next.data >= new_node.data
temp = current.next
current.next = new_node
new_node.next = temp
return
else
current = current.next
end
end

current.next = new_node

end

# method to print all the values in the linked list
def visit
puts "Not implemented"
current = @head
print "#{current.data} "
while current.next != nil
current = current.next
print "#{current.data} "
end
puts
end

# method to delete the first node found with specified value
def delete(value)
puts "Not implemented"
current = @head
if current.data == value && current.next == nil
@head = nil
elsif current == nil
puts "This list is empty"
return
end

until current.next == nil
previous = current
current = current.next
if current == value
previous.next = current.next
puts "here!!!!"
return
end
end

puts "#{value} is not found in this list"
return

end

# method to reverse the singly linked list