138k views
4 votes
In this homework problem we’ll begin completing an implementation of SimpleList that uses a linked list of Item objects internally. We’ve provided some starter code in a class called SimpleLinkedList. However, the get and set methods rely on a helper function called getItem to work properly, which is not properly implemented yet. You should create a class called YourSimpleLinkedList that inherits from SimpleLinkedList and provide a correct implementation of getItem. getItem should return a reference to the Item at that position in the list, or null if no such item exists. You may want to look at the implementation of size in SimpleLinkedList for a reminder of how to walk a linked list using a for loop. You will also need to provide a constructor for YourSimpleLinkedList that takes an array of Object references and simply passes them to a call to super to properly initialize SimpleLinkedList. A portion of the implementation of SimpleLinkedList follows:

User Zoubiock
by
3.3k points

1 Answer

4 votes

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) + " ");

}

}

}

User Obiwanjacobi
by
3.7k points