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__junit_junit_4_13_2.xml b/.idea/libraries/Maven__junit_junit_4_13_2.xml new file mode 100644 index 0000000..606c352 --- /dev/null +++ b/.idea/libraries/Maven__junit_junit_4_13_2.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/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..d2c0ceb 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,86 +1,23 @@ + + - + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -212,60 +150,19 @@ + + - + + + - - - - - - - - - - + + + + + + + + + + + + + + - - - - - - - - + + + + + + + - - - - - - + @@ -494,12 +386,17 @@ - - - - - + + + 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 b8cd84f..2d37b2b 100644 --- a/src/main/java/com/zipcodewilmington/singlylinkedlist/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/singlylinkedlist/MainApplication.java @@ -1,7 +1,39 @@ package com.zipcodewilmington.singlylinkedlist; +import java.util.LinkedList; + /** * Created by leon on 1/10/18. */ public class MainApplication { + + public static void main(String[] args) { + SinglyLinkedList singlyLinkedList = new SinglyLinkedList(); + 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"); + + 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 2fb3165..37bf85d 100644 --- a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java +++ b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java @@ -1,7 +1,142 @@ package com.zipcodewilmington.singlylinkedlist; + +import jdk.nashorn.internal.ir.WhileNode; + /** * Created by leon on 1/10/18. */ -public class SinglyLinkedList { +public class SinglyLinkedList> { + + public SinglyLinkedList() { + } + + class Node { + T data; + Node next; + + public Node(T data) { + this.data = data; + this.next = null; + } + } + + public Node head = null; + public Node tail = null; + + + public void add(T data) { + Node node = new Node(data); + if (head == null) { + head = node; + } else { + tail.next = node; + } + tail = node; + } + + + public Integer size() { + Node current = head; + Integer counter = 0; + while (current != null) { + counter++; + current = current.next; + + } + return counter; + } + + public Integer find(T data) { + Node current = head; + Integer index = 0; + + while (current != null) { + if (current.data == data) { + return index; + } + index++; + current = current.next; + } + 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; + + while (current != null) { + if (current.data == data) { + return true; + } + current = current.next; + } + return false; + } + + + + 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); + + } + }