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/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index b6d36bd..a59655b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,86 +1,20 @@ + + - + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ 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..b6ea8d6 100644 --- a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java +++ b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java @@ -1,7 +1,133 @@ package com.zipcodewilmington.singlylinkedlist; +import com.sun.org.apache.xpath.internal.operations.Bool; +import sun.plugin.javascript.navig.Link; + +import java.util.LinkedList; + /** * Created by leon on 1/10/18. */ public class SinglyLinkedList { + + static class Node { + + Object data; + Node next; + + Node(Object data) { + this.data = data; + this.next = null; + } + } + + public Node head = null; + public Node tail = null; + + public void add(Object data) { + + // Creating a node with given data + Node newNode = new Node(data); + + // If list is empty then make new node as head + if (this.head == null) { + this.head = newNode; + } else { + this.tail.next = newNode; + } + this.tail = newNode; + } + + public void remove(int index) { + Node current = this.head; + Node previous = null; + + if (current == null) { + throw new UnsupportedOperationException("Ain't nothing in the list!"); + } + int trackIndex = 0; + while (current != null) { + if (trackIndex == index) { + previous.next = current.next; + break; + } else { + previous = current; + current = current.next; + trackIndex++; + } + } + } + + public Boolean contains(Object key) { + Node current = this.head; + while (current != null) { + if(current.data == key) { + return true; + } + current = current.next; + } + return false; + } + + public Integer find(Object 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 int size() { + int length = 0; + Node current = this.head; + + while (current != null) { + length++; + current = current.next; + } + return length; + } + + public Object get(int index) { + Node current = this.head; + int search = 0; + while (current != null) { + if (search == index) { + return current.data; + } + search++; + current = current.next; + } + return -1; + } + + public SinglyLinkedList copy() { + SinglyLinkedList singlyLinkedList = new SinglyLinkedList(); + Node current = this.head; + while (current != null) { + singlyLinkedList.add(current.data); + current = current.next; + } + return singlyLinkedList; + } + + 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) { + 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..d05a0eb 100644 --- a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java +++ b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java @@ -1,7 +1,161 @@ 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(); + + // When + list.add(1); + list.add(2); + list.add(3); + + int actual = list.size(); + int expected = 3; + + + // 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.remove(2); + int expected = 2; + 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); + + + 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 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); + + + int expected = 4; + int actual =list.size(); + + 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); + + } }