CS 261 University of Puget Sound Spring, 2020
 
Computer Science II
Abstract Data Types and their Implementations, Some Basic Algorithms,
Object-oriented Problem Solving, and Efficiency
 

Warning: This course is under development. Although the basic structure of this course is largely established, nothing on this Web site should be considered official or even possibly correct.
DO NOT MAKE PLANS BASED ON THE CONTENTS OF THIS SITE UNTIL JANUARY, 2020.

Laboratory Exercise on Simple Lists in Java

Overview

This lab applies ideas of box-and-pointer representations and provides practice using lists with Java.


Work Started in Class

Background: Simple Lists

The reading for this lab describes box-and-pointer diagrams as a graphical model for lists.

  1. Draw box-and-pointer diagrams for each of the following lists:

    ((x) y z)
    (x (y z))
    ((a) b (c ()))
    

Copy classes ListNode.java and SimpleList.java to Eclipse and run SimpleList. This program will serve as the basis for the remaining steps of this lab. Be sure to ask about any sections you do not understand.

  1. Add a function second to this program that returns the data stored in the second element in a list (if present) or an empty string if the list is null or has only one element.

    In this exercise and the subsequent ones,

    • the function returns the required result (in this case, of type int), and the function prints nothing. To test the function, you might print a returned value in main, but not in the function.
    • you will want to add lines to main to test your methods.)
  2. Add a function count which counts how many times a specified item appears on a list. count should have two parameters: the list (of type listNode) and the desired item (of type int) for the search. Of course, if the item is not on the list, the function should return a count 0. In writing this code, you should use an iterative approach.

    Be sure to test your code to see if it works for a null list and that it works for lists of various lengths.


Homework

  1. Add a function last which returns the last item on the list. (If the list is null, the function should return the empty string.)

    Be sure to test your function to see if it works for a null list, for a list with only one element, and for a list which has more than one element.

  2. Add a function getIndex that finds the first index of the integer number in a list.

    • In counting indices, the index of the first item on the list should be 0, as with arrays.
    • If the item is not found on the list, then -1 should be returned.

    The function should use as parameters a pointer to the list and a number to look for. It should produce index, an integer.

    Test cases should include circumstances in which:

    • There is a null list.
    • number does not appear on the list.
    • number appears once in the list.
    • numberappears more than once in the list.


created 4 May 2000 by Henry m. Walker
revised 4 August 2011 by David Cowden and Dilan Ustek
revised 17 April 2012 by by Henry M. Walker>
reorganized and expanded 9 February 2014 by by Henry M. Walker
readings added 19 September 2014 by Henry M. Walker
functions clarified 22 April 2018 by Henry M. Walker
converted from C to Java 20-26 January 2020 by Henry M. Walker
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.