Using Java create a program that does the following: Modify theLinkedList1 class by adding sort() and reverse() methods. Thereverse method reverses the order of the elements in the list, andthe sort method rearranges the elements in the list so they aresorted in alphabetical order. Do not use recursion to implementeither of these operations. Extend the graphical interface in theLinkedList1Demo class to support sort and reverse commands, and useit to test the new methods. This should have two separate sourcefiles. LinkedList1 Class and LinkedList1Demo.
Include these modifications:
- Read in the names from an input file.
- Instead of using a graphical interface, create a menu methodand use it to interface with the user.
- Include menu options to add, remove and find names as well asto sort and reverse the entire contents of the list.
- The program should execute until the user enters \"exit.\"
LinkedList1 class:
/**
The LinkedList1 class implements a Linked list.
*/
class LinkedList1
{
/**
The Node class stores a list element
and a reference to the next node.
*/
 Â
private class Node
{
String value; Â Â
Node next;
 Â
/**
Constructor.
@param val The element to store in the node.
@param n The reference to the successor node.
*/
 Â
Node(String val, Node n)
{
value = val;
next = n;
}
 Â
/**
Constructor.
@param val The element to store in the node.
*/
 Â
Node(String val)
{
// Call the other (sister) constructor.
this(val, null);
}
} Â Â
private Node first; // list head
private Node last; // last element in list
/**
Constructor.
*/
 Â
public LinkedList1()
{
first = null;
last = null;
}
 Â
/**
The isEmpty method checks to see
if the list is empty.
@return true if list is empty,
false otherwise.
*/
 Â
public boolean isEmpty()
{
return first == null; Â Â
}
 Â
/**
The size method returns the length of the list.
@return The number of elements in the list.
*/
 Â
public int size()
{
int count = 0;
Node p = first; Â Â
while (p != null)
{
// There is an element at p
count ++;
p = p.next;
}
return count;
}
 Â
/**
The add method adds an element to
the end of the list.
@param e The value to add to the
end of the list. Â Â
*/
 Â
public void add(String e)
{
if (isEmpty())
{
first = new Node(e);
last = first;
}
else
{
// Add to end of existing list
last.next = new Node(e);
last = last.next;
}
}
 Â
/**
The add method adds an element at a position.
@param e The element to add to the list.
@param index The position at which to add
the element.
@exception IndexOutOfBoundsException When
index is out of bounds.
*/
 Â
public void add(int index, String e)
{
if (index < 0 || index > size())
{
String message = String.valueOf(index);
throw new IndexOutOfBoundsException(message);
}
// Index is at least 0
if (index == 0)
{
// New element goes at beginning
first = new Node(e, first);
if (last == null)
last = first;
return;
}
// Set a reference pred to point to the node that
// will be the predecessor of the new node
Node pred = first;
for (int k = 1; k <= index - 1; k++)
{
pred = pred.next; Â Â
}
// Splice in a node containing the new element
pred.next = new Node(e, pred.next);
// Is there a new last element ?
if (pred.next.next == null)
last = pred.next; Â Â
}
 Â
/**
The toString method computes the string
representation of the list.
@return The string form of the list.
*/
 Â
public String toString()
{
StringBuilder strBuilder = new StringBuilder();
 Â
// Use p to walk down the linked list
Node p = first;
while (p != null)
{
strBuilder.append(p.value + \"\n\");
p = p.next;
}
return strBuilder.toString();
}
 Â
/**
The remove method removes the element at an index.
@param index The index of the element to remove.
@return The element removed.
@exception IndexOutOfBoundsException When index is
out of bounds. Â Â
*/
 Â
public String remove(int index)
{
if (index < 0 || index >= size())
{
String message = String.valueOf(index);
throw new IndexOutOfBoundsException(message);
}
String element; // The element to return  Â
if (index == 0)
{
// Removal of first item in the list
element = first.value;
first = first.next;
if (first == null)
last = null; Â Â
}
else
{
// To remove an element other than the first,
// find the predecessor of the element to
// be removed.
Node pred = first;
 Â
// Move pred forward index - 1 times
for (int k = 1; k <= index -1; k++)
pred = pred.next;
 Â
// Store the value to return
element = pred.next.value;
 Â
// Route link around the node to be removed
pred.next = pred.next.next;
 Â
// Check if pred is now last
if (pred.next == null)
last = pred;
}
return element;
}
 Â
/**
The remove method removes an element.
@param element The element to remove.
@return true if the remove succeeded,
false otherwise.
*/
 Â
public boolean remove(String element)
{
if (isEmpty())
return false;
 Â
if (element.equals(first.value))
{
// Removal of first item in the list
first = first.next;
if (first == null)
last = null; Â Â
return true;
}
 Â
// Find the predecessor of the element to remove
Node pred = first;
while (pred.next != null &&
!pred.next.value.equals(element))
{
pred = pred.next;
}
// pred.next == null OR pred.next.value is element
if (pred.next == null)
return false;
 Â
// pred.next.value is element
pred.next = pred.next.next;
 Â
// Check if pred is now last
if (pred.next == null)
last = pred;
 Â
return true; Â Â
}
 Â
public static void main(String [] args)
{
LinkedList1 ll = new LinkedList1();
ll.add(\"Amy\");
ll.add(\"Bob\");
ll.add(0, \"Al\");
ll.add(2, \"Beth\");
ll.add(4, \"Carol\");
System.out.println(\"The members of the list are:\");
System.out.print(ll);
}
}
Program Output:
The members of the list are:
Al
Amy
Beth
Bob
Carol