From 128a6db7ddf3ec49d1f9c3feca5bdf0d94a0d3c3 Mon Sep 17 00:00:00 2001 From: MannyMb Date: Tue, 27 Jul 2021 02:33:18 -0400 Subject: [PATCH 1/5] SAving progress --- .idea/compiler.xml | 2 + .idea/jarRepositories.xml | 20 + .../Maven__com_beust_jcommander_1_78.xml | 13 + .idea/libraries/Maven__junit_junit_4_12.xml | 13 + ..._org_apiguardian_apiguardian_api_1_1_1.xml | 13 + .../Maven__org_hamcrest_hamcrest_core_1_3.xml | 13 + .../Maven__org_jetbrains_annotations_13_0.xml | 13 + ..._jetbrains_kotlin_kotlin_stdlib_1_4_20.xml | 13 + ...ins_kotlin_kotlin_stdlib_common_1_4_20.xml | 13 + ...rains_kotlin_kotlin_stdlib_jdk7_1_4_20.xml | 13 + ...rains_kotlin_kotlin_stdlib_jdk8_1_4_20.xml | 13 + ...g_junit_jupiter_junit_jupiter_5_8_0_M1.xml | 13 + ...nit_jupiter_junit_jupiter_api_5_8_0_M1.xml | 13 + ..._jupiter_junit_jupiter_engine_5_8_0_M1.xml | 13 + ..._jupiter_junit_jupiter_params_5_8_0_M1.xml | 13 + ...atform_junit_platform_commons_1_8_0_M1.xml | 13 + ...latform_junit_platform_engine_1_8_0_M1.xml | 13 + ...Maven__org_opentest4j_opentest4j_1_2_0.xml | 13 + .../Maven__org_testng_testng_7_4_0.xml | 13 + .../Maven__org_webjars_jquery_3_5_1.xml | 13 + .idea/modules.xml | 1 + .idea/workspace.xml | 364 +++++------------- pom.xml | 20 + .../singlylinkedlist/SinglyLinkedList.java | 84 ++++ .../SinglyLinkedListTest.java | 86 +++++ 25 files changed, 550 insertions(+), 261 deletions(-) create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/libraries/Maven__com_beust_jcommander_1_78.xml create mode 100644 .idea/libraries/Maven__junit_junit_4_12.xml create mode 100644 .idea/libraries/Maven__org_apiguardian_apiguardian_api_1_1_1.xml create mode 100644 .idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml create mode 100644 .idea/libraries/Maven__org_jetbrains_annotations_13_0.xml create mode 100644 .idea/libraries/Maven__org_jetbrains_kotlin_kotlin_stdlib_1_4_20.xml create mode 100644 .idea/libraries/Maven__org_jetbrains_kotlin_kotlin_stdlib_common_1_4_20.xml create mode 100644 .idea/libraries/Maven__org_jetbrains_kotlin_kotlin_stdlib_jdk7_1_4_20.xml create mode 100644 .idea/libraries/Maven__org_jetbrains_kotlin_kotlin_stdlib_jdk8_1_4_20.xml create mode 100644 .idea/libraries/Maven__org_junit_jupiter_junit_jupiter_5_8_0_M1.xml create mode 100644 .idea/libraries/Maven__org_junit_jupiter_junit_jupiter_api_5_8_0_M1.xml create mode 100644 .idea/libraries/Maven__org_junit_jupiter_junit_jupiter_engine_5_8_0_M1.xml create mode 100644 .idea/libraries/Maven__org_junit_jupiter_junit_jupiter_params_5_8_0_M1.xml create mode 100644 .idea/libraries/Maven__org_junit_platform_junit_platform_commons_1_8_0_M1.xml create mode 100644 .idea/libraries/Maven__org_junit_platform_junit_platform_engine_1_8_0_M1.xml create mode 100644 .idea/libraries/Maven__org_opentest4j_opentest4j_1_2_0.xml create mode 100644 .idea/libraries/Maven__org_testng_testng_7_4_0.xml create mode 100644 .idea/libraries/Maven__org_webjars_jquery_3_5_1.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 4be381c..a07d5fa 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -6,11 +6,13 @@ + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_beust_jcommander_1_78.xml b/.idea/libraries/Maven__com_beust_jcommander_1_78.xml new file mode 100644 index 0000000..788fa16 --- /dev/null +++ b/.idea/libraries/Maven__com_beust_jcommander_1_78.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__junit_junit_4_12.xml b/.idea/libraries/Maven__junit_junit_4_12.xml new file mode 100644 index 0000000..d411041 --- /dev/null +++ b/.idea/libraries/Maven__junit_junit_4_12.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apiguardian_apiguardian_api_1_1_1.xml b/.idea/libraries/Maven__org_apiguardian_apiguardian_api_1_1_1.xml new file mode 100644 index 0000000..f555713 --- /dev/null +++ b/.idea/libraries/Maven__org_apiguardian_apiguardian_api_1_1_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000..f58bbc1 --- /dev/null +++ b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_jetbrains_annotations_13_0.xml b/.idea/libraries/Maven__org_jetbrains_annotations_13_0.xml new file mode 100644 index 0000000..e2c8297 --- /dev/null +++ b/.idea/libraries/Maven__org_jetbrains_annotations_13_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_jetbrains_kotlin_kotlin_stdlib_1_4_20.xml b/.idea/libraries/Maven__org_jetbrains_kotlin_kotlin_stdlib_1_4_20.xml new file mode 100644 index 0000000..70c9b42 --- /dev/null +++ b/.idea/libraries/Maven__org_jetbrains_kotlin_kotlin_stdlib_1_4_20.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_jetbrains_kotlin_kotlin_stdlib_common_1_4_20.xml b/.idea/libraries/Maven__org_jetbrains_kotlin_kotlin_stdlib_common_1_4_20.xml new file mode 100644 index 0000000..24afaa1 --- /dev/null +++ b/.idea/libraries/Maven__org_jetbrains_kotlin_kotlin_stdlib_common_1_4_20.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_jetbrains_kotlin_kotlin_stdlib_jdk7_1_4_20.xml b/.idea/libraries/Maven__org_jetbrains_kotlin_kotlin_stdlib_jdk7_1_4_20.xml new file mode 100644 index 0000000..0af2311 --- /dev/null +++ b/.idea/libraries/Maven__org_jetbrains_kotlin_kotlin_stdlib_jdk7_1_4_20.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_jetbrains_kotlin_kotlin_stdlib_jdk8_1_4_20.xml b/.idea/libraries/Maven__org_jetbrains_kotlin_kotlin_stdlib_jdk8_1_4_20.xml new file mode 100644 index 0000000..3d09f6a --- /dev/null +++ b/.idea/libraries/Maven__org_jetbrains_kotlin_kotlin_stdlib_jdk8_1_4_20.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_5_8_0_M1.xml b/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_5_8_0_M1.xml new file mode 100644 index 0000000..f0982aa --- /dev/null +++ b/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_5_8_0_M1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_api_5_8_0_M1.xml b/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_api_5_8_0_M1.xml new file mode 100644 index 0000000..6ba70f1 --- /dev/null +++ b/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_api_5_8_0_M1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_engine_5_8_0_M1.xml b/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_engine_5_8_0_M1.xml new file mode 100644 index 0000000..467a7b6 --- /dev/null +++ b/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_engine_5_8_0_M1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_params_5_8_0_M1.xml b/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_params_5_8_0_M1.xml new file mode 100644 index 0000000..d33fe56 --- /dev/null +++ b/.idea/libraries/Maven__org_junit_jupiter_junit_jupiter_params_5_8_0_M1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_junit_platform_junit_platform_commons_1_8_0_M1.xml b/.idea/libraries/Maven__org_junit_platform_junit_platform_commons_1_8_0_M1.xml new file mode 100644 index 0000000..1332c3d --- /dev/null +++ b/.idea/libraries/Maven__org_junit_platform_junit_platform_commons_1_8_0_M1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_junit_platform_junit_platform_engine_1_8_0_M1.xml b/.idea/libraries/Maven__org_junit_platform_junit_platform_engine_1_8_0_M1.xml new file mode 100644 index 0000000..4e2a10f --- /dev/null +++ b/.idea/libraries/Maven__org_junit_platform_junit_platform_engine_1_8_0_M1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_opentest4j_opentest4j_1_2_0.xml b/.idea/libraries/Maven__org_opentest4j_opentest4j_1_2_0.xml new file mode 100644 index 0000000..fbc1b16 --- /dev/null +++ b/.idea/libraries/Maven__org_opentest4j_opentest4j_1_2_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_testng_testng_7_4_0.xml b/.idea/libraries/Maven__org_testng_testng_7_4_0.xml new file mode 100644 index 0000000..077b7c7 --- /dev/null +++ b/.idea/libraries/Maven__org_testng_testng_7_4_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_webjars_jquery_3_5_1.xml b/.idea/libraries/Maven__org_webjars_jquery_3_5_1.xml new file mode 100644 index 0000000..b1ee78f --- /dev/null +++ b/.idea/libraries/Maven__org_webjars_jquery_3_5_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index ad4fefc..8b9083f 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,6 +3,7 @@ + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index b6d36bd..947b515 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,86 +1,41 @@ + + - + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -212,60 +168,19 @@ + + - + + - - - + - - - - - - + + + + + + + + + + + + + - - - - - - - - + + + + + + + - - - - - - + + - - - - - - - - - - - - - + - - - - - - - - + @@ -494,12 +332,16 @@ - - - - - + + diff --git a/pom.xml b/pom.xml index ffa3f40..dfd86eb 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,26 @@ com.zipcodewilmington singlylinkedlist 1.0-SNAPSHOT + + + org.testng + testng + RELEASE + test + + + org.junit.jupiter + junit-jupiter + RELEASE + test + + + junit + junit + 4.12 + test + + \ No newline at end of file diff --git a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java index 2fb3165..279051a 100644 --- a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java +++ b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java @@ -4,4 +4,88 @@ * Created by leon on 1/10/18. */ public class SinglyLinkedList { + class Node { + Object data; + Node next; + + public Node(Object data) { + this.data = data; + this.next = null; + } + + } + + public Node head = null; + public Node tail = null; + + public void add(Object data) { + Node newNode = new Node(data); + if(head == null) { + head = newNode; + + } else { + tail.next = newNode; + } + tail = newNode; + } + + public void remove(Object data) { + if (head == data || head.next == data) { + head = null; + } else if (tail == data) { + tail = null; + } + } + + public boolean contains(Object data) { + if(head == data || head.next == data || tail == data) { + return true; + } else { + return false; + } + } + + public Integer find(Object data) { + + return null; + } + } + + + + +//>{ + + +// class Node { +// SomeType data; +// Node next; +// +// public Node(SomeType data) { +// this.data = data; +// this.next = null; +// } +// } +// +// public Node head = null; +// public Node tail = null; +// +// public void add(SomeType data) { +// Node newNode = new Node(data); +// if(head == null) { +// head = newNode; +// +// } else { +// tail.next = newNode; +// } +// tail = newNode; +// } +// +// public void remove(SomeType data) { +// Node newNode = new Node(data); +// +// } + + + diff --git a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java index 5cc057e..1ef4eaf 100644 --- a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java +++ b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java @@ -1,7 +1,93 @@ package com.zipcodewilmington.singlylinkedlist; +import org.junit.Assert; +import org.junit.Test; + + /** * Created by leon on 1/10/18. */ public class SinglyLinkedListTest { + + @Test + public void testAdd() { + // Given + SinglyLinkedList singlyLinkedList1 = new SinglyLinkedList(); + + // When + singlyLinkedList1.add(10); + + // Then + Assert.assertEquals(singlyLinkedList1, 10); + } + + @Test + public void testRemove() { + // Given + + // When + + // Then + + } + + @Test + public void testContains() { + // Given + + // When + + // Then + + } + + @Test + public void testFind() { + // Given + + // When + + // Then + + } + + @Test + public void testSize() { + // Given + + // When + + // Then + + } + + @Test + public void testGet() { + // Given + + // When + + // Then + + } + + @Test + public void testCopy() { + // Given + + // When + + // Then + + } + + @Test + public void testSort() { + // Given + + // When + + // Then + + } } From 307b99bd84aec815484249d99d8dd09ceee6dead Mon Sep 17 00:00:00 2001 From: MannyMb Date: Tue, 27 Jul 2021 23:09:24 -0400 Subject: [PATCH 2/5] saving progress --- .idea/workspace.xml | 103 +++++++++++++----- .../singlylinkedlist/SinglyLinkedList.java | 93 ++++++++++++++-- .../SinglyLinkedListTest.java | 55 +++++++--- 3 files changed, 197 insertions(+), 54 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 947b515..5a4a8c7 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,29 +5,7 @@ - - - - - - - - - - - - - - - - - - - - - - @@ -180,7 +158,7 @@ - + - + @@ -245,7 +223,75 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + @@ -342,6 +392,7 @@ + diff --git a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java index 279051a..ac3c438 100644 --- a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java +++ b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java @@ -1,5 +1,7 @@ package com.zipcodewilmington.singlylinkedlist; + + /** * Created by leon on 1/10/18. */ @@ -20,7 +22,7 @@ public Node(Object data) { public void add(Object data) { Node newNode = new Node(data); - if(head == null) { + if (head == null) { head = newNode; } else { @@ -29,30 +31,97 @@ public void add(Object data) { tail = newNode; } - public void remove(Object data) { - if (head == data || head.next == data) { - head = null; - } else if (tail == data) { - tail = null; + public Object remove(Integer index) { // REFACTOR TO RETURN TYPE VOID + SinglyLinkedList list = new SinglyLinkedList(); + Node current = list.head; + Node previous = null; + if (index == 0 && current != null) { + list.head = current.next; + return list; + } + int counter = 0; + while(current != null) { + if(counter == index) { + previous.next = current.next; + break; + } else { + previous = current; + current = current.next; + counter++; + } } + if (current == null) { + return -1; + } + return list; } - public boolean contains(Object data) { - if(head == data || head.next == data || tail == data) { - return true; + + public Boolean contains(Object data) { + Node current = head; + while (current != null) { + if (current.data == data){ + return true; + } + current = current.next; + } + return false; + } + + public Object find(Object data) { // NOT WORKING + Node current = head; + int index = 1; + boolean flag = false; + if (head == null) { + return -1; } else { - return false; + while (current != null) { + if (current.data == data) { + flag = true; + } + index++; + current = current.next; + } + } + if (flag) { + return index; } + return -1; } - public Integer find(Object data) { + public Integer size() { + Integer counter = 0; + Node current = head; + while (current != null) { + counter++; + current = current.next; + } + return counter; + } + + public Object get(Integer index) { + Node current = head; + int counter = 0; + while(current != null) { + if (counter == index) { + return current.data; + } + counter++; + current = current.next; + } + return -1; + } + + public SinglyLinkedList copy() { return null; } -} + public void sort() { + } +} //>{ diff --git a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java index 1ef4eaf..ee807c0 100644 --- a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java +++ b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java @@ -12,62 +12,85 @@ public class SinglyLinkedListTest { @Test public void testAdd() { // Given - SinglyLinkedList singlyLinkedList1 = new SinglyLinkedList(); - + SinglyLinkedList singlyLinkedList = new SinglyLinkedList(); + singlyLinkedList.add(1); + singlyLinkedList.add(2); + Integer expected = 2; // When - singlyLinkedList1.add(10); + Integer actual = singlyLinkedList.size(); // Then - Assert.assertEquals(singlyLinkedList1, 10); + Assert.assertEquals(expected, actual); } @Test public void testRemove() { // Given - + SinglyLinkedList singlyLinkedList = new SinglyLinkedList(); + singlyLinkedList.add(1); + singlyLinkedList.add(2); // When - + singlyLinkedList.remove(2); + Integer expectedSize = 1; + Integer actualSize = singlyLinkedList.size(); // Then - + Assert.assertEquals(expectedSize,actualSize); } @Test public void testContains() { // Given - + SinglyLinkedList singlyLinkedList = new SinglyLinkedList(); + singlyLinkedList.add("one"); + singlyLinkedList.add("two"); + Boolean expected = true; // When - + Boolean actual = singlyLinkedList.contains("one"); // Then - + Assert.assertEquals(expected, actual); } @Test public void testFind() { // Given - + SinglyLinkedList singlyLinkedList = new SinglyLinkedList(); + singlyLinkedList.add("one"); + singlyLinkedList.add("two"); + singlyLinkedList.add("three"); + Integer expectedIndex = 1; // When - + Object actualIndex = singlyLinkedList.find("three"); // Then - + Assert.assertEquals(expectedIndex,actualIndex); } @Test public void testSize() { // Given - + SinglyLinkedList singlyLinkedList = new SinglyLinkedList(); + singlyLinkedList.add(1); + singlyLinkedList.add(2); // When - + Integer expectedSize = 2; + Integer actualSize = singlyLinkedList.size(); // Then - + Assert.assertEquals(expectedSize,actualSize); } @Test public void testGet() { // Given + SinglyLinkedList singlyLinkedList = new SinglyLinkedList(); + singlyLinkedList.add(1); + singlyLinkedList.add(2); + singlyLinkedList.add(3); // When + Integer expectedElement = 2; + Integer actualElement = (Integer) singlyLinkedList.get(1); // Then + Assert.assertEquals(expectedElement, actualElement); } From cde400f5f850eeb4ffb34ad8f463fb3ddbd03835 Mon Sep 17 00:00:00 2001 From: MannyMb Date: Tue, 27 Jul 2021 23:53:27 -0400 Subject: [PATCH 3/5] saving progress --- .idea/workspace.xml | 9 +- .../singlylinkedlist/SinglyLinkedList.java | 83 +++++++++++-------- .../SinglyLinkedListTest.java | 12 +-- 3 files changed, 58 insertions(+), 46 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 5a4a8c7..8a8233c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -213,7 +213,7 @@ diff --git a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java index ac3c438..e4e40a8 100644 --- a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java +++ b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java @@ -19,6 +19,7 @@ public Node(Object data) { public Node head = null; public Node tail = null; + public int numOfElements; public void add(Object data) { Node newNode = new Node(data); @@ -31,30 +32,51 @@ public void add(Object data) { tail = newNode; } - public Object remove(Integer index) { // REFACTOR TO RETURN TYPE VOID - SinglyLinkedList list = new SinglyLinkedList(); - Node current = list.head; - Node previous = null; - if (index == 0 && current != null) { - list.head = current.next; - return list; - } - int counter = 0; - while(current != null) { - if(counter == index) { - previous.next = current.next; - break; - } else { - previous = current; - current = current.next; - counter++; - } - } - if (current == null) { - return -1; - } - return list; + public void remove(Integer index) { // REFACTOR TO RETURN TYPE VOID + } +// Node current = head; +// +// if (index == 0) { +// head = current.next; +// return; +// } +// int tracker = 0; +// Node previousNode = head; +// current = head.next; +// while (current != null) { +// if(tracker == index) { +// previousNode.next = current.next; +// return; +// } +// tracker++; +// previousNode = current; +// current = current.next; +// } +// } +// SinglyLinkedList list = new SinglyLinkedList(); +// Node current = list.head; +// Node previous = null; +// if (index == 0 && current != null) { +// list.head = current.next; +// return (Integer) list.get(index); +// } +// int counter = 0; +// while(current != null) { +// if(counter == index) { +// previous.next = current.next; +// break; +// } else { +// previous = current; +// current = current.next; +// counter++; +// } +// } +// if (current == null) { +// return null; +// } +// return (Integer) list.get(index); +// } public Boolean contains(Object data) { @@ -68,27 +90,18 @@ public Boolean contains(Object data) { return false; } - public Object find(Object data) { // NOT WORKING + public Object find(Object data) { Node current = head; int index = 1; - boolean flag = false; - if (head == null) { - return -1; - } else { while (current != null) { if (current.data == data) { - flag = true; + return index - 1; } index++; current = current.next; } + return -1; } - if (flag) { - return index; - } - return -1; - } - public Integer size() { Integer counter = 0; diff --git a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java index ee807c0..9617825 100644 --- a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java +++ b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java @@ -30,7 +30,7 @@ public void testRemove() { singlyLinkedList.add(1); singlyLinkedList.add(2); // When - singlyLinkedList.remove(2); + singlyLinkedList.remove(1); Integer expectedSize = 1; Integer actualSize = singlyLinkedList.size(); // Then @@ -54,12 +54,12 @@ public void testContains() { public void testFind() { // Given SinglyLinkedList singlyLinkedList = new SinglyLinkedList(); - singlyLinkedList.add("one"); - singlyLinkedList.add("two"); - singlyLinkedList.add("three"); - Integer expectedIndex = 1; + singlyLinkedList.add(1); + singlyLinkedList.add(2); + singlyLinkedList.add(3); + Integer expectedIndex = 2; // When - Object actualIndex = singlyLinkedList.find("three"); + Object actualIndex = singlyLinkedList.find(3); // Then Assert.assertEquals(expectedIndex,actualIndex); } From 1664eaaeb9868594ef8b0da6ecd2a63523bb302f Mon Sep 17 00:00:00 2001 From: MannyMb Date: Wed, 28 Jul 2021 19:39:49 -0400 Subject: [PATCH 4/5] still working --- .idea/workspace.xml | 1 - .../singlylinkedlist/SinglyLinkedList.java | 25 ++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 8a8233c..89f35df 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -7,7 +7,6 @@ -