To gain practice using linked lists.
For this lab, you will extend the LinkedList class with a couple of new methods.
int size()
- this method should return the number of elements in the list.boolean contains(Type item)
- this method should check if the given item is present in the list or not.Type get(int index)
- this method should return the element at a given index. You can ignore the possibility
of the index being out of bounds.You can use the following main method to test your code:
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 = 'A'; c <= 'Z'; c++) {
letters.append(c);
}
// test the size
System.out.println("There are " + letters.size() + " letters.");
// test contains
System.out.println("B: " + letters.contains('B'));
System.out.println("X: " + letters.contains('X'));
System.out.println("*: " + letters.contains('*'));
// test get
System.out.println("4: " + letters.get(4));
System.out.println("15: " + letters.get(15));
System.out.println("24: " + letters.get(24));
}
}
The correct output for this lab, when you are finished, should be:
There are 26 letters. B: true X: true *: false 4: E 15: P 24: Y
When you are finished, please upload your code to the Canvas page for this assignment. Please send the .java file(s) only.
Copyright © 2022 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.