diff --git a/README.md b/README.md
index 95fe85b..d4aee93 100644
--- a/README.md
+++ b/README.md
@@ -18,7 +18,7 @@ Implement a singly linked list:
- contains -- returns true if the element is in the list, false otherwise
- find -- returns the element's index if it is in the list, -1 otherwise
- size -- returns the current size of the list
- - get -- returns the element at the specified index
+ - get -- returns the element at the specified index````
- copy -- returns a new linked list containing the same values (look up deep versus shallow copy)
- sort -- sorts the list using your algorithm of choice. You must perform the sorting yourself (no fair using someone else's library)
diff --git a/pom.xml b/pom.xml
index ffa3f40..7b92ec4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -7,6 +7,14 @@
com.zipcodewilmingtonsinglylinkedlist1.0-SNAPSHOT
+
+
+ junit
+ junit
+ RELEASE
+ 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..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);
+
+ }
}