Setup
Register for the lab using grinch
: https://grinch.caltech.edu/register and clone the repository as explained in the setup instructions.
Part 0
Trace through each of the following lines of code carefully to determine what memory looks like after each line.
To do this, follow these steps:
- Evaluate the right-hand side as a value.
- Evaluate the left-hand side by either declaring a new variable or “looking up” the value of an existing variable.
- Assign (i.e., draw an arrow from) the LHS to the RHS.
Code Snippet #1
L1: Node<Integer>
first
=
new Node<>(10)
;
L2: Node<Integer>
temp
=
first
.
next
;
Task 1.
Answer each of the questions in answers.txt
related to code snippet #1 by replacing the TODO
with your answers.
Part 1
Assuming L1 and L2 have already been executed, trace through each of the following lines of code carefully to determine what memory looks like after each line.
Code Snippet #2
L3: first
.
next
=
new Node<>(100)
;
L4: first
.
next
.
next
=
temp
;
Task 2.
Answer each of the questions in answers.txt
related to code snippet #2 by replacing the TODO
with your answers.
Part 2
Assuming L1-L4 have already been executed, trace through the following loop in the same way as above.
Code Snippet #3
for (int i = 1; i < 5; i++) {
Node<Integer> temp2 = first.next;
first.next = new Node<>((i+1)*100);
first.next.next = temp2;
}
Task 3.
Answer each of the questions in answers.txt
related to code snippet #3 by replacing the TODO
with your answers.
Part 3
Consider the following recursive function:
private static <E> void addToEnd(Node<E> curr, Node<E> node) {
if (curr == null) {
curr = node;
}
else {
addToEnd(curr.next, node);
}
}
Assuming everything so far has already been executed, trace through the execution of the following method call.
Code Snippet #4
addToEnd(first, new Node<>(0));
Task 4.
Answer each of the questions in answers.txt
related to code snippet #4 by replacing the TODO
with your answers.
Part 4
Assuming everything so far has already been executed, trace through the following loop in the same way as above.
Code Snippet #5
Node<Integer> curr = first.next;
while (curr != first) {
if (curr.next == null) {
curr.next = first;
}
curr = curr.next;
}
Task 5.
Answer each of the questions in answers.txt
related to code snippet #5 by replacing the TODO
with your answers.