From 203efd0c815a56898bcf6941ecab5757a60d2fbf Mon Sep 17 00:00:00 2001 From: Natalie Tapias Date: Wed, 4 Dec 2019 14:26:27 -0800 Subject: [PATCH 1/5] fixing length" --- lib/linked_list.rb | 26 +++++++++++++++++++++----- test/linked_list_test.rb | 6 +++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 9e97557..491b050 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -10,19 +10,31 @@ def initialize # Time complexity - ? # Space complexity - ? def add_first(data) - + @head = Node.new(data, head) end # Time complexity - ? # Space complexity - ? def get_first - + return nil if @head.nil? + return @head.data end # Time complexity - ? # Space complexity - ? def length - return 0 + counter = 0 + current = @head + if current.nil? + return 0 + end + + while current != nil + current = current.next + counter += 1 + end + + return counter end # Time complexity - ? @@ -34,12 +46,16 @@ def add_last(data) # Time complexity - ? # Space complexity - ? def get_last - + # until nextNode = nil + # end end # Time complexity - ? # Space complexity - ? def get_at_index(index) - + index.times do + self.next + end + return self.value end end diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 2a805c7..f40487f 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -21,7 +21,7 @@ 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) @@ -51,7 +51,7 @@ end end - xdescribe "length" do + describe "length" do it "will return 0 for an empty list" do expect(@list.length).must_equal 0 end @@ -66,7 +66,7 @@ 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 From 1320625717d9bcbd08835ae34e2862234995cfde Mon Sep 17 00:00:00 2001 From: Natalie Tapias Date: Mon, 9 Dec 2019 21:25:12 -0800 Subject: [PATCH 2/5] still have two tests to work on --- lib/linked_list.rb | 35 ++++++++++++++++++++++++++--------- test/linked_list_test.rb | 2 +- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 491b050..84a4308 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -7,14 +7,14 @@ def initialize @head = nil end - # Time complexity - ? - # Space complexity - ? + # Time complexity - O(1) + # Space complexity - O(1) def add_first(data) @head = Node.new(data, head) end - # Time complexity - ? - # Space complexity - ? + # Time complexity - O(1) + # Space complexity - O(1) def get_first return nil if @head.nil? return @head.data @@ -40,22 +40,39 @@ def length # Time complexity - ? # Space complexity - ? def add_last(data) - + if @head.nil? + @head = Node.new(data, head) + else + current = @head + until current.next.nil? + current = current.next + end + end + # current.next = Node.new(data, nil) end # Time complexity - ? # Space complexity - ? def get_last - # until nextNode = nil - # end + if head.nil? + return nil + end + current = @head + + while current.next != nil + current = current.next + end + return current end # Time complexity - ? # Space complexity - ? def get_at_index(index) + return nil if @head.nil? + current = @head index.times do - self.next + current.next end - return self.value + return current end end diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index f40487f..fac4c41 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -91,7 +91,7 @@ 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 From 8dc6eed390868f2e312889bc9de76a47dbf82567 Mon Sep 17 00:00:00 2001 From: Natalie Tapias Date: Mon, 9 Dec 2019 21:30:27 -0800 Subject: [PATCH 3/5] get at index --- lib/linked_list.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 84a4308..071c388 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -20,8 +20,8 @@ def get_first return @head.data end - # Time complexity - ? - # Space complexity - ? + # Time complexity - ? O(n) where N is the number of elements in the linked list + # Space complexity - O(1) def length counter = 0 current = @head @@ -37,7 +37,7 @@ def length return counter end - # Time complexity - ? + # Time complexity - O(n) where n is the number of elements in the list # Space complexity - ? def add_last(data) if @head.nil? @@ -68,11 +68,13 @@ def get_last # Time complexity - ? # Space complexity - ? def get_at_index(index) - return nil if @head.nil? + if @head.nil? + return nil + end current = @head index.times do - current.next + current = current.next end - return current + return current.data end end From 8bb6522d9049f811eb34180e1bd3b752362529c0 Mon Sep 17 00:00:00 2001 From: Natalie Tapias Date: Mon, 9 Dec 2019 21:50:42 -0800 Subject: [PATCH 4/5] modified add last --- lib/linked_list.rb | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 071c388..bab3e6b 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -38,42 +38,43 @@ def length end # Time complexity - O(n) where n is the number of elements in the list - # Space complexity - ? + # Space complexity - O(1) def add_last(data) + new_node = Node.new(data, nil) if @head.nil? - @head = Node.new(data, head) + @head = new_node else - current = @head + current = head + until current.next.nil? current = current.next end + current.next = new_node end - # current.next = Node.new(data, nil) end - # Time complexity - ? - # Space complexity - ? + # Time complexity - O(n) where N is number of elements in the list + # Space complexity - O(1) def get_last - if head.nil? - return nil - end - current = @head + current = head while current.next != nil current = current.next end - return current + + return current.data end - # Time complexity - ? + # Time complexity - # Space complexity - ? def get_at_index(index) - if @head.nil? + if head.nil? return nil end - current = @head + current = head + index.times do - current = current.next + current = current.next end return current.data end From 07f8d7b5e9c1543828c42954f30aa35bb0591333 Mon Sep 17 00:00:00 2001 From: Natalie Tapias Date: Mon, 9 Dec 2019 21:56:07 -0800 Subject: [PATCH 5/5] adding time complexity --- lib/linked_list.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index bab3e6b..abbd809 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -65,8 +65,8 @@ def get_last return current.data end - # Time complexity - - # Space complexity - ? + # Time complexity - O(n) where N is the index + # Space complexity - ? O(1) def get_at_index(index) if head.nil? return nil