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_12.xml b/.idea/libraries/Maven__junit_junit_4_12.xml
new file mode 100644
index 0000000..d411041
--- /dev/null
+++ b/.idea/libraries/Maven__junit_junit_4_12.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..d642e3c 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -1,86 +1,19 @@
+
+
+
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -212,60 +146,19 @@
+
+
+
+
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -301,157 +194,151 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
@@ -494,12 +381,17 @@
-
-
-
-
-
-
+
+
+
+
+
diff --git a/pom.xml b/pom.xml
index ffa3f40..6cb9726 100644
--- a/pom.xml
+++ b/pom.xml
@@ -7,6 +7,20 @@
com.zipcodewilmington
singlylinkedlist
1.0-SNAPSHOT
+
+
+ junit
+ junit
+ 4.12
+ test
+
+
+ junit
+ junit
+ 4.12
+ 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..5645037 100644
--- a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java
+++ b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java
@@ -3,5 +3,148 @@
/**
* Created by leon on 1/10/18.
*/
-public class SinglyLinkedList {
+public class 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 newNode = new Node(data);
+ if(head == null) {
+ head = newNode;
+ } else {
+ tail.next = newNode;
+ }
+ tail = newNode;
+ }
+
+ public int size() {
+ Node temp = head;
+ int count = 0;
+ while(temp != null) {
+ count++;
+ temp = temp.next;
+ }
+ return count;
+ }
+
+ public Integer find(T data) {
+ Node temp = head;
+ for(int i = 0; i < size(); i++) {
+ if(!temp.data.equals(data)) {
+ temp = temp.next;
+ } else return i;
+ }
+ return -1;
+ }
+
+ 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 = head;
+ Node last = head;
+ for (int i = 0; i < size(); i++) {
+ if(i != index) {
+ last = current;
+ current = current.next;
+ } else {
+ while(i < size() - 1) {
+ last.next = current.next;
+ }
+ }
+ }
+ }
+
+ public T get(Integer index) {
+ Node temp = head;
+ for(int i = 0; i < size(); i++) {
+ if(i == index) {
+ return temp.data;
+ } else temp = temp.next;
+ }
+ return null;
+ }
+
+
+ public SinglyLinkedList copy() {
+ SinglyLinkedList newList = new SinglyLinkedList();
+ Node temp = head;
+ while(temp != null) {
+ newList.add(temp.data);
+ temp = temp.next;
+ }
+ return newList;
+ }
+
+ public void sort() {
+ Node current = head;
+
+ for(int i = 0; i < size(); i++) {
+ current = head;
+ while(current.next != null) {
+ if(current.data.compareTo(current.next.data) > 0) {
+ T temp = current.next.data;
+ current.next.data = current.data;
+ current.data = temp;
+ }
+ current = current.next;
+ }
+ }
+ }
+
+ public void reverse() {
+ Node original = head;
+ Node previous = null;
+ Node current = null;
+
+ while(original != null) {
+ current = original;
+ original = original.next;
+
+ current.next = previous;
+ previous = current;
+ head = current;
+ }
+ }
+
+ public SinglyLinkedList slice(Integer startIndex, Integer endIndex) {
+ SinglyLinkedList slicedList = new SinglyLinkedList();
+
+ for(int i = 0; i <= endIndex; i++) {
+ if(i >= startIndex) {
+ slicedList.add(get(i));
+ }
+ }
+ return slicedList;
+ }
+
+ public void display() {
+ Node current = head;
+ //checkListEmpty("There's no list, idiot.");
+ System.out.println("Nodes of singly linked list:");
+ while (current != null) {
+ System.out.println(current.data + "");
+ 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..9c1316f 100644
--- a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java
+++ b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java
@@ -1,7 +1,185 @@
package com.zipcodewilmington.singlylinkedlist;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+
/**
* Created by leon on 1/10/18.
*/
public class SinglyLinkedListTest {
+
+ @Test
+ public void testAdd() {
+ //given
+ SinglyLinkedList list = new SinglyLinkedList();
+ Integer expected = 3;
+ //when
+ list.add(3);
+ list.add(5);
+ list.add(7);
+ Integer actual = list.size();
+ //then
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testSize() {
+ //given
+ SinglyLinkedList list = new SinglyLinkedList();
+ Integer expected = 3;
+ //when
+ list.add(3);
+ list.add(5);
+ list.add(7);
+ Integer actual = list.size();
+ //then
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testFind() {
+ //given
+ SinglyLinkedList list = new SinglyLinkedList();
+ Integer expected = 1;
+ //when
+ list.add("wine");
+ list.add("coffee");
+ list.add("tea");
+ Integer actual = list.find("coffee");
+ //then
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testContains() {
+ //given
+ SinglyLinkedList list = new SinglyLinkedList();
+ //when
+ list.add("wine");
+ list.add("coffee");
+ list.add("tea");
+ //then
+ Assert.assertTrue(list.contains("tea"));
+ }
+
+ @Test
+ public void testRemove() {
+ //given
+ SinglyLinkedList list = new SinglyLinkedList();
+ Integer expected = 3;
+ //when
+ list.add(3);
+ list.add(5);
+ list.add(7);
+ list.add(2);
+ list.remove(2);
+ Integer actual = list.size();
+ //then
+ Assert.assertTrue(list.contains(5));
+ Assert.assertFalse(list.contains(7));
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testGet() {
+ //given
+ SinglyLinkedList list = new SinglyLinkedList();
+ String expected = "wine";
+ //when
+ list.add("wine");
+ list.add("coffee");
+ list.add("tea");
+ String actual = list.get(0);
+ //then
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testCopy() {
+ //given
+ SinglyLinkedList list = new SinglyLinkedList();
+ Integer expected = 2;
+ //when
+ list.add(3);
+ list.add(5);
+ list.add(7);
+ list.add(2);
+ SinglyLinkedList newList = list.copy();
+ Integer actual = newList.get(3);
+ //then
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testSort() {
+ //given
+ SinglyLinkedList list = new SinglyLinkedList();
+ Integer expected0 = 1;
+ Integer expected1 = 5;
+ Integer expected2 = 8;
+ Integer expected3 = 13;
+ //when
+ list.add(8);
+ list.add(1);
+ list.add(13);
+ list.add(5);
+ list.sort();
+ Integer actual0 = list.get(0);
+ Integer actual1 = list.get(1);
+ Integer actual2 = list.get(2);
+ Integer actual3 = list.get(3);
+ //then
+ Assert.assertEquals(expected0, actual0);
+ Assert.assertEquals(expected1, actual1);
+ Assert.assertEquals(expected2, actual2);
+ Assert.assertEquals(expected3, actual3);
+ }
+
+ @Test
+ public void testReverse() {
+ //given
+ SinglyLinkedList list = new SinglyLinkedList();
+ Integer expected0 = 1;
+ Integer expected1 = 5;
+ Integer expected2 = 8;
+ Integer expected3 = 13;
+ //when
+ list.add(13);
+ list.add(8);
+ list.add(5);
+ list.add(1);
+ list.reverse();
+ Integer actual0 = list.get(0);
+ Integer actual1 = list.get(1);
+ Integer actual2 = list.get(2);
+ Integer actual3 = list.get(3);
+ //then
+ list.display();
+ Assert.assertEquals(expected0, actual0);
+ Assert.assertEquals(expected1, actual1);
+ Assert.assertEquals(expected2, actual2);
+ Assert.assertEquals(expected3, actual3);
+ }
+
+ @Test
+ public void testSlice() {
+ //given
+ SinglyLinkedList list = new SinglyLinkedList();
+ Integer expected = 4;
+ //when
+ list.add(13);
+ list.add(8);
+ list.add(5);
+ list.add(1);
+ list.add(12);
+ list.add(9);
+ SinglyLinkedList slicedList = list.slice(1, 4);
+ Integer actual = slicedList.size();
+ //then
+ slicedList.display();
+ Assert.assertEquals(expected, actual);
+ }
}