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_1.xml b/.idea/libraries/Maven__junit_junit_4_13_1.xml new file mode 100644 index 0000000..9fa24fc --- /dev/null +++ b/.idea/libraries/Maven__junit_junit_4_13_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/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..1ff988b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,86 +1,25 @@ + + - + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -212,60 +152,19 @@ + + - + + + - - - - - - - - - - + + + + + + + + + + + + + + - - - - - - - - + + + + + + + - - - - - - + @@ -494,12 +388,16 @@ - - - - - + + diff --git a/pom.xml b/pom.xml index ffa3f40..87a2afa 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,14 @@ com.zipcodewilmington singlylinkedlist 1.0-SNAPSHOT + + + junit + junit + 4.13.1 + 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..0d1a553 100644 --- a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java +++ b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java @@ -1,7 +1,127 @@ package com.zipcodewilmington.singlylinkedlist; +import com.sun.org.apache.xpath.internal.operations.Bool; + /** * Created by leon on 1/10/18. */ public class SinglyLinkedList { + + class Node { + Object data; + Node next; + + public Node(Object data){ + this.data = data; + next = null; + } + } + + public Node head; + public Node tail; + + public void add(Object data){ + Node newNode = new Node(data); + if(this.head == null){ + this.head = newNode; + } else { + tail.next = newNode; + } + tail = newNode; + } + + public void remove(int 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; //[previous] -> [current.next] + break; + } else { + previous = current; + current = current.next; + counter++; + } + } + if (current == null){ + System.out.println("not found"); + } + } + + public Boolean contains(Object key){ + Node current = this.head; + while (current != null) { + if (current.data == key){ + return true; + } + current = current.next; + } + return false; + } + + public int size(){ + int counter = 0; + Node current = head; + while (current != null) { + counter++; + current = current.next; + } + return counter; + } + + public Integer find(int data){ + Node current = this.head; + int index = 0; + while(current != null){ + if(current.data.equals(data)){ + return index; + } + index++; + current = current.next; + } + return -1; + } + + public Object get(int index){ + Node current = this.head; + int seek = 0; + while(current != null){ + if(seek == index){ + return current.data; + } + seek++; + current = current.next; + } + return -1; + } + + public SinglyLinkedList copy(){ + SinglyLinkedList copiedList = new SinglyLinkedList(); + Node current = this.head; + while(current != null){ + copiedList.add(current.data); + current = current.next; + } + return copiedList; + } + + public void sort(){ + Node current = this.head; + for(int i =0; i < size(); i++){ + while(current.next != null){ + Node next = current.next; + if((Integer)current.data > (Integer) next.data){ // [head] -> [current] -> current.next] -> [tail] -> [null] + Object temp = current.data; + current.data = next.data; + next.data = temp; + } + current = current.next; + } + } + } } diff --git a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java index 5cc057e..cd9267b 100644 --- a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java +++ b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java @@ -1,7 +1,167 @@ package com.zipcodewilmington.singlylinkedlist; +import com.sun.org.apache.xpath.internal.operations.Bool; +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(); + + // When + list.add(1); + list.add(2); + list.add(3); + list.add(4); + + int actual = list.size(); + int expected = 4; + + + // Then + Assert.assertEquals(expected, actual); + } + + @Test + public void removeTest(){ + // Given + SinglyLinkedList list = new SinglyLinkedList(); + + // When + list.add(1); + list.add(2); + list.add(3); + list.add(4); + + list.remove(3); + int expected = 3; + int actual = list.size(); + + Assert.assertEquals(expected, actual); + + } + + @Test + public void containsTest(){ + // Given + SinglyLinkedList list = new SinglyLinkedList(); + + // When + list.add(1); + list.add(2); + list.add(3); + list.add(4); + + Boolean actual = list.contains(3); + + Assert.assertTrue(actual); + Assert.assertFalse(list.contains(6)); + } + + @Test + public void sizeTest(){ + // Given + SinglyLinkedList list = new SinglyLinkedList(); + + // When + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + list.add(6); + + int expected = 6; + int actual =list.size(); + + Assert.assertEquals(expected,actual); + } + + @Test + public void findTest(){ + // Given + SinglyLinkedList list = new SinglyLinkedList(); + + // When + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + list.add(6); + + Integer actual = list.find(4); + Integer expected = 3; + + Assert.assertEquals(expected, actual); + } + + @Test + public void getTest(){ + // Given + SinglyLinkedList list = new SinglyLinkedList(); + + // When + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + list.add(6); + + Integer actual = (Integer) list.get(4); + Integer expected = 5; + + Assert.assertEquals(expected, actual); + } + + @Test + public void copyTest(){ + // Given + SinglyLinkedList list = new SinglyLinkedList(); + + // When + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + list.add(6); + + SinglyLinkedList newList = list.copy(); + Integer expected = list.size(); + Integer actual = newList.size(); + + Assert.assertEquals(expected, actual); + + } + + @Test + public void sortTest(){ + // Given + SinglyLinkedList list = new SinglyLinkedList(); + + // When + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + list.add(9); + list.add(7); + list.add(6); + + list.sort(); + + Assert.assertTrue(list.find(1) == 0); + Assert.assertTrue(list.find(9) == 7); + + } + }