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..0788368 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,86 +1,19 @@ + + - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -212,60 +146,19 @@ + + - + + + - - - - - - - - - - + + + + + + + + + + + + + + - - - - - - - - + + + + + + + - - - - - - + @@ -494,12 +382,38 @@ - - \ 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..6dbacc3 100644 --- a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java +++ b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java @@ -3,5 +3,172 @@ /** * 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 addNode(T data){ + Node newNode = new Node(data); + + if(head == null) { + head = newNode; + } else { + tail.next = newNode; + } + tail = newNode; //on the first iteration, head and tail mean the same thing + } + + + public Integer size(){ + Integer count = 0; + Node current = this.head; + count = current.next == null ? 0 : 1; + while(current.next != null){ + count++; + current = current.next; + } + return count; + } + + public Integer find(T valueToFind){ + Node current = this.head; + Integer index = 0; + if(current == null) + return -1; + while(current != null){ + if(current.data.equals(valueToFind)) + return index; + index++; + current = current.next; + } + return -1; + } + + public Boolean contains(T valueToFind){ + return this.find(valueToFind) >= 0; + } + + + public void remove(T valueToFind){ + Node current = head; + Node last = head; + while(current != null){ + if(current.data.equals(valueToFind)) + if(current.next == null) { + last.next = null; + } else if(current == last){ + head = current.next; + } + else { + last.next = current.next; + } + last = current; + current = current.next; + + } + } + + 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; + } + } + + public T get(Integer index){ + Node current = head; + Integer currentIndex = 0; + if(index >= size()) + return null; + if(index == size() - 1) + return (T)tail.data; + for(int i = 0; i < index; i++){ + current = current.next; + } + return (T)current.data; + } + + public SinglyLinkedList copy(){ + SinglyLinkedList copiedList = new SinglyLinkedList(); + Node current = head; + + while(current != null){ + copiedList.addNode((T)current.data); + current = current.next; + } + return copiedList; + } + + public void set(Integer index, T valueToSet){ + Node current = head; + + for(int i = 0; i < index; i++){ + current = current.next; + } + current.data = valueToSet; + } + //10 1 + //15 + //2 + //1 + public void sort(){ + Node current = head; + for(int i = 0; i < size(); i++){ + current = head; + while(current.next != null){ + if(compareTo((T)current.data, (T)current.next.data) > 0){ + T nextVal = (T)current.next.data; + current.next.data = current.data; + current.data = nextVal; + } + current = current.next; + } + } + } + + public SinglyLinkedList slice(int start, int end){ + SinglyLinkedList subList = new SinglyLinkedList(); + + for(int i = 0; i < end; i++){ + if(i >= start) + subList.addNode(get(i)); + } + return subList; + } + + public void reverse(){ + Node orig = head; + Node prev = null; + Node current = null; + + while(orig != null) { + current = orig; + orig = orig.next; + + current.next = prev; + prev = current; + head = current; + //this.display(); + } + } + + public int compareTo(T o, T o2) { + return o.compareTo(o2); + } } diff --git a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java index 5cc057e..fb77fc4 100644 --- a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java +++ b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java @@ -1,7 +1,157 @@ 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 testAdd(){ + SinglyLinkedList list = new SinglyLinkedList(); + list.addNode(1); + list.addNode(2); + list.addNode(3); + + Integer expected = 3; + Integer actual = list.size(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testFind(){ + SinglyLinkedList list = new SinglyLinkedList(); + list.addNode("Truck"); + list.addNode("Car"); + list.addNode("Van"); + + Integer expected = 2; + Integer actual = list.find("Van"); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testContains(){ + SinglyLinkedList list = new SinglyLinkedList(); + list.addNode("Truck"); + list.addNode("Car"); + list.addNode("Van"); + + Boolean actual = list.contains("Van"); + + Assert.assertTrue(actual); + } + + @Test + public void testRemove(){ + SinglyLinkedList list = new SinglyLinkedList(); + list.addNode("Truck"); + list.addNode("Car"); + list.addNode("Lame Jeep"); + list.addNode("Car"); + list.addNode("Van"); + + list.remove("Truck"); + Integer expected = 4; + Integer actual = list.size(); + list.display(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testGet(){ + SinglyLinkedList list = new SinglyLinkedList(); + list.addNode(10l); + list.addNode(12l); + list.addNode(155l); + list.addNode(100l); + + Long expected = 155l; + Long actual = list.get(2); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testCopy(){ + SinglyLinkedList list = new SinglyLinkedList(); + Float expected = 2.3f; + Float expected2 = 1.8f; + + + list.addNode(1.0f); + list.addNode(1.3f); + list.addNode(2.3f); + list.addNode(1.5f); + + SinglyLinkedList copied = list.copy(); + + list.set(2, expected2); //if shallow, thisw would cahnge both lists + + Float actual = copied.get(2); + Float actual2 = list.get(2); + + Assert.assertEquals(expected, actual); + Assert.assertEquals(expected2, actual2); + } + + @Test + public void testSort(){ + SinglyLinkedList list = new SinglyLinkedList(); + list.addNode(12); + list.addNode(21); + list.addNode(33); + list.addNode(1); + list.addNode(200); + list.addNode(4); + + list.sort(); + + Integer expected = 1; + Integer actual = list.get(0); + list.display(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testReverse(){ + SinglyLinkedList list = new SinglyLinkedList(); + list.addNode(12); + list.addNode(21); + list.addNode(33); + list.addNode(4); + + list.reverse(); + + Integer expected = 4; + Integer actual = list.get(0); + + list.display(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testSlice(){ + SinglyLinkedList list = new SinglyLinkedList(); + + list.addNode(12); + list.addNode(21); + list.addNode(33); + list.addNode(4); + list.addNode(3); + list.addNode(100); + + SinglyLinkedList result = list.slice(1, 4); + Integer expected = 3; + Integer actual = result.size(); + + result.display(); + Assert.assertEquals(expected, actual); + } }