From 6f150fa8d45d8c9d59a48e33e801622a72b02eff Mon Sep 17 00:00:00 2001 From: junior Date: Thu, 25 Nov 2021 16:39:00 -0500 Subject: [PATCH 1/2] added add method, size method, find method and contains method --- .idea/compiler.xml | 2 + .idea/jarRepositories.xml | 20 + .idea/modules.xml | 1 + .idea/workspace.xml | 351 +++++------------- .../singlylinkedlist/MainApplication.java | 13 + .../singlylinkedlist/SinglyLinkedList.java | 72 +++- 6 files changed, 198 insertions(+), 261 deletions(-) create mode 100644 .idea/jarRepositories.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/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..54cbb4b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,86 +1,22 @@ + + - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -212,60 +149,19 @@ + + - + + - - - + - - - - - - - - - - - + @@ -494,12 +310,27 @@ - - - - - - - - file://$PROJECT_DIR$/src/main/java/com/zipcodewilmington/singlylinkedlist/MainApplication.java - 14 - - - + diff --git a/pom.xml b/pom.xml index ffa3f40..7b92ec4 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,14 @@ com.zipcodewilmington singlylinkedlist 1.0-SNAPSHOT + + + junit + junit + RELEASE + test + + \ No newline at end of file diff --git a/src/main/java/com/zipcodewilmington/singlylinkedlist/MainApplication.java b/src/main/java/com/zipcodewilmington/singlylinkedlist/MainApplication.java index 153ee72..2d37b2b 100644 --- a/src/main/java/com/zipcodewilmington/singlylinkedlist/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/singlylinkedlist/MainApplication.java @@ -1,5 +1,7 @@ package com.zipcodewilmington.singlylinkedlist; +import java.util.LinkedList; + /** * Created by leon on 1/10/18. */ @@ -10,11 +12,28 @@ public static void main(String[] args) { singlyLinkedList.add("1"); singlyLinkedList.add("2"); singlyLinkedList.add("3"); + singlyLinkedList.add("4"); + // singlyLinkedList.remove(3); + + + SinglyLinkedList singlyLinkedList1 = singlyLinkedList.copy(); + singlyLinkedList.add("6"); + Boolean result = singlyLinkedList.contains("3"); - Boolean result = singlyLinkedList.contains("4"); System.out.println(result); System.out.println(singlyLinkedList.find("1")); + singlyLinkedList.display(); + singlyLinkedList1.display(); + + + LinkedList list = new LinkedList(); + list.clone(); + list.add("1"); + list.add("2"); + list.add("3"); + + // System.out.println(list); } } diff --git a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java index f98253f..37bf85d 100644 --- a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java +++ b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java @@ -61,6 +61,18 @@ public Integer find(T data) { return -1; } + public Node get(Integer index){ + + Node current = head; + Integer currentIndex = 0; + + while(currentIndex != index){ + current = current.next; + currentIndex++; + } + return current; + } + public Boolean contains(T data) { Node current = head; @@ -74,4 +86,57 @@ public Boolean contains(T data) { } + + public void remove(Integer index) { + Node current = this.head; + Node previous = null; + + if (index == 0 && current != null) { + this.head = current.next; + } + int counter = 0; + while (current != null) { + if (counter == index) { + previous.next = current.next; + break; + } else { + previous = current; + current = current.next; + counter++; + } + } + } + + public SinglyLinkedList copy() { + SinglyLinkedList newList = new SinglyLinkedList(); + Node current = head; + + while(current != null) { + newList.add((T) current.data); + current = current.next; + } + return newList; + } + + + + + + + public void display() { + + Node current = head; + if(head == null) { + System.out.println("List is empty"); + return; + } + while(current != null) { + + System.out.print(current.data + " "); + current = current.next; + } + System.out.println(); + } + + } diff --git a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java index 5cc057e..c782f2f 100644 --- a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java +++ b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java @@ -1,7 +1,121 @@ 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 addTest(){ + //given + SinglyLinkedList list = new SinglyLinkedList(); + Integer expectedSize = 3; + //when + list.add("a"); + list.add("b"); + list.add("c"); + Integer actual = list.size(); + //then + Assert.assertEquals(expectedSize, actual); + } + + + @Test + public void removeTest(){ + //given + SinglyLinkedList list = new SinglyLinkedList(); + Integer expectedSize = 2; + //when + list.add("a"); + list.add("b"); + list.add("c"); + list.remove(2); + Integer actual = list.size(); + //then + Assert.assertEquals(expectedSize, actual); + + } + @Test + public void sizeTest(){ + //given + SinglyLinkedList list = new SinglyLinkedList(); + Integer expectedSize = 6; + //when + list.add("a"); + list.add("b"); + list.add("c"); + list.add("d"); + list.add("e"); + list.add("f"); + Integer actual = list.size(); + //then + Assert.assertEquals(expectedSize, actual); + + } + + @Test + public void containsTest(){ + //given + SinglyLinkedList list = new SinglyLinkedList(); + + //when + list.add("a"); + list.add("b"); + list.add("c"); + Boolean actual = list.contains("b"); + //then + Assert.assertTrue(actual); + + } + + @Test + public void findTest(){ + //given + SinglyLinkedList list = new SinglyLinkedList(); + Integer expectedIndex = 2; + //when + list.add("a"); + list.add("b"); + list.add("c"); + Integer actual = list.find("c"); + //then + Assert.assertEquals(expectedIndex, actual); + + } + + @Test + public void getTest(){ + //given + SinglyLinkedList list = new SinglyLinkedList(); + String expected = "c"; + //when + list.add("a"); + list.add("b"); + list.add("c"); + + String actual = list.get(2).data; + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void copyTest(){ + //given + SinglyLinkedList list = new SinglyLinkedList(); + Integer expectedSize = 3; + //when + list.add("a"); + list.add("b"); + list.add("c"); + SinglyLinkedList list2 = list.copy(); + list.add("d"); //added after copy to test if it would be added to copy + Integer actual = list2.size(); + //then + Assert.assertEquals(expectedSize, actual); + + } + }