Home CPSC 340

Linked List Exercise

 

Objective

To gain practice using linked lists.


 

Task

For this lab, you will extend the LinkedList class with a method that will return the number of elements in the list. You could do this by keeping track of the items in the list with an instance variable, but instead you should do it by counting the elements in the list one by one.


 

Details


 

Testing

You can use the following main method to test your code:


// ListMain.java

public class ListMain {
    public static void main(String args[]) {
        // make a linked list of the letters A-Z
        LinkedList<Character> letters = new LinkedList<Character>();
        for (char c = 'Z'; c >= 'A'; c--) {
            letters.add(c);
        }

        // print them out, and the count
        letters.print();
        System.out.println("There are " + letters.count() + " letters in the alphabet."); 
    }
}

It should print that there are 26 items in the list.


 

Submitting

When you're finished, email your edited LinkedList.java file to ifinlay@umw.edu.

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