Skip to content

Leaves - Emily V #44

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
244 changes: 212 additions & 32 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,225 @@
require_relative 'node'

class LinkedList
attr_reader :head
# Defines a node in the singly linked list
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
end
end

# Defines the singly linked list
class LinkedList
def initialize
@head = nil
@head = nil # keep the head private. Not accessible outside this class
end

# Time complexity - ?
# Space complexity - ?
def add_first(data)

# 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)
@head = Node.new(value, @head)
end

# Time complexity - ?
# Space complexity - ?
def get_first


# method to find if the linked list contains a node with specified value
# returns true if found, false otherwise
def search(value)
current = @head
until current.nil?
return true if current.data == value
current = current.next
end
return false
end

# Time complexity - ?
# Space complexity - ?

# method to return the max value in the linked list
# returns the data value and not the node
def find_max
return nil if @head.nil?
max_data = @head.data
curr = @head
until curr == nil
max_data = curr.data if curr.data > max_data
curr = curr.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
return nil if @head.nil?
min_data = @head.data
curr = @head
until curr == nil
min_data = curr.data if curr.data < min_data
curr = curr.next
end
return min_data
end


# method that returns the length of the singly linked list
def length
return 0
length = 0
curr = @head
until curr.nil?
curr = curr.next
length += 1
end
return length
end

# Time complexity - ?
# Space complexity - ?
def add_last(data)


# 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)
curr = @head
index.times do
curr.nil? ? return : curr = curr.next
end
return curr.data
end

# Time complexity - ?
# Space complexity - ?

# method to print all the values in the linked list
def visit
curr = @head
until curr.nil?
puts curr.data
curr = curr.next
end
end

# method to delete the first node found with specified value
def delete(value)
curr = prev = @head
return if @head.nil?
@head = @head.next if @head.data == value
until curr.nil?
if curr.data == value
prev.next = curr.next
return
end
prev = curr
curr = curr.next
end
end

# method to reverse the singly linked list
# note: the nodes should be moved and not just the values in the nodes
def reverse
curr = @head
prev = nil
until curr.nil?
next_node = curr.next
curr.next = prev
prev = curr
curr = next_node
end
@head = prev
end


## Advanced Exercises
# returns the value at the middle element in the singly linked list
def find_middle_value
fast = slow = @head
until fast.next.nil?
2.times do
fast = fast.next if !fast.nil?
end
slow = slow.next
end
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)
return if @head.nil?
k_ahead = curr = @head
n.times do
k_ahead = k_ahead.next
return if k_ahead.nil?
end
until k_ahead.next.nil?
k_ahead = k_ahead.next
curr = curr.next
end
return curr.data
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
nodes_visited = {}
curr = @head
until curr.nil?
if nodes_visited[curr]
return true
else
nodes_visited[curr] = true
end
end
return false
end


# Additional Exercises
# returns the value in the first node
# returns nil if the list is empty
def get_first
return @head.nil? ? nil : @head.data
end

# method that inserts a given value as a new last node in the linked list
def add_last(value)
curr = @head
if curr.nil?
@head = Node.new(value, nil)
return
end
until curr.next == nil
curr = curr.next
end
curr.next = Node.new(value, nil)
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

return if @head.nil?
curr = @head
until curr.next.nil?
curr = curr.next
end
return curr.data
end

# Time complexity - ?
# Space complexity - ?
def get_at_index(index)


# method to insert a new node with specific data value, assuming the linked
# list is sorted in ascending order
def insert_ascending(value)
curr = @head
while curr.data < value
prev = curr
curr = curr.next
end
prev.next = Node.new(value, curr)
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
56 changes: 28 additions & 28 deletions test/linked_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,50 @@
before do
@list = LinkedList.new
end

describe 'initialize' do
it 'can be created' do

# Assert
expect(@list).must_be_kind_of LinkedList
end
end

xdescribe 'add_first & get_first' do
describe 'add_first & get_first' do
it 'can add values to an empty list' do
# Act
@list.add_first(3)

# Assert
expect(@list.get_first).must_equal 3
@list.add_first(3)
# Assert
expect(@list.get_first).must_equal 3
end

it 'will put the last added item to the front of the list' do
# Act
@list.add_first(1)
@list.add_first(2)

# Assert
expect(@list.get_first).must_equal 2

# Act again
@list.add_first(3)

# Assert
expect(@list.get_first).must_equal 3
end

it 'will return `nil` for `getFirst` if the list is empty' do

expect(@list.get_first).must_be_nil
end
end

xdescribe "length" do
describe "length" do
it "will return 0 for an empty list" do
expect(@list.length).must_equal 0
end

it "will return the length for nonempty lists" do
count = 0
while count < 5
Expand All @@ -65,43 +65,43 @@
end
end
end

xdescribe "addLast & getLast" do
describe "addLast & getLast" do
it "will add to the front if the list is empty" do
@list.add_last(1)
expect(@list.get_first).must_equal 1
end

it "will put new items to the rear of the list" do
@list.add_last(2)
expect(@list.length).must_equal 1
expect(@list.get_last).must_equal 2

@list.add_last(3)
expect(@list.get_first).must_equal 2
expect(@list.get_last).must_equal 3
expect(@list.length).must_equal 2

@list.add_last(4)
expect(@list.get_first).must_equal 2
expect(@list.get_last).must_equal 4
expect(@list.length).must_equal 3

end

end

xdescribe 'get_at_index' do
describe 'get_at_index' do
it 'returns nil if the index is outside the bounds of the list' do
expect(@list.get_at_index(3)).must_be_nil
end

it 'can retrieve an item at an index in the list' do
@list.add_first(1)
@list.add_first(2)
@list.add_first(3)
@list.add_first(4)

expect(@list.get_at_index(0)).must_equal 4
expect(@list.get_at_index(1)).must_equal 3
expect(@list.get_at_index(2)).must_equal 2
Expand Down