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..417ceb5 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -1,86 +1,37 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index ffa3f40..6bbb3a4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -7,6 +7,14 @@
com.zipcodewilmington
singlylinkedlist
1.0-SNAPSHOT
+
+
+ junit
+ junit
+ 4.12
+ 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..7f1ff5d 100644
--- a/src/main/java/com/zipcodewilmington/singlylinkedlist/MainApplication.java
+++ b/src/main/java/com/zipcodewilmington/singlylinkedlist/MainApplication.java
@@ -4,4 +4,24 @@
* Created by leon on 1/10/18.
*/
public class MainApplication {
+ public static void main(String[] args) {
+ SinglyLinkedList doesThisWork = new SinglyLinkedList();
+ doesThisWork.addNode(10);
+ doesThisWork.addNode(9);
+ doesThisWork.addNode(2);
+ doesThisWork.addNode(146);
+ doesThisWork.addNode(1245);
+ doesThisWork.addNode(56);
+ doesThisWork.addNode(4);
+ doesThisWork.addNode(5);
+ doesThisWork.addNode(90);
+ doesThisWork.addNode(1);
+
+ doesThisWork.display();
+ System.out.println("\n" + "\n");
+
+// doesThisWork.sortLeastToGreatest();
+// doesThisWork.reverse().display();
+ doesThisWork.sortGreatestToLeast();
+ }
}
diff --git a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java
index 2fb3165..39f88fa 100644
--- a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java
+++ b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java
@@ -1,7 +1,183 @@
package com.zipcodewilmington.singlylinkedlist;
+
+import sun.jvm.hotspot.StackTrace;
+
/**
* Created by leon on 1/10/18.
*/
+
public class SinglyLinkedList {
+
+ class Node{
+ Object data;
+ Node next;
+
+ public Node(Object data) {
+ this.data = data;
+ this.next = null;
+ }
+ }
+
+ public Node head = null;
+ public Node tail = null;
+
+ public void addNode(Object data) {
+ Node newNode = new Node(data);
+
+ if(head == null) {
+ head = newNode;
+ } else {
+ tail.next = newNode;
+ }
+ tail = newNode;
+ }
+
+ public int size() {
+ int count = 0;
+ Node current = head;
+
+ while(current != null) {
+ count++;
+ current = current.next;
+ }
+ return count;
+ }
+
+ 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;
+ }
+ }
+
+ // returning new SinglyLinkedList - there is a way to stitch two nodes together
+ public SinglyLinkedList remove(Object data) {
+ SinglyLinkedList result = new SinglyLinkedList();
+ Node current = head;
+ checkListEmpty("There's no list, dummy");
+ while(current != null) {
+ if(current.data != data) {
+ result.addNode(current.data);
+ }
+ current = current.next;
+ }
+ return result;
+ }
+
+ private void checkListEmpty(String s) {
+ if (head == null) {
+ throw new UnsupportedOperationException(s);
+ }
+ }
+
+ public Boolean contains(Object data) {
+ Node current = head;
+ checkListEmpty("This list empty, doood");
+ while(current != null) {
+ if(current.data == data) {
+ return true;
+ }
+ current = current.next;
+ }
+// if(find(data).equals(data)) {
+// return true;
+// }
+ return false;
+ }
+
+ public int find(Object data) {
+ Node current = head;
+ int theIndex = 0;
+ if (head == null) {
+ System.out.println("List is empty");
+ }
+ while(current != null) {
+ if(current.data == data) {
+ return theIndex;
+ }
+ theIndex++;
+ current = current.next;
+ }
+ return -1;
+ }
+
+ public Object get(double index) {
+ Node current = head;
+ int theIndex = 0;
+ if (head == null || index > size() || index < 0) {
+ throw new UnsupportedOperationException("You doin' something wrong, buckaroo");
+ }
+ while(current != null) {
+ if (theIndex == index) {
+ return current.data;
+ }
+ theIndex++;
+ current = current.next;
+ }
+ return -1;
+ }
+
+ public SinglyLinkedList copy() {
+ SinglyLinkedList newList = new SinglyLinkedList();
+
+ Node current = head;
+ checkListEmpty("List don't be there, my guy");
+ while (current != null) {
+ newList.addNode(current.data);
+ current = current.next;
+ }
+ return newList;
+ }
+
+
+ public void sortLeastToGreatest() {
+ Node current = head;
+ checkListEmpty("Where's the car fax?");
+ for (int i = 0; i < size(); i++) {
+ current = head;
+ 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;
+ }
+ }
+ }
+ public SinglyLinkedList reverse () {
+ SinglyLinkedList resultList = new SinglyLinkedList();
+ Integer i = size() - 1;
+ checkListEmpty("You ain't got one of them lists, buckaroo");
+ while(i >= 0) {
+ resultList.addNode(get(i));
+ i--;
+ }
+ return resultList;
+ }
+
+ public SinglyLinkedList splice (Integer beginningIndex, Integer endingIndex) {
+ Node current = head;
+ Integer index = 0;
+ SinglyLinkedList result = new SinglyLinkedList();
+ checkListEmpty("Where's the car fax?");
+ while(current != null) {
+ if (index >= beginningIndex && index <= endingIndex) {
+ result.addNode(current.data);
+ }
+ index++;
+ current = current.next;
+ }
+ return result;
+ }
+
+ public void sortGreatestToLeast () {
+ sortLeastToGreatest();
+ reverse().display();
+ }
}
diff --git a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java
index 5cc057e..f1fc37c 100644
--- a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java
+++ b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java
@@ -1,7 +1,57 @@
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 addNodeTest () {
+ SinglyLinkedList single = new SinglyLinkedList();
+ Integer expected = 3;
+
+ single.addNode(3);
+ single.addNode(2);
+ single.addNode(5);
+ Integer actual = single.size();
+
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void sizeTest () {
+ SinglyLinkedList single = new SinglyLinkedList();
+ Integer expected = 0;
+ Integer actual = single.size();
+
+ Assert.assertEquals(expected, actual);
+ }
+
+ @Test
+ public void displayTest () {
+ SinglyLinkedList single = new SinglyLinkedList();
+ single.addNode(3);
+ single.addNode(2);
+ single.addNode(5);
+
+ // Visual test - display returns void
+ single.display();
+ }
+
+ @Test
+ public void removeTest() {
+ SinglyLinkedList test = new SinglyLinkedList();
+ test.addNode(3);
+ test.addNode(6);
+ test.addNode(4);
+ test.addNode(1);
+ test.addNode(8);
+
+ SinglyLinkedList actual = test.remove(4);
+
+ Assert.assertEquals(test.size() - 1, actual.size());
+ }
}