From 6f25750b54677b126a6061d8148d98e6cf8053a1 Mon Sep 17 00:00:00 2001 From: RustyRaptor Date: Mon, 30 Sep 2019 23:29:19 -0600 Subject: [PATCH 1/2] Finished Lab 5 hooray --- src/IntNode.java | 66 +++++++++++++++++++++++++++++++----- src/IntNodeAdvancedTest.java | 28 +++++++++++++++ 2 files changed, 86 insertions(+), 8 deletions(-) diff --git a/src/IntNode.java b/src/IntNode.java index 6b3bfa1..4de513a 100644 --- a/src/IntNode.java +++ b/src/IntNode.java @@ -163,6 +163,12 @@ public class IntNode { cursor.addNodeAfterThis(newData); } + /** + * returns the sum of the last N items in the linked list + * @param head the head + * @param num the number of items from the end we want to sum up + * @return and integer of the sum. + */ public static int sumLast(IntNode head, int num) { int sum = 0; int numLast = IntNode.listLength(head) - num; @@ -222,22 +228,64 @@ public class IntNode { return copyHead; } + /** + * remove all of the items equal to e + * @param head the head of the Intnode we want to remove from + * @param e the integer we wanna remove + * @return a new linked list with the items removed. + */ public static IntNode removeAll(IntNode head, int e) { if(head == null) return null; - IntNode newHead = head; - while (newHead.getData() != e) { - newHead = newHead.getLink(); - } - - for (IntNode cursor = newHead; cursor.getLink() != null; cursor - = cursor.getLink()){ + IntNode start = new IntNode(0, head); + IntNode cursor = start; + while ( cursor.getLink() != null){ if (cursor.getLink().getData() == e){ cursor.removeNodeAfterThis(); + } else { + cursor = cursor.getLink(); } } - return newHead; + return start.getLink(); } + /** + * returns the given linked list in reverse order + * @param head the head of the list we want to reverse + * @return a reversed IntNode + */ + public static IntNode reverse (IntNode head) { + IntNode cursor = head; + IntNode newHead = new IntNode(0, null); + int[] a = new int[IntNode.listLength(head)]; + for (int i = 0; i < a.length; i++) { + a[i] = cursor.getData(); + cursor = cursor.getLink(); + } + for (int i = a.length-1; i >= 0; i--) { + newHead.addToEnd(a[i]); + } + + return newHead.getLink(); + + } + + /** + * Checks heuristically if the Linked list goes into a loop + * @param head the head of the node we are checking + * @return true or false + */ + public static boolean hasCycle(IntNode head){ + if (head == null) return false; + IntNode cursor = head; + while (cursor.link != null) { + if (cursor.link.data == head.data) { + return true; + } else { + cursor = cursor.link; + } + } + return false; + } /** * Builds a string from all the nodes in the list. * @@ -262,8 +310,10 @@ public class IntNode { return "IntNode{" + "data=" + linkStr + '}'; + } + /** * prints out the toString value of the Node. */ diff --git a/src/IntNodeAdvancedTest.java b/src/IntNodeAdvancedTest.java index a46e68d..5ef1ddd 100644 --- a/src/IntNodeAdvancedTest.java +++ b/src/IntNodeAdvancedTest.java @@ -11,12 +11,19 @@ public class IntNodeAdvancedTest { public static void main(String[] args) { System.out.println("Testing add node"); testAddNodeAfterThis(); + System.out.println("Testing Add Range After This"); testAddRangeAfterThis(); + System.out.println("Testing Testing Print"); testPrintData(); + System.out.println("Testing Remove after this"); testRemoveNodeAfterThis(); + System.out.println("Testing toString"); testTestToString(); + System.out.println("Testing Search"); testSearch(); + System.out.println("Testing list even number"); testListEvenNumber(); + System.out.println("Testing List Length"); testListLength(); System.out.println("Testing Sum Last"); testSumLast(); @@ -25,6 +32,10 @@ public class IntNodeAdvancedTest { testCopyOdd(); System.out.println("TESTING REMOVE ALL"); testRemoveAll(); + System.out.println("TESTING REVERSE "); + testReverse(); + System.out.println("TEST HAS CYCLE"); + testHasCycle(); System.out.println("Testing All"); testAll(); } @@ -37,6 +48,23 @@ public class IntNodeAdvancedTest { } + private static void testHasCycle() { + IntNode myNode = new IntNode(); + System.out.println(IntNode.hasCycle(myNode)); + myNode.addRangeAfterThis(0, 10); +// myNode.getLink().getLink().getLink().getLink().getLink().getLink().getLink().getLink().getLink().getLink().setLink(myNode); + System.out.println(IntNode.hasCycle(myNode)); + } + + private static void testReverse() { + IntNode myNode = new IntNode(); + System.out.println(IntNode.reverse(myNode)); + myNode.addRangeAfterThis(0, 10); + System.out.println(IntNode.reverse(myNode)); + System.out.println(IntNode.reverse(IntNode.reverse(myNode))); + + } + private static void testCopyOdd() { IntNode myNode = new IntNode(); System.out.println(IntNode.copyOdd(myNode)); From 107639225781faff1081318dbe9b4d509800ad89 Mon Sep 17 00:00:00 2001 From: RustyRaptor Date: Mon, 30 Sep 2019 23:29:56 -0600 Subject: [PATCH 2/2] removed redundant continue --- src/IntNode.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/IntNode.java b/src/IntNode.java index 4de513a..30b8b36 100644 --- a/src/IntNode.java +++ b/src/IntNode.java @@ -158,7 +158,6 @@ public class IntNode { IntNode cursor; for (cursor = this; cursor.getLink() != null; cursor = cursor.getLink()){ - continue; } cursor.addNodeAfterThis(newData); }