From 136996e859e89d090217cf223074ff0ae2aaeaef Mon Sep 17 00:00:00 2001 From: Rex Date: Fri, 6 Aug 2021 00:51:03 -0400 Subject: [PATCH] linked the traincars --- .idea/compiler.xml | 2 + .idea/jarRepositories.xml | 20 + .idea/libraries/Maven__junit_junit_4_13_1.xml | 13 + .../Maven__org_hamcrest_hamcrest_core_1_3.xml | 13 + .idea/modules.xml | 1 + .idea/runConfigurations.xml | 10 + .idea/workspace.xml | 429 +++++++----------- pom.xml | 14 + .../singlylinkedlist/SinglyLinkedList.java | 123 ++++- .../SinglyLinkedListTest.java | 83 ++++ 10 files changed, 445 insertions(+), 263 deletions(-) create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/libraries/Maven__junit_junit_4_13_1.xml create mode 100644 .idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml create mode 100644 .idea/runConfigurations.xml 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/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index b6d36bd..ffb063d 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/SinglyLinkedList.java b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java index 2fb3165..addfae6 100644 --- a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java +++ b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java @@ -3,5 +3,124 @@ /** * Created by leon on 1/10/18. */ -public class SinglyLinkedList { -} +public class SinglyLinkedList< T extends Comparable> { + + 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; + } + + public Integer size(){ + Integer count = 0; + Node current = this.head; + + while(current != 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; //if value is not -1 then the list contains the value + } + + 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 T get(Integer index) { + Node current = head; + + if(index >= size() || index < 0) + 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 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 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..aaaebe3 100644 --- a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java +++ b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java @@ -1,7 +1,90 @@ 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 TestAddNode() { + 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("Skyrim"); + list.addNode("Fallout"); + list.addNode("PipMAN"); + + Integer expected = 2; + Integer actual = list.find("PipMAN"); + + Assert.assertEquals(expected, actual); + } + + @Test + public void TestRemove() { + SinglyLinkedList list = new SinglyLinkedList(); + + list.addNode("Skyrim"); + list.addNode("Fallout"); + list.addNode("PipMAN"); + list.addNode("DankBrotherhood"); + list.addNode("ArgonianMaid"); + + list.remove("DankBrotherhood"); + Integer expected = 4; + Integer actual = list.size(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void TestCopy() { + SinglyLinkedList list = new SinglyLinkedList(); + + list.addNode(4.0); + list.addNode(8.0); + list.addNode(6.0); + list.addNode(9.0); + list.addNode(11.0); + + SinglyLinkedList newList = list.copy(); + Integer expected = 5; + Integer actual = newList.size(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void TestSort() { + SinglyLinkedList list = new SinglyLinkedList(); + + list.addNode(4); + list.addNode(8); + list.addNode(6); + list.addNode(9); + list.addNode(11); + + list.sort(); + Integer expected = 6; + Integer actual = list.get(1); + + Assert.assertEquals(expected, actual); + } }