The solution contains multiple java files. The initial linked list is 10 20 30 40 . The modified list is 20 30 40
Step-by-step explanation
//Define the interface SimpleList;
public interface SimpleList
{
public Object get (int index);
public void set (int index, Object element);
public void add (int index, Object element);
public Object remove(int index);
public int size();
}
SimpleLinkedList.java:
//Define the class SimpleLinkedList that implements the SimpleList.
public class SimpleLinkedList implements SimpleList
{
//Define the Item class.
class Item
{
Object value;
Item next;
Item (Object setValue, Item setNext)
{
value = setValue;
next = setNext;
}
}
protected Item start;
protected int currentSize;
//Define the default constructor.
public SimpleLinkedList()
{
currentSize = 0;
start = null;
}
//Define the parameterized constructor.
SimpleLinkedList(Object[] values)
{
currentSize = 0;
//Start the loop and call the add method.
for(int i = 0; i<values.length;i++)
{
add(i, values[i]);
}
}
//Define the method to return the object
// at the given index.
atOverride
public Object get (int index)
{
//Call the getItem() method and return it's value.
return getItem(index).value;
}
//Define the set() method.
atOverride
public void set (int index, Object element)
{
if (index < 0 || index >= currentSize)
{
return;
}
int currentIndex = 0;
for (Item current = start; current != null; current = current.next)
{
if (currentIndex == index)
{
current.value = element;
break;
}
currentIndex++;
}
}
//Define the getItem() method.
protected Item getItem(int index)
{
if (index < 0 || index >= currentSize)
{
return null;
}
int currentIndex = 0;
for (Item current = start; current != null; current = current.next)
{
if (currentIndex == index)
{
return current;
}
currentIndex++;
}
return null;
}
//Define the add() method.
atOverride
public void add (int index, Object toAdd)
{
if (index == 0)
{
start = new Item (toAdd, start);
currentSize++;
return;
}
Item previousItem = getItem(index - 1);
if(previousItem == null)
{
return;
}
Item newItem = new Item(toAdd, previousItem.next);
previousItem.next = newItem;
currentSize++;
}
//Define the method to return the size of the linked list.
atOverride
public int size ()
{
return currentSize;
}
//Define the method to remove an index from the linked list.
atOverride
public Object remove (int index) {
// TODO Auto-generated method stub
return null;
}
}
YourSimpleLinkedList.java:
//Define the YourSimpleLinkedList class.
public class YourSimpleLinkedList extends SimpleLinkedList
{
//Define the default constructor.s
public YourSimpleLinkedList()
{
super();
}
//Define the parameterized constructor.
YourSimpleLinkedList(Object[] values)
{
super(values);
}
//Define the method to remove an index.
at Override
public Object remove(int index)
{
//Return null if the index is out of range.
if (index < 0 || index >= currentSize)
{
return null;
}
//Set the start Item if the first value
// is to be removed.
if (index == 0)
{
//Store the value of the node to be removed.
Object temp = start.value;
//Set the start Item.
start = start.next;
//Update the size of the linked list and return
// the deleted value.
currentSize--;
return temp;
}
//Initialize the required variables.
int currentIndex = 0;
Item current = start;
Item prev = start;
//Start the loop to traverse the list.
while (current != null)
{
//Check the index value.
if (currentIndex == index)
{
//Store the value to be deleted.
Object temp = current.value;
//Set the next pointer.
prev.next = current.next;
//Update the size of the linked list and return
// the deleted value.
currentSize--;
return temp;
}
//Update the values.
currentIndex++;
prev = current;
current = current.next;
}
return null;
}
}
Main.java:
public class Main
{
public static void main(String[] args)
{
//Create a list of objects.
Object [] values = {10, 20, 30, 40};
//Create an object of the YourSimpleLinkedList class.
YourSimpleLinkedList myList = new YourSimpleLinkedList(values);
//Display the initial list.
System.out.print("The initial linked list is ");
for (int i=0;i<myList.size();i++)
{
System.out.print(myList.get(i) + " ");
}
//Remove an index from the list.
myList.remove(0);
//Display the modified list.
System.out.println();
System.out.print("The modified list is ");
for (int i=0;i<myList.size();i++)
{
System.out.print(myList.get(i) + " ");
}
}
}