Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Overview For this homework we will be creating a SortedLinkedList class to pract

ID: 3831026 • Letter: O

Question

Overview

For this homework we will be creating a SortedLinkedList class to practice pointer manipulation. To simplify our problem, we will only store string values in our linked list class. Use the following class diagrams as a basis for your linked list. Your node class should be an inner-class of your SortedLinkedList class:

add() Method

Whenever the add() method is called the new string value is inserted into a location of the list where each of the elements are in sorted order.

For example: if I added the following string values from left to right:

"banana", "apple" "pear", and "lemon"

then the linked list would look like the following:

Add "banana":

banana -> null

Add "apple":

apple -> banana -> null

Add "pear":

apple -> banana -> pear -> null

Add "lemon":

apple -> banana -> lemon -> pear -> null

Notice how the list is always sorted after each call to add().

Driver Class

Part 1:

Write a simple test class that instantiates a SortedLinkedList and adds the following string values, from left to right:

“orange”, “grape fruit”, “apple”, “strawberry”, “cantaloupe”, “lemon”, “tangerine”, “banana”, “grape”, “pear”, “raisin”

Then, call your display() method to verify that your list is sorted correctly.

Part2:

Test your remove() method and then call your display() method to verify that your list nodes are correctly removed. Test removing the first, last and middle node.

Explanation / Answer

This is SortedLinkedList class having display() method

package com.LinkedList;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;

public class SortedLinkedList {

public void display(LinkedList al){
   System.out.println("SortedList as: ");
   Collections.sort(al);
   Iterator<String> itr=al.iterator();
   while(itr.hasNext()){
   System.out.print(itr.next()+" --> ");
   }
}
}

This is Driver Class only run this class

package com.LinkedList;

import java.util.LinkedList;

public class TestDriver {

   public static void main(String[] args) {
       // TODO Auto-generated method stub

       //This is part 1
       SortedLinkedList sll=new SortedLinkedList();
       LinkedList<String> al=new LinkedList<String>();
       al.add("orange");
       al.add("grape fruit");
       al.add("apple");
       al.add("strawberry");
       al.add("cantaloupe");
       al.add("lemon");
       al.add("tangerine");
       al.add("banana");
       al.add("grape");
       al.add("pear");
       al.add("raisin");
      
       System.out.println("Part1:");
       sll.display(al);
         
       //This is part 2
       System.out.println(" Part2:");
       System.out.println(" After removing the first, last and middle node");
       al.remove("apple");
       al.remove("lemon");
       al.remove("tangerine");
      
       sll.display(al);
      
   }

}

Output:

Part1:
SortedList as:
apple --> banana --> cantaloupe --> grape --> grape fruit --> lemon --> orange --> pear --> raisin --> strawberry --> tangerine -->

Part2:

After removing the first, last and middle node
SortedList as:
banana --> cantaloupe --> grape --> grape fruit --> orange --> pear --> raisin --> strawberry -->