Skip to content

Instantly share code, notes, and snippets.

@klaeufer
Last active April 22, 2020 20:51
Show Gist options
  • Save klaeufer/7a73dd5de4005da7d5ac5009aaaa3b2d to your computer and use it in GitHub Desktop.
Save klaeufer/7a73dd5de4005da7d5ac5009aaaa3b2d to your computer and use it in GitHub Desktop.
Loyola COMP 271 Sample JShell History
class BTNode<E> {
public E data;
public BTNode<E> left;
public BTNode<E> right;
public BTNode(final E data, final BTNode<E> left, final BTNode<E> right) {
if (data == null) throw new IllegalArgumentException("data is null");
this.data = data;
this.left = left;
this.right = right;
}
public BTNode(final E data) { this(data, null, null); }
public String toString() {
final StringBuilder result = new StringBuilder();
result.append("BTNode@");
result.append(hashCode());
result.append("(");
result.append(data);
result.append(", ");
result.append(left != null ? "BTNode@" + left.hashCode() : ".");
result.append(", ");
result.append(right != null ? "BTNode@" + right.hashCode() : ".");
result.append(")");
return result.toString();
}
}
var empty = null
BTNode<Integer> empty = null
var leaf = new BTNode<>(9)
var leaf2 = new BTNode<>(9)
new BTNode<Integer>(1)
new BTNode<Integer>(2)
new BTNode<Integer>(3)
/list
var x = 5
7
$5
$6
var tree = $5
tree
tree.left = $4
tree.left = $6
tree.right = $7
tree
tree.left
tree.right
<E> int size(final BTNode<E> root) {
if (root == null) {
return 0;
} else {
return 1 + size(root.left) + size(root.right);
}
}
size(null)
size($7)
size(tree)
<E> int size(final BTNode<E> root) {
if (root == null) {
return 0;
} else {
int mySize = 1;
final leftSize = size(root.left);
final rightSize = size(root.right);
return mySize + leftSize + rightSize;
}
}
<E> int size(final BTNode<E> root) {
if (root == null) {
return 0;
} else {
final int mySize = 1;
final int leftSize = size(root.left);
final int rightSize = size(root.right);
return mySize + leftSize + rightSize;
}
}
size(null)
size(tree)
<E> void printTree(final BTNode<E> root) {
if (root != null) {
printTree(root.left);
System.out.println(root.data + " ");
printTree(root.right);
}
}
printTree(null)
printTree($7)
printTree(tree)
/save -history lab10.jsh
assert false
assert true
class Node<E> {
public E data;
public Node<E> next;
public Node(final E data, final Node<E> next) {
if (data == null) throw new IllegalArgumentException("data is null");
this.data = data;
this.next = next;
}
public Node(final E data) { this(data, null); }
public String toString() {
return "Node@" + hashCode() + "(" + data +
(next != null ? ", Node@" + next.hashCode() + ")" : ")");
}
}
var n1 = new Node<String>("oiu")
var n2 = new Node<Integer>(77)
n1.next = n2;
var n3 = new Node<String>("hello")
n1.next = n3;
n1.data
n1.next
n3.next
n1.next
n3
var n4 = new Node("hello")
Node<String> n4 = new Node<>("hello")
n3
n4
n1.next
n3.next = n4
n1.next.next
n1.next.next.data = "world"
n4
var curr
var curr = n1
n1
n2
n2
n1
n3
n4
curr
n1
curr
while (curr != null) {
System.println(curr.data);
curr = curr.next;
}
while (curr != null) {
System.out.println(curr.data);
curr = curr.next;
}
n4.next = n1
n1
n3
n4
curr = n1
while (curr != null) {
System.out.println(curr.data);
curr = curr.next;
}
while (curr != null) {
System.out.println(curr.data);
curr = curr.next;
}
curr
n4
n1
n4.next = null
while (curr != null) {
System.out.println(curr.data);
curr = curr.next;
}
curr = n1
while (curr != null) {
System.out.println(curr.data);
curr = curr.next;
}
n1
n3
n4
n3.next = null
n1
n3
var n2 = new Node<String>("what")
n2.next = n4
n1
n3
n1.next = n2
n3
n1
n2
n3
n4
n2 = n3
n1
n2
<E> void printList(final Node<E> n) {
if (n == null) {
System.out.println();
} else {
<E> void printList(final Node<E> n) {
Node<E> curr = n;
while (curr != null) {
System.out.print(curr.data);
System.out.print(" ");
curr = curr.next;
}
System.out.println();
}
printList(n1)
printList(n2)
printList(n1)
var curr = n1
while (curr != null) {
curr = curr.next;
}
curr.next = new Node<String>("up")
var curr = n1
while (curr.next != null) {
curr = curr.next;
}
curr.next = new Node<String>("up")
printList(n1)
<E> void addToList(final Node<E> head, final E element) {
Node<E> curr = head;
while (curr.next != null) {
curr.next;
}
curr.next = new Node<E>(element);
}
<E> void addToList(final Node<E> head, final E element) {
Node<E> curr = head;
while (curr.next != null) {
curr = curr.next;
}
curr.next = new Node<E>(element);
}
printList(n1)
addToList(n1, "today")
printList(n1)
n4
n2
n2
printList(n2)
addToList(n2, "there")
printList(n2)
Node<String> n5 = null;
addToList(n5, "there")
<E> void addToList(final Node<E> head, final E element) {
<E> void addToList2(final Node<E> head, final E element) {
if (head == null) {
head = new Node<E>(element);
} else {
addToList(head, element);
}
}
<E> Node<E> addToList2(final Node<E> head, final E element) {
if (head == null) {
return new Node<E>(element);
} else {
addToList(head, element);
return head;
}
}
n5 = addToList2(n5, "there")
addToList2(n1, "there")
printList(n5)
printList(n1)
/save -history myjshhistory.jsh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment