Cs61b Experimental records (I) Lab2, Lab3

Lab2

Debug

  • step into

    Step into shows the literal next step of the program

  • step over

    Button allows us to complete a function call without showing the function

  • step out

    escape the function

  • Resume and return to this position

  • Conditional Breakpoints

    An even faster approach is to make our breakpoint conditional. To do this, right (or two-finger) click on the red breakpoint dot. Here, You can set a condition for when you want to stop. In the condition box, enter newTotal < 0, stop your program, And click “debug” again.

Implementing Destructive vs. Non-destructive Methods

  • dcatenate(IntList A, IntList B): returns a list consisting of all elements of A, followed by all elements of B. May modify A. To be completed by you.

    Error:

    Calling methods and properties on temporary object A operates on the original object, but assigning A value to A simply changes the direction of A

    Assigning B to A at the end only changes the direction of the temporary object, not the previous object.

    So we need to actually connect the last node of B and A, rather than assign to A temporary object

    To truly connect, you can’t simply assign B to A (as above).

    A=B;
    Copy the code

    Instead, you should access the real object by calling the properties of the temporary object, A, and then connect them

    A.rest=B;
    Copy the code

    So the most intuitive thing to do is

    Considering that A starts with null:

    Or, more succinctly, after the last node of A joins B, update their links one by one

    Attention! You can’t write that

    Instead of A, it returns a.test!

  • catenate(IntList A, IntList B): returns a list consisting of all elements of A, followed by all elements of B. May not modify A. To be completed by you.

    obviously:

Autograder results:

Lab3

Unidirectional list inversion

/** * Returns the reverse of the given IntList. * This method is destructive. If given null * as an input, returns null. */
public static IntList reverse(IntList A)
Copy the code

Your test should test at least the following three situations:

  • That the function returns a reversed list.
  • That the function is destructive, i.e. when it is done running, the list pointed to by A has been tampered with. You can use assertNotEquals. This is sort of a silly test.
  • That the method handles a null input properly.

The reverse method was called. I thought that not only would A reversed IntList be returned, but that the IntList that A pointed to should also be reversed.

But the reverse method was only required to return A reversed List, and the IntList pointed to by A was not required to be reversed.

All you really need to do is change the link between the two nodes.

Iterative solution:

The linked list is traversed from beginning to end, with x pointing to each node, and two auxiliary temporary objects, temp1 pointing to the previous node of X, temp2 to the next node of X, temp1 to the previous node of X. For each iteration, save X. est with temp2, point X. est to temp1, move TEMP1 back by one node (where X is), and move X to the next node as well. Moves to the last location to return a reference to the last node.

public static IntList reverse(IntList A){
        IntList temp1=null;
        IntList x=A;
        while(x! =null){
            IntList temp2=x.rest;
            x.rest=temp1;
            temp1=x;
            x=temp2;
        }
        return temp1;
    }
Copy the code

Recursive solution:

This problem is actually easier to use recursion, we just need to find the last node of the linked list, and then back, and change the direction of each node

public static IntList recursiveReverse(IntList A){
        if (A==null||A.rest==null)
            return A;
        IntList reversed=recursiveReverse(A.rest);
        A.rest.rest=A;
        A.rest=null;
        return reversed;
    }
Copy the code

Autograder results: