From cabacd2b7a7dfa134fee8e5f2411e62d581ac483 Mon Sep 17 00:00:00 2001 From: Farah Davoodi Date: Sun, 8 Dec 2019 21:38:57 -0800 Subject: [PATCH 1/4] completed addfirst and getfirst tests --- lib/linked_list.rb | 10 ++++++---- test/linked_list_test.rb | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 9e97557..a77b60b 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -1,4 +1,5 @@ require_relative 'node' +require 'pry' class LinkedList attr_reader :head @@ -7,16 +8,17 @@ def initialize @head = nil end - # Time complexity - ? - # Space complexity - ? + # Time complexity - O(1) where 1 is the one node created + # Space complexity - O(1) where 1 is the one node created in space 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 - ? diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 2a805c7..6be42e0 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) From 2ff81377ca9c2be7a71b4d76ea10a20d9e90cb55 Mon Sep 17 00:00:00 2001 From: Farah Davoodi Date: Sun, 8 Dec 2019 21:43:41 -0800 Subject: [PATCH 2/4] completed length tests --- lib/linked_list.rb | 16 +++++++++++----- test/linked_list_test.rb | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index a77b60b..e2c831d 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -14,17 +14,23 @@ def add_first(data) @head = Node.new(data, head) end - # Time complexity - ? - # Space complexity - ? + # Time complexity - O(1) because it is grabbing the first node in the list + # Space complexity - O(1) because it is returning one node def get_first return nil if head.nil? return @head.data end - # Time complexity - ? - # Space complexity - ? + # Time complexity - O(n) where n is the length of the linked list + # Space complexity - O(1) because only a single number is being returning, the length of the list def length - return 0 + count = 0 + current = @head + until current.nil? + count += 1 + current = current.next + end + return count end # Time complexity - ? diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 6be42e0..1385216 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -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 From 991938488c24712a628c1016e4c0ffabac9c69cb Mon Sep 17 00:00:00 2001 From: Farah Davoodi Date: Sun, 8 Dec 2019 22:05:15 -0800 Subject: [PATCH 3/4] completed tests for addlast and getlast --- lib/linked_list.rb | 15 +++++++++++++++ test/linked_list_test.rb | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index e2c831d..22ef7eb 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -36,13 +36,28 @@ def length # Time complexity - ? # Space complexity - ? def add_last(data) + current = @head + + if current.nil? + add_first(data) + return + end + until current.next == nil + current = current.next + end + current.next = Node.new(data) end # Time complexity - ? # Space complexity - ? def get_last + current = @head + until current.next == nil + current = current.next + end + return current.data end # Time complexity - ? diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 1385216..f40487f 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -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 3aa032e36619de5a9fdce86a827bda9cd9d135d1 Mon Sep 17 00:00:00 2001 From: Farah Davoodi Date: Sun, 8 Dec 2019 22:12:09 -0800 Subject: [PATCH 4/4] completed get at index tests --- lib/linked_list.rb | 23 +++++++++++++++++------ test/linked_list_test.rb | 2 +- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 22ef7eb..7a73a77 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -33,8 +33,8 @@ def length return count end - # Time complexity - ? - # Space complexity - ? + # Time complexity - O(n) where n is the length of the entire list + # Space complexity - O(1) because you are adding one node def add_last(data) current = @head @@ -49,8 +49,8 @@ def add_last(data) current.next = Node.new(data) end - # Time complexity - ? - # Space complexity - ? + # Time complexity - O(n) where n is the length of the entire list + # Space complexity - O(1) because you are returning informatio on one node def get_last current = @head until current.next == nil @@ -60,9 +60,20 @@ def get_last return current.data end - # Time complexity - ? - # Space complexity - ? + # Time complexity - O(n) where n is the length of the list, in worst case scenario + # Space complexity - O(1) because only single node data is returned def get_at_index(index) + current = @head + if current.nil? + return nil + end + i = 0 + while i != index + i += 1 + current = current. next + end + + return current.data 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