Fundamentals of Computer Science II (CSC-152 98S)

[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Labs] [Assignments] [Examples] [Bailey Docs] [SamR Docs] [Tutorial] [API]


Lab 12: Making Doubly-Linked Lists

Held: Monday, March 30, 1998 and Tuesday, March 31, 1998.

Overview: In this lab, you will convert an implementation of singly-linked lists to an implementation of doubly-linked lists. You will start by observing the behavior of the implementation of singly-linked lists and then will add and test methods one-by-one.

Purpose: The primary goal of this assignment is to strengthen your understanding of the implementation and use of linked lists.

Notes: This laboratory is in a somewhat different form than previous laboratories. In particular, while previous laboratories have been somewhat open-ended, this lab has more step-by-step details. I'd be interested in learning which form you prefer and why.

You may also note that I've tried to format the Java code. Let me know if you think this is a help or a hindrance.

Finally, I've set this lab up as a form. You need not use the form, but you may certainly fill in the various areas and submit it to me. I would like to know whether you consider such form-based labs useful.

Required Code Files:

Library Files:

Experiments

Name:
Email:

Experiment 1: Preparation

Step 1. In this lab, you'll be working with an implementation of linked lists. In my design of linked lists, I've chosen to design a generic List interface and then have a LinkedList class implement that interface. To handle different choices in the implementation, I've chosen to allow each function to throw a number of exceptions. In particular, most functions can throw

Scan the documentation for the List interface and note any questions you might have about it. I'll be available to answer questions.

Step 2. Make copies of LinkedList.java and LinkedListTester.java. Compile the two programs and execute LinkedListTester. By this point, you should be familiar with testing programs. Determine which, if any, functions are implemented and unimplemented and write them here.

Step 3. As you know, good testing relies an understanding of what the various methods should do. Pick a few methods you'd like to test and write down a series of commands to test them appropriately. For example, you might decide to check what happens if you advance the current reference beyond the end of the list.

Step 4. Run your test and jot down any notes here.

Experiment 2: Preparing to add "previous" links

You are now ready to begin converting your singly-linked list into a doubly-linked list. In particular, you will need to (1) create (or use) a previous link in each node in the linked list, (2) modify functions to support that link, and (3) add functions that use that link. We'll begin by considering what needs to be done.

Step 1. To ease your development, I've already included a previous link in my BinaryNode class. However, I have not used that link in my LinkedList class. Read through the documentation for the BinaryNode class and note which methods you will need to use in order to access the previous link.

Step 2. Why are we adding a previous link? Because it allows us to provide a much richer list library. For example, if we include a previous link, we can support a constant-time deleteLast method (simply move back back to the previous node, delete the old back, and update the next link of the new back to be null). Read through the LinkedList code and make a list of functions that the previous link can help support.

Step 3. Obviously, adding previous links requires modification to a number of functions in order to support the links. For example, deleteFirst will need to set the previous link of the new first element to null. Read through the LinkedList code and note which functions you expect to need to change and how you might change them.

Step 4. The simplest method that uses the previous link is backup, which move the current reference back one element in the list. Write that method and put the code here.

Step 5. In order to make sure that your new functions work correctly and that your modifications to the old functions work correctly, it is helpful to design an appropriate testing strategy. Sketch the strategy you would use to test each of your changes.

Experiment 3: Updating Methods

You are now ready to begin updating methods to support the previous link. In this part of the laboratory, you will update each method.

Step 1. Update addAtFront to correctly handle the previous link. When the list is empty, it's likely that you won't have to make any changes. When the list is nonempty, you'll need to make sure that the previous link of the old front refers to the new front.

Compile and test the updated code and write your test and the results of the test here.

Step 2. Update addAtEnd to correctly handle the previous link. In particular, you will need to set the previous link of the new back element.

Compile and test your updated code and write your test and the results of the test here.

Step 3. Update deleteFirst to correctly handle the previous link. In particular, you will need to ensure that the previous element of the new first element is null. Make sure that you correctly handle deletion in a one-element list.

Compile and test your updated code and write your test and the results of the test here.

Step 4. Update addAfterCurrent to correctly handle the previous link. In particular, you will need to set the previous link of the newly added element to the current element. You will also need to update the previous link of the element that comes after the newly added element. Make sure to worry about what happens if the current element is the last element!

Compile and test your updated code and write your test and the results of the test here.

Step 3.

Experiment 4: Adding Methods

Step 1. Develop appropriate code for deleteLast, which deletes the last element of the list. You will probably want to model this on deleteFirst. To delete the last element, you can set the back to the previous element of the back element and then update the next reference of the back element to be null. You will also need to return the contents of the deleted node.

Make sure to handle the special cases of the empty list (in which case you should throw an exception) and the one-element list (in which case you need to be careful about updating references). Make sure to do something reasonable if the current reference refers to the old last element.

Write your code here before putting it in the program.

Step 2. Compile and test your updated code and write your test and the results of the test here.

Step 3. Develop appropriate code for addBeforeCurrent which inserts an element before the current element. You may want to model this on addAfterCurrent. For addBeforeCurrent you will need to create the new node and set its previous and next links. You will also need to make the previous link of the current node refer to this new node and to make the next link of the old previous node refer to this new node. Don't forget to return the contents of the deleted node.

Make sure to concern yourself with special cases, such as when the list is empty or has only one element or when the current reference is at the front of the list.

Write your code here before putting it in the program.

Step 4. Compile and test your updated code and write your test and the results of the test here.

Step 5. Develop appropriate code for deleteCurrent, which deletes the current element of the list. As there is no corresponding function already implemented, you cannot model this on another function. For the general case, deleteCurrent needs to (1) make the next link of the previous node refer to the next node of the current node and (2) make the previous link of the next node refer to the previous node of the current node. In addition, deleteCurrent needs to move the cursor to an appropriate location in the list.

Make sure you worry about the special cases. These include the empty list, the single-element list, and lists in which the cursor is at the beginning or end of the list.

Write your code here before putting it in the program.

Step 6.

Compile and test your updated code and write your test and the results of the test here.

Finishing Up

Read over your answers and, if you think it's appropriate to do so, click on the button below.


[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Labs] [Assignments] [Examples] [Bailey Docs] [SamR Docs] [Tutorial] [API]

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

Source text last modified Tue Mar 31 08:24:01 1998.

This page generated on Tue Mar 31 13:58:01 1998 by SiteWeaver.

Contact our webmaster at rebelsky@math.grin.edu