Home CPSC 340

Doubly Linked List Methods

 

Objective

To add some helpful methods to our doubly linked list class.


 

Task

For this lab, you will add some additional methods to our doubly linked list. Start with the DoubleList.java file, and add the following:


 

Testing

You can use the following main program to test your methods:


public class Lab6Main {
    public static void main(String args[]) {
        DoubleList<String> names = new DoubleList<String>();
        names.addEnd("Arthur");
        names.addEnd("Beatrice");
        names.addEnd("Charlie");
        names.addEnd("Daniella");
        names.addEnd("Eustace");

        System.out.println("The list:");
        names.printForwards();

        System.out.println("~~~");
        System.out.println("Slot 2 = " + names.get(2));

        System.out.println("~~~");
        System.out.println("After removing the first node:");
        names.removeFirst();
        names.printForwards();

        System.out.println("~~~");
        System.out.println("After removing the last node:");
        names.removeLast();
        names.printForwards();

        System.out.println("~~~");
        System.out.println("After Clearing:");
        names.clear();
        names.printForwards();
    }
}

The output of this program should look like the following:

The list:
Arthur
Beatrice
Charlie
Daniella
Eustace
~~
Slot 2 = Charlie
~~
After removing the first node:
Beatrice
Charlie
Daniella
Eustace
~~
After removing the last node:
Beatrice
Charlie
Daniella
~~
After Clearing:

 

Submitting

When you're finished, email the updated DoubleList.java file to ifinlay@umw.edu.

Copyright © 2019 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.