Home CPSC 340

Linked List Exercise



To gain practice using linked lists.



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.





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--) {

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

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



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.