CS 112
Spring 2018

Old version

This is the CS 112 site as it appeared on May 11, 2018.

Lab 7: Methods that operate on linked lists

FLR 267

If your lab meets in FLR 267, you should begin by following the instructions found here.

Task 1: Trace a recursive linked-list method

Your work for this task should go on the piece of paper that we give you. Please show your paper to a staff member before you leave the lab.

In lecture, we have continued to work with linked lists of characters, where each node in the linked list is an instance of the following class:

public class StringNode {
    private char ch;
    private StringNode next;
    ...
}

One of the methods in the StringNode class is the following recursive method called numOccur:

public static int numOccur(StringNode str, char ch) {
    if (str == null) {
        return 0;
    }

    int numInRest = numOccur(str.next, ch);
    if (str.ch == ch) {
        return 1 + numInRest;
    } else {
        return numInRest;
    }
}

It finds the number of occurrences of the character ch in the linked list to which str refers.

Considered the following linked list, which represents the string "java":

Trace the following call of the numOccur() method on this linked list:

int count = numOccur(s, 'a');

which we assume is being made from the main() method in the StringNode class.

You should begin with something like the following at the bottom of the page:

Note that we have included the stack frame for main(), and above it the stack frame for the initial call to numOccur(). (We’ve omitted the parameter ch from that stack frame, because its value ('a') doesn’t change over time.

To complete the trace, you should:

Task 2: Convert a recursive method to an iterative one

To begin this task:

Rewrite the numOccur() method that we traced in Task 1. Remove the existing recursive implementation of the method, and replace it with one that uses iteration instead.

You should make use of the template for iteratively traversing a linked list that we considered in lecture:

StringNode trav = str;    // make trav point to the first node
while (trav != null) {
    // do something here
    trav = trav.next;
}

To test your iterative version of the method, compile the class, and try tests like this one from the Interactions Pane:

> StringNode s = StringNode.convert("java");
> StringNode.numOccur(s, 'a')
2

Task 3: Trace an iterative linked-list method

The StringNode class includes another method called convert that uses iteration to gradually convert a Java String object to a linked-list string:

public static StringNode convert(String s) {
    if (s.length() == 0) {
        return null;
    }

    StringNode firstNode = new StringNode(s.charAt(0), null);
    StringNode prevNode = firstNode;
    StringNode nextNode;

    for (int i = 1; i < s.length(); i++) {
        nextNode = new StringNode(s.charAt(i), null);
        prevNode.next = nextNode;
        prevNode = nextNode;
    }

    return firstNode;
}

We used this method in our testing for Task 2.

Note that the method uses several variables of type StringNode:

Let’s trace through the execution of the following call of this method:

StringNode s1 = StringNode.convert("node");

As we trace through it on the board, add the diagram to the paper that you used in Task 1.

Task 4: Convert an iterative method to a recursive one

Now rewrite the convert() method. Remove the existing iterative implementation of the method, and replace it with one that uses recursion instead.

Your new method should be much simpler! It should take advantage of our usual recursive approach to processing strings – both Java String objects, and strings that are represented as linked lists:

To test your recursive version of the method, compile the file, and try tests like this one from the Interactions Pane:

> StringNode s = StringNode.convert("node");
> System.out.println(s);
node

(Note: We are able to see the string when we print it because our StringNode class includes a toString() method.)

Task 5: Submit your work

You should show the paper with your work on Tasks 1 and 3 to the teaching assistant.

You should use Apollo to submit the following file:

Don’t worry if you didn’t finish all of the tasks. You should just submit whatever work you were able to complete during lab.

Warnings

  • Make sure to use these exact file names, or Apollo will not accept your files. If Apollo reports that a file does not have the correct name, you should rename the file using the name listed in the assignment or on the Apollo upload page.

  • Make sure that you do not try to submit a .class file or a file with a ~ character at the end of its name.

  • Before submitting your files, make sure that your BU username is visible at the top of the Apollo page. If you don’t see your username, click the Log out button and login again.

  • If you make any last-minute changes to one of your Java files (e.g., adding additional comments), you should compile and run the file in DrJava after you make the changes to ensure that it still runs correctly. Even seemingly minor changes can cause your code to become unrunnable.

  • If you encounter problems with Apollo, close your browser and try again. If possible, you may also want to wait an hour or two before retrying. If you are unable to submit and it is close to the deadline, email your homework before the deadline to cs112-staff@cs.bu.edu

Here are the steps:

  1. Login to Apollo, using the link in the left-hand navigation bar. You will need to use your Kerberos user name and password.
  2. Check to see that your BU username is at the top of the Apollo page. If it isn’t, click the Log out button and login again.
  3. Find the appropriate lab section on the main page and click Upload files.
  4. For each file that you want to submit, find the matching upload section for the file. Make sure that you use the right section for each file. You may upload any number of files at a time.
  5. Click the Upload button at the bottom of the page.
  6. Review the upload results. If Apollo reports any issues, return to the upload page by clicking the link at the top of the results page, and try the upload again, per Apollo’s advice.
  7. Once all of your files have been successfully uploaded, return to the upload page by clicking the link at the top of the results page. The upload page will show you when you uploaded each file, and it will give you a way to view or download the uploaded file. Click on the link for each file so that you can ensure that you submitted the correct file.