mirror of
https://github.com/RustyRaptor/CS-272.git
synced 2026-12-03 15:55:22 -07:00
Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
3380531e56
2 files changed
+86
-9
No files matched your search
+58
-9
@@ -158,11 +158,16 @@ public class IntNode {
|
||||
IntNode cursor;
|
||||
for (cursor = this; cursor.getLink() != null; cursor
|
||||
= cursor.getLink()){
|
||||
continue;
|
||||
}
|
||||
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 +227,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 +309,10 @@ public class IntNode {
|
||||
return "IntNode{" +
|
||||
"data=" + linkStr +
|
||||
'}';
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* prints out the toString value of the Node.
|
||||
*/
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in new issue
Block a user