Introductory Laboratory on Linked Lists


Introduction to Singly-Linked Lists

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

Rearranging List Items

The previous lab filled in two parts of the program ~walker/152/java-examples/OurSinglyLinkedList.java. For the first part of this lab, you are to fill in the remaining menu option. Specifically, the putFirst (String item) method should take the name of an item on the list and moves that item to the front of the list.

Aside: The following discussion reflects a fairly common circumstance in list processing. Considerable space is taken in what follows to describe what must be done during processing to move a item to the front of a list. The actual coding, however, takes just a very few lines.

To understand how the desired rearrangement of a list might occur, consider the following linked list, represented using the box-and-pointer diagrams of CSC 151:

Linked List Example

Of course, if we want to focus on the apple node, then no further processing is needed, since that node is already first.

Processing is more interesting if we want to move the banana node to the front. We consider the steps needed to change the pointers, so that the banana node will be considered the head of the list -- as in the following diagram:

Linked List Example

In order to effect this change, we must change three pointer fields:

To begin this work, we first must search the original list to find the item to be moved (e.g., the banana node). Note that in the changed list, the next of the previous node (e.g., the grape node) will have to change. Thus, in searching for the desired node, we also must keep track of the node just before the desired node (which we will call previous). Searching the list to find the desired item then results in the following diagram:

Linked List Example

From this picture, we now can change the various next fields, so the banana node will be considered at the head. Work proceeds in three steps, as outlined previously:

  1. Change the next field of the grape node:

    Linked List Example

  2. Change the next field of the banana node:

    Linked List Example

  3. Change the head field to the banana node to get the desired new list.

Steps for this part of the Lab

  1. Review the above process. To what extent can the above changes to the pointers be done in another order?
  2. Using the above discussion as a guide, write the details for putFirst (String item) which takes as parameter the name of an item on the list and moves that item to the front of the list. (For efficiency, you should implement this operation directly -- you should not first call the remove method and then add, although the details of your putFirst method may parallel much of the code in these other methods.)

    Of course, your method putFirst should leave the list unchanged if either the list is null or the item designated is not found on the list.

Maintaining Ordered Lists

One important type of linked list arises when we want to keep the data in order. For example, numerical data may be in ascending order, or string data may be in alphabetical order. Of course, for such ordered lists, a putFirst method could not be allowed. However, most other methods from our OurSinglyLinkedList can be used directly.

  1. Make a copy of OurSinglyLinkedList.java, calling the result OurOrderedList.java, and removing the putFirst method.

  2. To establish an ordered, singly-linked list, the only change needed will be for insertion. Rather than insert a node at the start of a list, one will need to locate where the data should be placed to maintain ordering, and then the node will have to be inserted at that place.

    Revise the insert method, so a list of strings will be kept in ascending order. If the list is null or if the new item should go at the start of the list, then head will have to be changed. Otherwise, proceed in these steps:
    1. traverse the list to find where to put the new node, keeping track of the previous node,
    2. create a new node and insert the string into the data field,
    3. set the next field of the new node to the next node in the original list,
    4. set the next field of the previous node to indicate the new node.


  3. Review the above steps for your new insert method. To what extent can these steps be done in another order in order to accomplish a correction insertion?


This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~walker/courses/152.sp01/lab-more-lists.html

created March 12, 2001
last revised March 29, 2001
Henry Walker (walker@cs.grinnell.edu)