mirror of
https://github.com/RustyRaptor/CS-272.git
synced 2026-12-03 15:55:22 -07:00
Started Lab5 IntNode 2 Electric Boogaloo
This commit is contained in:
1 parent
6c9e4dc679
commit
1ef79453a3
2 files changed
+150
-24
No files matched your search
+84
-10
@@ -29,6 +29,7 @@ public class IntNode {
|
||||
/**
|
||||
* removes an X number of nodes starting from the node it's called
|
||||
* from
|
||||
*
|
||||
* @param count the number of nodes you want to remove
|
||||
*/
|
||||
public void removeXNodesAfterThis(int count) {
|
||||
@@ -43,16 +44,30 @@ public class IntNode {
|
||||
|
||||
/**
|
||||
* add a range of ints to a List
|
||||
*
|
||||
* @param start the start of the range
|
||||
* @param end the end of the range
|
||||
* @param end the end of the range
|
||||
*/
|
||||
public void addRangeAfterThis(int start, int end) {
|
||||
for (int i = end; i > start; i--) {
|
||||
addNodeAfterThis(i);
|
||||
this.addNodeAfterThis(i);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a range of numbers to end the of the linked list.
|
||||
*
|
||||
* @param start the start of the range
|
||||
* @param end the end of the range
|
||||
*/
|
||||
public void addRangeToEnd(int start, int end) {
|
||||
for (int i = start; i <= end; i++) {
|
||||
this.addToEnd(i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Getters and setters
|
||||
public int getData() {
|
||||
return data;
|
||||
@@ -73,12 +88,14 @@ public class IntNode {
|
||||
|
||||
/**
|
||||
* adds a new IntNode after the node it's called from
|
||||
* @param newdata the data you want to add (int)
|
||||
*
|
||||
* @param newData the data you want to add (int)
|
||||
*/
|
||||
public void addNodeAfterThis(int newdata) {
|
||||
setLink(new IntNode(newdata, this.link));
|
||||
public void addNodeAfterThis(int newData) {
|
||||
setLink(new IntNode(newData, this.link));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* removes the IntNode after the one it's called from
|
||||
*/
|
||||
@@ -94,6 +111,7 @@ public class IntNode {
|
||||
|
||||
/**
|
||||
* gets the number of nodes after the one it's called from.
|
||||
*
|
||||
* @param head the head node you want to start from
|
||||
* @return the number of items in the List
|
||||
*/
|
||||
@@ -103,7 +121,7 @@ public class IntNode {
|
||||
}
|
||||
int cnt = 1;
|
||||
for (IntNode cursor = head.getLink(); cursor != null; cursor
|
||||
= cursor.getLink()){
|
||||
= cursor.getLink()) {
|
||||
cnt++;
|
||||
}
|
||||
return cnt;
|
||||
@@ -112,6 +130,7 @@ public class IntNode {
|
||||
|
||||
/**
|
||||
* returns number of even numbers in the list starting from "head" node
|
||||
*
|
||||
* @param head the node to start from.\
|
||||
* @return integer
|
||||
*/
|
||||
@@ -121,8 +140,8 @@ public class IntNode {
|
||||
}
|
||||
int cnt = 1;
|
||||
for (IntNode cursor = head.getLink(); cursor != null; cursor
|
||||
= cursor.getLink()){
|
||||
if (cursor.data%2 == 0) {
|
||||
= cursor.getLink()) {
|
||||
if (cursor.data % 2 == 0) {
|
||||
cnt++;
|
||||
}
|
||||
}
|
||||
@@ -130,10 +149,39 @@ public class IntNode {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* adds a new integer to the end of the linked list.
|
||||
*
|
||||
* @param newData the new integer to be added.
|
||||
*/
|
||||
public void addToEnd(int newData) {
|
||||
IntNode cursor;
|
||||
for (cursor = this; cursor.getLink() != null; cursor
|
||||
= cursor.getLink()){
|
||||
continue;
|
||||
}
|
||||
cursor.addNodeAfterThis(newData);
|
||||
}
|
||||
|
||||
public static int sumLast(IntNode head, int num) {
|
||||
int sum = 0;
|
||||
int numLast = IntNode.listLength(head) - num;
|
||||
int cnt = 0;
|
||||
for (IntNode cursor = head; cursor != null; cursor =
|
||||
cursor.getLink()) {
|
||||
if (cnt >= numLast) {
|
||||
sum += cursor.getData();
|
||||
}
|
||||
cnt++;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* searches for a data input and returns whether or not it exists in
|
||||
* the List
|
||||
* @param head the head node to start from in the search loop
|
||||
*
|
||||
* @param head the head node to start from in the search loop
|
||||
* @param _data the data you want to search for
|
||||
* @return true or false depending on what it finds
|
||||
*/
|
||||
@@ -142,7 +190,7 @@ public class IntNode {
|
||||
return false;
|
||||
}
|
||||
for (IntNode cursor = head.getLink(); cursor != null; cursor
|
||||
= cursor.getLink()){
|
||||
= cursor.getLink()) {
|
||||
if (cursor.data == _data) {
|
||||
return true;
|
||||
}
|
||||
@@ -150,8 +198,34 @@ public class IntNode {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns a new Linked list with all the odd numbers from source
|
||||
* @param source the source node to copy from.
|
||||
* @return new IntNode with odd nums from source IntNode
|
||||
*/
|
||||
public static IntNode copyOdd (IntNode source) {
|
||||
IntNode copyHead;
|
||||
IntNode copyTail;
|
||||
|
||||
if (source == null) return null;
|
||||
|
||||
copyHead = new IntNode(source.getData(), null);
|
||||
copyTail = copyHead;
|
||||
|
||||
while (source.getLink() != null && ) {
|
||||
source = source.getLink();
|
||||
if (source.getData() % 2 == 0) {
|
||||
copyTail.addNodeAfterThis(source.getData());
|
||||
}
|
||||
copyTail = copyTail.getLink();
|
||||
}
|
||||
|
||||
return copyHead;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a string from all the nodes in the list.
|
||||
*
|
||||
* @return a string of the data separated by arrows.
|
||||
*/
|
||||
@Override
|
||||
|
||||
@@ -9,13 +9,52 @@ public class IntNodeAdvancedTest {
|
||||
* This main function just runs all the test functions.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
IntNodeAdvancedTest.testAddNodeAfterThis();
|
||||
IntNodeAdvancedTest.testAddRangeAfterThis();
|
||||
IntNodeAdvancedTest.testPrintData();
|
||||
IntNodeAdvancedTest.testRemoveNodeAfterThis();
|
||||
IntNodeAdvancedTest.testTestToString();
|
||||
IntNodeAdvancedTest.testSearch();
|
||||
IntNodeAdvancedTest.testAll();
|
||||
System.out.println("Testing add node");
|
||||
testAddNodeAfterThis();
|
||||
testAddRangeAfterThis();
|
||||
testPrintData();
|
||||
testRemoveNodeAfterThis();
|
||||
testTestToString();
|
||||
testSearch();
|
||||
testListEvenNumber();
|
||||
testListLength();
|
||||
System.out.println("Testing Sum Last");
|
||||
testSumLast();
|
||||
System.out.println("Testing Copy ODD");
|
||||
testCopyOdd();
|
||||
System.out.println("Testing All");
|
||||
testAll();
|
||||
}
|
||||
|
||||
private static void testCopyOdd() {
|
||||
IntNode myNode = new IntNode();
|
||||
System.out.println(IntNode.copyOdd(myNode));
|
||||
myNode.addRangeAfterThis(0, 10);
|
||||
System.out.println(IntNode.copyOdd(myNode));
|
||||
|
||||
}
|
||||
|
||||
private static void testSumLast() {
|
||||
IntNode myNode = new IntNode();
|
||||
System.out.println(IntNode.sumLast(myNode, 2));
|
||||
myNode.addRangeToEnd(0, 100);
|
||||
System.out.println(myNode);
|
||||
System.out.println(IntNode.sumLast(myNode, 100));
|
||||
System.out.println(IntNode.sumLast(myNode, 1));
|
||||
for (int i = 0; i < 100; i++){
|
||||
System.out.print(IntNode.sumLast(myNode, i) + " ");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void testListLength() {
|
||||
IntNode myNode = new IntNode();
|
||||
System.out.println(IntNode.listLength(myNode));
|
||||
myNode.addRangeAfterThis(0, 100);
|
||||
System.out.println(IntNode.listLength(myNode));
|
||||
myNode.addRangeAfterThis(1000, 2000);
|
||||
System.out.println(IntNode.listLength(myNode));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -23,19 +62,20 @@ public class IntNodeAdvancedTest {
|
||||
IntNode myNode = new IntNode();
|
||||
myNode.addRangeAfterThis(0, 100);
|
||||
myNode.getLink().getLink().getLink().getLink()
|
||||
.addRangeAfterThis(100,200);
|
||||
.addRangeAfterThis(100, 200);
|
||||
myNode.addRangeAfterThis(0, 100);
|
||||
myNode.addRangeAfterThis(1000, 1100);
|
||||
myNode.getLink().getLink().addRangeAfterThis(1000, 1100);
|
||||
|
||||
}
|
||||
|
||||
private static void testListEvenNumber() {
|
||||
IntNode myNode = new IntNode();
|
||||
System.out.println(IntNode.listEvenNumber(myNode));
|
||||
myNode.addRangeAfterThis(0, 100);
|
||||
System.out.println(IntNode.listEvenNumber(myNode));
|
||||
myNode.getLink().getLink().getLink().getLink()
|
||||
.addRangeAfterThis(100,200);
|
||||
.addRangeAfterThis(100, 200);
|
||||
System.out.println(IntNode.listEvenNumber(myNode));
|
||||
myNode.addRangeAfterThis(0, 100);
|
||||
System.out.println(IntNode.listEvenNumber(myNode));
|
||||
@@ -131,12 +171,24 @@ public class IntNodeAdvancedTest {
|
||||
IntNodeAdvancedTest.testPrintData();
|
||||
IntNodeAdvancedTest.testRemoveNodeAfterThis();
|
||||
IntNodeAdvancedTest.testTestToString();
|
||||
System.out.println(IntNode.listEvenNumber(myNode));
|
||||
myNode.addRangeAfterThis(0, 100);
|
||||
System.out.println(IntNode.listEvenNumber(myNode));
|
||||
myNode.getLink().getLink().getLink().getLink()
|
||||
.addRangeAfterThis(100, 200);
|
||||
System.out.println(IntNode.listEvenNumber(myNode));
|
||||
myNode.addRangeAfterThis(0, 100);
|
||||
System.out.println(IntNode.listEvenNumber(myNode));
|
||||
myNode.addRangeAfterThis(1000, 1100);
|
||||
System.out.println(IntNode.listEvenNumber(myNode));
|
||||
myNode.getLink().getLink().addRangeAfterThis(1000, 1100);
|
||||
System.out.println(IntNode.listEvenNumber(myNode));
|
||||
}
|
||||
|
||||
private static void testSearch() {
|
||||
IntNode myNode = new IntNode();
|
||||
IntNode bools = new IntNode();
|
||||
for (int i = 0; i < 100000; i++){
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
IntNode.search(myNode, i);
|
||||
|
||||
}
|
||||
@@ -147,20 +199,20 @@ public class IntNodeAdvancedTest {
|
||||
myNode.addNodeAfterThis(-1000);
|
||||
myNode.addNodeAfterThis(-2000);
|
||||
myNode.addNodeAfterThis(0);
|
||||
for (int i = 0; i < 100000; i++){
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
IntNode.search(myNode, i);
|
||||
|
||||
}
|
||||
myNode.addRangeAfterThis(0, 100);
|
||||
myNode.getLink().getLink().getLink().getLink()
|
||||
.addRangeAfterThis(100,200);
|
||||
.addRangeAfterThis(100, 200);
|
||||
myNode.addRangeAfterThis(0, 100);
|
||||
myNode.addRangeAfterThis(1000, 1100);
|
||||
myNode.getLink().getLink().addRangeAfterThis(1000, 1100);
|
||||
for (int i = 0; i < 100000; i++){
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
if (IntNode.search(myNode, i)) {
|
||||
bools.addNodeAfterThis(1);
|
||||
} else{
|
||||
} else {
|
||||
bools.addNodeAfterThis(0);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user