Skip to content

Instantly share code, notes, and snippets.

@quinnzipse
Created November 5, 2019 18:16
Show Gist options
  • Save quinnzipse/4a452f1eee2ad09b9953b38846bb8ccb to your computer and use it in GitHub Desktop.
Save quinnzipse/4a452f1eee2ad09b9953b38846bb8ccb to your computer and use it in GitHub Desktop.
Simple Linked List that contains a simple, recursive function to map and reduce the list.
public class NumberNode {
private NumberNode next;
private int num;
public NumberNode(int num, NumberNode next) {
this.num = num;
this.next = next;
}
public NumberNode(int num) {
this(num, null);
}
public NumberNode() {
this(0, null);
}
public NumberNode getNext() {
return next;
}
public void setNext(NumberNode next) {
this.next = next;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public static void printList(NumberNode head) {
System.out.print("{");
NumberNode pos = head;
while(pos != null) {
if(head != pos) System.out.print(",");
System.out.print(pos.getNum());
pos = pos.getNext();
}
System.out.println("}");
}
public void insertAfter(int number) {
this.setNext(new NumberNode(number, this.getNext()));
}
public void removeAfter() {
NumberNode temp = this.getNext();
this.setNext(temp.getNext());
temp.setNext(null);
}
public int get(int index) throws Exception {
NumberNode pos = this;
for(int i=0; i<index; i++) {
pos = pos.getNext();
if(pos == null) throw new Exception("Index not in list.");
}
return pos.getNum();
}
public int length() {
NumberNode pos = this;
int i = 0;
while(pos != null) {
i++;
pos = pos.getNext();
}
return i;
}
public NumberNode map(IntegerFunction f) {
// Build one node of a result list, using recursion to build
// the rest of the result. So we need to figure out the values
// for both the number field and the next-node field of the new
// result node.
// First apply f to this node's number field to get the new node's
// number field.
int newNumber = f.apply(this.getNum());
// Next recursively call map on the rest of this list, to get the
// rest of the new result list. But if this node's next link is
// null, then we just use null
NumberNode next = this.getNext();
if(next != null) {
next = next.map(f);
}
// Create and return the new list node.
return new NumberNode(newNumber, next);
}
public int reduce(IntegerBinaryFunction op, int dft) {
// We use recursion to first reduce the rest of the list, and then
// produce a value for this node.
final NumberNode next = this.getNext();
int reducedNum;
// Is this next field null?
if(next == null) {
// If so, then we use the default-value argument dft as the result
// of reducing it
reducedNum = dft;
} else {
// If not, then we make a recursive call to reduce on this next field.
reducedNum = next.reduce(op, dft);
}
// Combine this number field with the result of the recursive call, and
// return that result.
return op.apply(this.getNum(), reducedNum);
}
public static void main(String args[]) {
/* final NumberNode head = new NumberNode(1, new NumberNode(2, new NumberNode(3)));
System.out.print("Original list: ");
printList(head);
System.out.println("Adding 4 after the head");
head.insertAfter(4);
System.out.print("New list: ");
printList(head);
System.out.println("Removing the 4 I just added");
head.removeAfter();
System.out.print("Updated list: ");
printList(head);
try { System.out.println("Getting number at index 2: " + head.get(2)); }
catch(Exception e) { System.out.println("There is not an index 2"); }
System.out.println("The length of this linked list is: " + head.length()); */
System.out.println("\nLab 6 tests: ");
final NumberNode tenToForty = new NumberNode(10, new NumberNode(20, new NumberNode(30, new NumberNode(40))));
final NumberNode justFive = new NumberNode(5);
final IntegerFunction doubling = new IntegerFunction() {
public int apply(int i) { return i*2; }
};
final IntegerFunction squaring = new IntegerFunction() {
public int apply(int i) { return i*i; }
};
final IntegerBinaryFunction addition = new IntegerBinaryFunction() {
public int apply(int i, int j) { return i+j; }
};
final IntegerBinaryFunction multiplication = new IntegerBinaryFunction() {
public int apply(int i, int j) { return i*j; }
};
System.out.print("***********\nOriginal list values:\ntenToForty: ");
printList(tenToForty);
System.out.print("justFive: ");
printList(justFive);
System.out.print("\nMapping <double> onto tenToForty, expect [20, 40, 60, 80]: \nGot: ");
printList(tenToForty.map(doubling));
System.out.println("Original is... (Should be unchanged) ");
printList(tenToForty);
System.out.print("\nMapping <squaring> onto tenToForty, expect [100, 400, 900, 1600]: \nGot: ");
printList(tenToForty.map(squaring));
System.out.println("Original is... (Should be unchanged) ");
printList(tenToForty);
System.out.print("\nMapping <double> onto justFive, expect [10]: \nGot: ");
printList(justFive.map(doubling));
System.out.println("Original is... (Should be unchanged) ");
printList(justFive);
System.out.print("\nMapping <squaring> onto justFive, expect [25]: \nGot: ");
printList(justFive.map(squaring));
System.out.println("Original is... (Should be unchanged) ");
printList(justFive);
System.out.println("\nReduce tenToForty with (+, 0), expecting 100.\nGot: "
+ tenToForty.reduce(addition, 0));
System.out.println("Original is... (Should be unchanged) ");
printList(tenToForty);
System.out.println("\nReduce tenToForty with (*, 1), expecting 240,000.\nGot: "
+ tenToForty.reduce(multiplication, 1));
System.out.println("Original is... (Should be unchanged) ");
printList(tenToForty);
System.out.println("\nReduce justFive with (*, 2), expecting 10.\nGot: "
+ justFive.reduce(multiplication, 2));
System.out.println("Original is... (Should be unchanged) ");
printList(justFive);
System.out.println("\nReduce justFive with (+, 8), expecting 13.\nGot: "
+ justFive.reduce(addition, 8));
System.out.println("Original is... (Should be unchanged) ");
printList(justFive);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment