QuestionQuestion

Task 5.1
Today’s lab will help directly with the assignment.
In this lab, you will implement a class called LinkedList.java. The class should use a generic (LinkedList<T>) to constrain the types that can be put into the linked list. This class should have the following attributes and methods that implement a linked list:
• a head and tail reference to the beginning and end of the linked list
• a constructor that initialises an empty linked list
• isEmpty() that returns true if there are no elements in the list and false otherwise.
Your linked list should also have the following methods:
• T elementAt (i): Returns the element at position i. The first position in the list is 0.
If the ith position does not exist, a NoSuchElementException is thrown.
• void insertAt (int i, T e): Inserts element e into the ith position of the linked list. If the ith position does not exist, a NoSuchElementException is thrown. Note for an empty list, you can insert at position 0. For a list of one element, you can insert at position 1. etc.
Implement this linked list inside LinkedList.java. Create a Main.java class with a main method to test your linked list. Create link lists of type String and Integer. Print out the elements to the screen to demonstrate that your methods work.
Task 5.2
In your main method, write the following java code:
LinkedList < String > strs = new LinkedList < String > ();
LinkedList < Integer > ints = new LinkedList < Integer > ();
ints . insertAt (0 , ‘‘ Hi there . ’ ’ );
Please explain why this code does not compile.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

/**
*
*
*/
public class Main {

    public static void main(String[] args) {
       /**
         * Create link lists of type String. Print out the elements
         * to the screen to demonstrate that your methods work.
         */
       LinkedList<String> stringList = new LinkedList<>();
      
       System.out.println(stringList.isEmpty());
      
       // add item to list
       String [] items = {"I", "have", "a", "dream"};
       for (int i = 0; i < items.length; i++) {
            String item = items[i];
            stringList.insertAt(i, item);            
       }
      
       // show item from the list
      
       for (int i = 0; i < items.length; i++) {
            System.out.print(stringList.elementAt(i) + " ");
       }
       System.out.println("");
       System.out.println...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$15.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Computer Science - Other Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats