From 22a56d7be733d42b730cc568f2fc7b0d1fa66d39 Mon Sep 17 00:00:00 2001 From: Brianna Kemp Date: Fri, 6 Dec 2019 13:43:36 -0800 Subject: [PATCH] Implemented methods --- lib/linked_list.rb | 88 ++++++++++++++++++++++++++++------------ test/linked_list_test.rb | 56 ++++++++++++------------- 2 files changed, 90 insertions(+), 54 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 9e97557..ad91970 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -2,44 +2,80 @@ class LinkedList attr_reader :head - + def initialize @head = nil end - - # Time complexity - ? - # Space complexity - ? + + # Time complexity - O(1) + # Space complexity - O(1) def add_first(data) - - end - - # Time complexity - ? - # Space complexity - ? + current = head + @head = Node.new(data, current) + end + + # Time complexity - O(1) + # Space complexity - O(1) def get_first - + return nil if head.nil? + return head.data end - - # Time complexity - ? - # Space complexity - ? + + # Time complexity - O(n) + # Space complexity - O(1) def length - return 0 + node = head + count = 0 + + return 0 if head == nil + + until node == nil + count += 1 + node = node.next + end + return count end - - # Time complexity - ? - # Space complexity - ? + + # Time complexity - O(n) + # Space complexity - O(1) def add_last(data) - + if head.nil? + @head = Node.new(data) + return head.data + end + + current = head + until current.next == nil + current = current.next + end + + node = Node.new(data) + current.next = node + return node.data end - - # Time complexity - ? - # Space complexity - ? + + # Time complexity - O(n) + # Space complexity - O(1) def get_last - + current = head + + until current.next == nil + current = current.next + end + + return current.data end - - # Time complexity - ? - # Space complexity - ? + + # Time complexity - O(n) + # Space complexity - O(1) def get_at_index(index) - + return nil if head == nil + current = head + index.times do + current = current.next + end + + return current.data end + end diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 2a805c7..4c191a7 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -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 @@ -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