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..2afaadc 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,86 +1,26 @@ + + - + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ 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..1a23fbb 100644 --- a/src/main/java/com/zipcodewilmington/singlylinkedlist/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/singlylinkedlist/MainApplication.java @@ -1,7 +1,28 @@ package com.zipcodewilmington.singlylinkedlist; +import java.util.LinkedList; + /** * Created by leon on 1/10/18. */ public class MainApplication { + public static void main(String[] args) { + + SinglyLinkedList myList = new SinglyLinkedList(); + + myList.add(1); + myList.add(2); + myList.add(3); + myList.add(4); + myList.add(5); + myList.add(55); + myList.displayNodes(); + + System.out.println(myList.get(5).getData()); + + + + + } + } diff --git a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java index 2fb3165..a1935e7 100644 --- a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java +++ b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java @@ -1,7 +1,234 @@ package com.zipcodewilmington.singlylinkedlist; /** - * Created by leon on 1/10/18. + * Created by Nathan on 7/25/22. */ -public class SinglyLinkedList { +public class SinglyLinkedList { + + class Node { + T data; + Node next; + + public Node(T data){ + this.data = data; + this.next = null; + } + + public T getData() { + return data; + } + } + + public Node head; + public Node tail; + + public SinglyLinkedList(){ + this.head = null; + this.tail = null; + } + public void add(T data){ + Node newNode = new Node(data); + if(head == null){ + head = newNode; + tail = newNode; + } else { + tail.next = newNode; + tail = newNode; + } + } + + public void addHead(T data){ + Node newNode = new Node(data); + + if(head == null){ + head = newNode; + tail = newNode; + } else { + newNode.next = head; + head = newNode; + } + } + + public boolean contains(T data){ + Node current = head; + while(current != null){ + if(current.data.equals(data)){ + return true; + } + current = current.next; + } + return false; + } + + public void removeTail(){ + Node current = head; + if(current == null){ + return; + } + Node nextNode = current.next; + while(current.next != null){ + if(nextNode.next == null){ + tail = current; + current.next = null; + return; + } + current = nextNode; + nextNode = current.next; + } + } + + public void removeHead(){ + Node current = head; + if(current == null || current.next == null){ + return; + } + if(head != null){ + head = current.next; + } + + } + + public void remove(Integer index){ + if(index < 0 || index > this.size()){ + throw new UnsupportedOperationException("Index is out of bounds :("); + } + if(index == 0){ + this.removeHead(); + return; + } + if(index == (this.size() - 1)){ + this.removeTail(); + return; + } else { + Integer currentIndex = 0; + Node current = head; + while(currentIndex < (index - 1)){ + current = current.next; + currentIndex++; + } + Node nextNode = current.next; + current.next = nextNode.next; + } + } + + public Integer findIndex(T data){ + Node current = head; + Integer index = 0; + if(current == null){ + return -1; + } + while(current != null){ + if(current.data == data){ + return index; + } + current = current.next; + index++; + } + return -1; + } + + public Integer size(){ + Integer size = 0; + Node current = head; + while(current != null){ + size++; + current = current.next; + } + return size; + } + + public Node get(Integer index){ + if(index < 0 || index > (this.size() -1)){ + throw new UnsupportedOperationException("Chosen index is outside the scope of this list! :("); + } + Node current = head; + Integer currentIndex = 0; + while(currentIndex != index){ + current = current.next; + currentIndex++; + } + return current; + } + + public void displayNodes() { + Node current = head; + if (head == null) { + System.out.println("List is empty"); + return; + } + System.out.println("Nodes of singly linked list: "); + while (current != null) { + System.out.print(current.data + " "); + current = current.next; + } + System.out.println(); + } + + + public SinglyLinkedList clone(){ + SinglyLinkedList cloned = new SinglyLinkedList(); + Node current = head; + while(current != null){ + cloned.add(current.data); + current = current.next; + } + return cloned; + } + + public void reverse(){ + tail = head; + Node origin = head; + Node prev = null; + Node current = null; + + while(origin != null){ + current = origin; + origin = origin.next; + current.next = prev; + prev = current; + head = current; + } + } + + public void sort(){ + Node current = head; + for (int i = 0; i < size(); i++) { + current = head; + while(current.next != null){ + if(compareTo(current.getData(), current.next.getData()) > 0){ + T nextValue = current.next.getData(); + current.next.data = current.data; + current.data = nextValue; + } + current = current.next; + } + } + + } + + public Integer compareTo(T obj, T obj2){ + return obj.compareTo(obj2); + } + + public SinglyLinkedList slice(Integer start, Integer end){ +// SinglyLinkedList cloned = this.clone(); +// for (int i = 0; i < start; i++) { +// cloned.removeHead(); +// } +// for (int i = this.size(); i > end; i--) { +// cloned.removeTail(); +// } + SinglyLinkedList cloned = new SinglyLinkedList(); + for (int i = 0; i < end; i++) { + if(i >= start){ + cloned.add(get(i).getData()); + } + } + return cloned; + } + + + + + } diff --git a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java index 5cc057e..991c786 100644 --- a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java +++ b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java @@ -1,7 +1,192 @@ package com.zipcodewilmington.singlylinkedlist; +import org.junit.Assert; +import org.junit.Test; + /** - * Created by leon on 1/10/18. + * Created by Nathan on 7/25/22. */ public class SinglyLinkedListTest { + @Test + public void testConstruction(){ + //when + SinglyLinkedList newList = new SinglyLinkedList(); + //then + Assert.assertTrue(newList instanceof SinglyLinkedList); + } + + @Test + public void testAddNext(){ + //given + SinglyLinkedList myList = new SinglyLinkedList(); + //when + myList.add("2"); + //then + Assert.assertTrue(myList.contains("2")); + } + + @Test + public void addHeadTest(){ + //given + SinglyLinkedList myList = new SinglyLinkedList(); + //when + myList.add(2); + myList.addHead(1); + Integer expected = 1; + Integer actual = myList.get(0).getData(); + //then + + Assert.assertEquals(expected,actual); + } + + @Test + public void testContains(){ + //given + SinglyLinkedList myList = new SinglyLinkedList(); + myList.add(1); + myList.add(2); + //when + boolean actual = myList.contains(2); + //then + Assert.assertTrue(actual); + } + + @Test + public void testRemoveTail(){ + //given + SinglyLinkedList myList = new SinglyLinkedList(); + myList.add(1); + myList.add(2); + //when + myList.removeTail(); + Integer exp = 1; + Integer act = myList.size(); + //then + Assert.assertEquals(exp,act); + } + + @Test + public void testRemoveHead(){ + //given + SinglyLinkedList myList = new SinglyLinkedList(); + myList.add(1); + myList.add(2); + myList.add(3); + //when + myList.removeHead(); + Integer exp = 2; + Integer act = myList.size(); + //then + Assert.assertEquals(exp,act); + } + + @Test + public void testRemove_firstIndex(){ + //given + SinglyLinkedList newList = new SinglyLinkedList(); + newList.add(1); + newList.add(2); + newList.add(3); + //when + newList.remove(0); + Integer exp = 2; + Integer act = newList.size(); + //then + Assert.assertEquals(exp,act); + } + + @Test + public void testRemove_lastIndex(){ + //given + SinglyLinkedList newList = new SinglyLinkedList(); + newList.add(1); + newList.add(2); + newList.add(3); + //when + newList.remove(2); + boolean actual = newList.contains(3); + //then + Assert.assertFalse(actual); + } + + @Test + public void testFindIndex(){ + //given + SinglyLinkedList newList = new SinglyLinkedList(); + newList.add(1); + newList.add(2); + newList.add(3); + //when + Integer actual = newList.findIndex(3); + Integer expected = 2; + //then + Assert.assertEquals(expected,actual); + } + + @Test + public void testClone(){ + //given + SinglyLinkedList newList = new SinglyLinkedList(); + newList.add(1); + newList.add(2); + newList.add(3); + //when + SinglyLinkedList clone = newList.clone(); + //then + clone.displayNodes(); + newList.displayNodes(); + Assert.assertNotEquals(clone, newList); + } + + @Test + public void testReverse(){ + //given + SinglyLinkedList newList = new SinglyLinkedList(); + newList.add(1); + newList.add(2); + newList.add(3); + //when + newList.reverse(); + Integer expected = 3; + Integer actual = newList.get(0).getData(); + //then + Assert.assertEquals(expected,actual); + } + + @Test + public void testSort(){ + //given + SinglyLinkedList newList = new SinglyLinkedList(); + newList.add(5); + newList.add(4); + newList.add(3); + newList.add(2); + newList.add(1); + //when + newList.sort(); + //then + newList.displayNodes(); + } + + @Test + public void testSlice(){ + //given + SinglyLinkedList newList = new SinglyLinkedList(); + newList.add(1); + newList.add(2); + newList.add(3); + newList.add(4); + newList.add(5); + newList.add(6); + newList.add(7); + newList.add(8); + //when + SinglyLinkedList sliced = newList.slice(2,7); + //then + sliced.displayNodes(); + } + + + + }