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 Linked Lists in Java using Iteration

Overview:

This laboratory helps you gain more experience with the use of lists and pointers.


Introduction

Some parts of this lab build upon the previous session on Simple Lists. However, for variety, this lab stores strings within a node, rather than integers.

This lab involves working with lists of data, using an existing driver program which actually constructs the lists. In particular, class ListStringNode defines a node with string data, and class NameList contains methods and a menu-driven program to maintain a list of names.

As written, the program contains functions to insert a name on the list (before a designated node), to delete a specified name from the list, and to print the names on the list.

  1. Copy ListStringNode and NameList to a lists package within Eclipse, compile it, and run it several times to discover just what the program does.

As you work with NameList, for each new method created, you will need to add an option to the print menu, expand the menu processing (adding additional case options, and create the new method itself.


Work Started in Class

  1. Write the details for a countList () method which counts the number of items in the list.

    To perform this task, you will want to move along the list item-by-item, counting the items as you go.

  2. Write the details for a method printLast () which should print the data for the last item on the list. (If the list is null, the procedure should print a message to that effect instead.)

    To perform this task, proceed iteratively, moving along the list item-by-item until coming to the end, where the next field is NULL .

  3. Write a method countOccurances (String str) that counts the number of times that the string str appears on the list.

    Test your program to see if it works for different cases such as a null list.

  4. Add a method getIndex that fines the first index of the string str in a list. As parameter, it should take a string to look for, and it should produce index, an integer as a result, where str is located at the indexth position on the list, counting the first node as position 1.

    For example, consider the following list:

    list with four nodes
    • getIndex ("study CS") would return 1
    • getIndex ("Read email") would return 3
    • getIndex ("not here") would return 0, as a signal that the string is not present on the list.
  5. Write the details for putFirst ( ) method which reads the name of an item on the list and moves that item to the front of the list.

    To perform this task, you first have to read in the name of the item desired. Then you need to search the list item-by-item to find the desired item. Next you need to remove that given item from its current place in the list (if it is not already first). Finally you need to insert that given item at the beginning of the list.

    Since manipulation of lists is most efficient if only references are manipulated, your program should neither create new nodes nor dispose of existing ones.

    In effect, you will be reassigning several references. Consider a list where the item "sought" appears somewhere in the middle of the list, as shown below.

    list before putFirst call

    After putFirst runs, the various pointers will be reassigned as follows.

    list after putFirst call

    Your search loop must account for the need to manipulate the next field of the node preceding the entry containing "sought". (Alas, there is no previous field in our list node structure.)

    Finally, your program should respond appropriately in all cases. In particular, putFirst should print appropriate messages if either list is null or the item designated is not found on the list.



created 4 August 2011 by David Cowden and Dilan Ustek
revised 4 August 2011 by David Cowden and Dilan Ustek
edited 17 November 2011 by Dilan Ustek
additional editing 17 November 2011 by Henry M. Walker
reorganized and rethought 9 February 2014 by Henry M. Walker
readings added 19 September 2014 by Henry M. Walker
reformatted with minor editing 27 October 2016 by Henry M. Walker
revised for recusive algorithms only 27-28 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.