CMPU 102 - Lab 5

$Revision: 1.1 $

In this lab you will implement a singly-linked list class.  In particular, you will implement methods to add elements to the beginning and end of the list, and for traversing the list.

Getting Started

From the Labs section of the class web page, download lab5.zip and import it into your Eclipse workspace.

Your Task

Your task is to implement the methods defined in the StringList class.  The comment for each method explains what each method is supposed to do.

addToHead

The addToHead method adds a new element to the head (beginning) of the linked list.  Before you start writing code, draw sketches of the following two scenarios:

  1. Adding a new element to an empty list

  2. Adding a new element to a nonempty list

Each sketch should both the "before" and "after" pictures of the linked list.  For example, your sketch of the first scenario (adding an element to the head of an empty list) should look something like this:

You should plan the code to implement the addToHead method on paper.  Make sure your code correctly takes the linked list structure from the "Before" state to the "After" state!

addToTail

The addToTail method adds a value to the tail (end) of the linked list.  Again, before you write code, sketch the before and after pictures of the same two scenarios (adding to an empty list and adding to a nonempty list).

applyToAll

The applyToAll method takes a StringFunctor object and applies (using its apply method) it to each element in the linked list.  To implement this method, you will need to write a loop that visits each node in the linked list.  Before you start to write code for this method, sketch a nonempty list like this:

Plan your code by defining a loop variable (the current node being visited), the condition under which the loop should continue executing, and how to update the loop variable following each iteration of the loop.  Write down the method on paper and make sure it works correctly on the example list.

applyToAllPairs

The applyToAllPairs method is like applyToAll, except that it applies a functor to all pairs of elements in the list: the first and second, second and third, third and fourth, etc.

Again, sketch a nonempty list and write your code on paper first.  Make sure it works on your example list.

Testing Your Work

You can use the main method of the TestStringList class to test your work.  Right click on "TestStringList.java" and choose "Run As->Java Application".

When You Are Done

Show an instructor or lab coach that the TestStringList program operates correctly using your StringList class.

Submitting: from a terminal window run the commands

cd
cd eclipse-workspace
submit102 lab5