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..f43fccc 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,86 +1,19 @@ + + - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -212,60 +146,19 @@ + + - + + + - - - - - - - - - - + + + + + + + + + + + + + + - - - - - - - - + + + + + + + - - - - - - + @@ -494,12 +382,17 @@ - - - - - + + + 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/MainApplication.java b/src/main/java/com/zipcodewilmington/singlylinkedlist/MainApplication.java index b8cd84f..095dd2e 100644 --- a/src/main/java/com/zipcodewilmington/singlylinkedlist/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/singlylinkedlist/MainApplication.java @@ -4,4 +4,9 @@ * Created by leon on 1/10/18. */ public class MainApplication { + + public static void main(String[] args) { + SinglyLinkedList string = new SinglyLinkedList(); + string.slice(3,5); + } } diff --git a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java index 2fb3165..454ada3 100644 --- a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java +++ b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java @@ -1,7 +1,236 @@ package com.zipcodewilmington.singlylinkedlist; +import javax.xml.soap.Node; +import java.util.List; + /** * Created by leon on 1/10/18. */ public class SinglyLinkedList { + + class Node { + String data; + Node next; + + public Node(String data) { + this.data = data; + this.next = null; + } + } + + public Node head = null; + public Node tail = null; + + public void addNode(String data){ + Node newNode = new Node (data); + if (head == null){ + head = newNode; + } else { + tail.next =newNode; + } + tail = newNode; + } + + public int size() { + int length = 0; + Node current = head; + + while (current != null){ + length++; + current = current.next; + } + return length; + } + + public void remove(int index){ + Node current = head; + if (current == null) { + throw new UnsupportedOperationException("List is uhhh duhh empty"); + } + int trackIndex = 0; + while (current != null){ + if (trackIndex == index){ + current.data = null; + } + current = current.next; + } + + } + + public SinglyLinkedList remove(String data) { + SinglyLinkedList list = new SinglyLinkedList(); + Node current = head; + if (current == null) { + throw new UnsupportedOperationException("No list here, smart guy"); + } + + while (current != null) { + // current = current.next; + if (current.data != data) { + list.addNode(current.data); + //current.data = null; + } + + current = current.next; + } + return list; + + } + + + public Boolean contains(String data){ + Node current = head; + if (head == null){ + throw new UnsupportedOperationException("No list again fool!"); + } + while(current != null){ + if (current.data == data){ + return true; + } + current = current.next; + } + return false; + } + + + public int find (String data) { + Node current = head; + int index = 0; + if (head == null) { + throw new UnsupportedOperationException("Got ya again!"); + } + while (current != null){ + if (current.data == data){ + return index; + } + index++; + current = current.next; + } + return -1; + } + + public String get (int index) { + Node current = head; + int trackIndex = 0; + if (head == null || index > size() || index <0) { + throw new UnsupportedOperationException("THERE'S NO LIST"); + } + while (current != null){ + if (trackIndex ==index){ + return current.data; + } + trackIndex++; + current = current.next; + } + return null; + } + + public SinglyLinkedList copy() { + SinglyLinkedList copied = new SinglyLinkedList(); + + Node current = head; + if (head ==null) { + throw new UnsupportedOperationException("Nick proved this would happen"); + } + while (current != null){ + copied.addNode(current.data); + current = current.next; + } + return copied; + } + + public SinglyLinkedList reverse () { + SinglyLinkedList reverseList = new SinglyLinkedList(); + int i = size() -1; + if (head == null) { + throw new UnsupportedOperationException("Try it again"); + } + while (i >=0) { + reverseList.addNode(get(i)); + i--; + } + return reverseList; + } + + public String slice(int start, int end){ + String substring = ""; + Node current = head; + if (head == null){ + throw new UnsupportedOperationException("You wildin again, no list buddy"); + } + int i = 0; + while (current.next != null){ + if (i >= start && i < end) { + substring += current.data; + } + current = current.next; + i++; + } + + return substring; + } + + public void sort(){ + //Node current = head; + int length = size(); + + for (int i = 0; i < length; i++) { + Node current = head; + while (current.next != null){ + String currentValue = current.data; + String nextValue = current.next.data; + if (compareNodeValue(currentValue, nextValue)){ + String temp = current.data; + current.data = nextValue; + current.next.data = temp; + } + current = current.next; + } + + } + +// while (i < length) { +// String currentValue = copyOfSort.get(i); +// String nextValue = copyOfSort.get(i + 1); +// while (compareNodeValue(currentValue, nextValue)) { +// String temp = currentValue; +// copyOfSort.setNode(i, nextValue); +// copyOfSort.setNode(i+1, temp); //null?? +// //copyOfSort.addNode(nextValue); +// i++; +// } +//// copyOfSort.addNode(current.data); +// i++; +// current = current.next; +// } + + //return copyOfSort; + } + + public void setNode (int index, String reset) { + Node current = head; + int trackIndex = 0; + if (head == null || index > size() || index <0) { + throw new UnsupportedOperationException("THERE'S NO LIST"); + } + while (current != null) { + if (trackIndex == index) { + current.data = reset; + } + trackIndex++; + current = current.next; + } + } + + public Boolean compareNodeValue(String first, String next){ + if (first == null || next == null){ + throw new UnsupportedOperationException("empty node"); + } + + if (first.charAt(0) > next.charAt(0)){ + return true; + } + return false; + } + } diff --git a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java index 5cc057e..3d63eb9 100644 --- a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java +++ b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java @@ -1,7 +1,329 @@ 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(){ + //given + SinglyLinkedList testString = new SinglyLinkedList(); + + //when + testString.addNode("Hey"); + String expected = "Hey"; + + //then + String actual = testString.get(0); + Assert.assertEquals(expected, actual); + + + } + + @Test + public void addNodeTest2(){ + //given + SinglyLinkedList testString = new SinglyLinkedList(); + + //when + testString.addNode("Hey"); + int expected = 1; + + //then + int actual = testString.size(); + Assert.assertEquals(expected, actual); + } + + @Test + public void addNodeTest3(){ + //given + SinglyLinkedList testString = new SinglyLinkedList(); + + //when + testString.addNode("Hey"); + testString.addNode("there"); + int expected = 2; + + //then + int actual = testString.size(); + Assert.assertEquals(expected, actual); + } + + + + @Test (expected = UnsupportedOperationException.class) + public void sliceErrorTest(){ + //given + SinglyLinkedList testString = new SinglyLinkedList(); + + //when + testString.slice(3,5); + + //then + //Assert.assertEquals(expected, actual); + } + + @Test + public void sliceTest(){ + //given + SinglyLinkedList testString = new SinglyLinkedList(); + testString.addNode("Wonder"); + testString.addNode("if "); + testString.addNode("this "); + testString.addNode("will "); + testString.addNode("be "); + testString.addNode("cut"); + testString.addNode("right"); + String expected = "this will be cut"; + + //when + String actual = testString.slice(2,6); + + + //then + Assert.assertEquals(expected, actual); + } + + + @Test (expected = UnsupportedOperationException.class) + public void removeExceptionTest(){ + //given + SinglyLinkedList testString = new SinglyLinkedList(); + + //when + testString.remove("Hey"); + + } + + @Test + public void removeFirstTest(){ + //given + SinglyLinkedList testString = new SinglyLinkedList(); + testString.addNode("Hey"); + testString.addNode("there"); + + //when + testString.remove(0); + String actual = testString.get(0); + + //then + Assert.assertNull(actual); + + } + + @Test + public void removeSecondTest(){ + //given + SinglyLinkedList testString = new SinglyLinkedList(); + testString.addNode("Hey"); + testString.addNode("there"); + testString.addNode("Delilah"); + + //when + testString.remove("there"); + String actual = testString.get(0); + + //then + String expected = "Hey"; + Assert.assertEquals(expected, actual); + + } + + @Test + public void removeCheckThirdTest(){ + //given + SinglyLinkedList testString = new SinglyLinkedList(); + testString.addNode("Hey"); + testString.addNode("there"); + testString.addNode("Delilah"); + + //when + testString.remove("there"); + String actual = testString.get(2); + + //then + String expected = "Delilah"; + Assert.assertEquals(expected, actual); + + } + + @Test + public void checkNewListWithRemove(){ + //given + SinglyLinkedList testString = new SinglyLinkedList(); + testString.addNode("Hey"); + testString.addNode("there"); + testString.addNode("Delilah"); + + //when + testString.remove("there"); + SinglyLinkedList actual = testString.remove("there"); + String actualString = actual.get(1); + + //then + String expected = "Delilah"; + Assert.assertEquals(expected, actualString); + + } + + @Test + public void containsTest(){ + //given + SinglyLinkedList testString = new SinglyLinkedList(); + testString.addNode("Hey"); + testString.addNode("there"); + + //when + boolean contains = testString.contains("there"); + + //then + Assert.assertTrue(contains); + + } + + @Test + public void findHelloAbsentTest(){ + //given + SinglyLinkedList testString = new SinglyLinkedList(); + testString.addNode("Hey"); + testString.addNode("there"); + testString.addNode("Delilah"); + int expected = -1; + + //when + int actual = testString.find("Hello"); + + //then + Assert.assertEquals(expected, actual); + + } + + @Test + public void findHelloPresentTest(){ + //given + SinglyLinkedList testString = new SinglyLinkedList(); + testString.addNode("Hey"); + testString.addNode("there"); + testString.addNode("Delilah"); + testString.addNode("Hello"); + int expected = 3; + + //when + int actual = testString.find("Hello"); + + //then + Assert.assertEquals(expected, actual); + + } + + @Test (expected = UnsupportedOperationException.class) + public void copyNothingTest(){ + //given + SinglyLinkedList perfect = new SinglyLinkedList(); + + //when + perfect.copy(); + } + + @Test + public void copyNotNullTest(){ + //given + SinglyLinkedList perfect = new SinglyLinkedList(); + perfect.addNode("I"); + perfect.addNode("went"); + perfect.addNode("the"); + perfect.addNode("extra"); + perfect.addNode("mile"); + + //when + SinglyLinkedList copy = perfect.copy(); + String actual = copy.get(1); + String expected = perfect.get(1); + + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void sortTest(){ + //given + SinglyLinkedList testSort = new SinglyLinkedList(); + testSort.addNode("i"); + testSort.addNode("went"); + testSort.addNode("the"); + testSort.addNode("extra"); + testSort.addNode("mile"); + String expected = "extra"; + + //when + testSort.sort(); + String actual = testSort.get(0); + + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void reverseTest(){ + //given + SinglyLinkedList testSort = new SinglyLinkedList(); + testSort.addNode("i"); + testSort.addNode("went"); + testSort.addNode("the"); + testSort.addNode("extra"); + testSort.addNode("mile"); + String expected = "mile"; + + //when + SinglyLinkedList sorted = testSort.reverse(); + String actual = sorted.get(0); + + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void setTest(){ + //given + SinglyLinkedList setThis = new SinglyLinkedList(); + setThis.addNode("hope"); + setThis.addNode("this"); + setThis.addNode("doesnt"); + setThis.addNode("works"); + String reset = "does"; + + //when + setThis.setNode(2, reset); + String actual = setThis.get(2); + + //then + Assert.assertEquals(reset, actual); + + } + + @Test + public void compareTest(){ + //given + SinglyLinkedList setThis = new SinglyLinkedList(); + setThis.addNode("hope"); + setThis.addNode("this"); + setThis.addNode("doesnt"); + setThis.addNode("works"); + + //when + String first = setThis.get(1); + String second = setThis.get(2); + + boolean actual = setThis.compareNodeValue(first, second); + + + //then + Assert.assertTrue(actual); + + } + }