Question: I need the main method to test The chain then is never empty and so the head reference is never null, even when the

I need the main method to test " The chain then is never empty and so the head reference is never null, even when the list is empty" for this program:

public class LinkedList implements ListInterface

{

//Member variables declaration

private Node firstNode;

private int numberOfEntries;

//Constructor

public LinkedList()

{

// create a dummy node

@SuppressWarnings("unchecked")

T dummyEntry =(T)"0";

Node dummyNode=new Node(dummyEntry);

firstNode=dummyNode;

clear();

}//End constructor

//Method definition

public final void clear()

{

//Initialize o to variable numberofentries

numberOfEntries = 0;

}//End method

//Method definition

public void add(T newEntry)

{

Node newNode = new Node(newEntry);

Node lastNode = getNodeAt(numberOfEntries);

lastNode.setNextNode(newNode);

numberOfEntries++;

}//End method

//Method definition

public T remove(int givenPosition)

{

T result = null;

if((givenPosition>= 1)&&(givenPosition <=

numberOfEntries))

{

assert !isEmpty();

Node nodeBefore = getNodeAt(givenPosition - 1);

Node nodeToRemove = nodeBefore.getNextNode();

Node nodeAfter = nodeToRemove.getNextNode();

nodeBefore.setNextNode(nodeAfter);

result = nodeToRemove.getData();

numberOfEntries--;

}

return result;

}//End method

//Method definition

public boolean isEmpty()

{

boolean result;

if (numberOfEntries == 0)

{

assert firstNode == null;

result = true;

}

else

{

assert firstNode != null;

result = false;

} // end if

return result;

}//End method

//Method definition

private Node getNodeAt(int givenPosition)

{

assert !isEmpty() && (0 <= givenPosition) &&

(givenPosition <= numberOfEntries);

Node currentNode = firstNode;

for (int counter = 0; counter < givenPosition;

counter++)

currentNode = currentNode.getNextNode();

assert currentNode != null;

return currentNode;

}//End method

//Class definition

private class Node

{

private T data;

private Node next;

//Constructor with one parameter

private Node(T dataPortion)

{

this(dataPortion, null);

}//End constructor

//Constructor with two parameters

private Node(T dataPortion, Node nextNode)

{

data = dataPortion;

next = nextNode;

}//End constructor

//Method definition

private T getData()

{

return data;

} //End method

//Method definition

private void setData(T newData)

{

data = newData;

}//End method

//Method definition

private Node getNextNode()

{

return next;

} //End method

//Method definition

private void setNextNode(Node nextNode)

{

next = nextNode;

}//End method

}//End Node class

//Method definition

public boolean replace(int givenPosition,

T newEntry)

{

boolean isSuccessful = true;

if ((givenPosition >= 1) && (givenPosition <= numberOfEntries))

{

assert !isEmpty();

Node desiredNode = getNodeAt(givenPosition);

desiredNode.setData(newEntry);

}

else isSuccessful = false;

return isSuccessful;

}//End method

//Method definition

public T getEntry(int givenPosition)

{

T result = null; // result to return

if ((givenPosition >= 1) && (givenPosition

<= numberOfEntries))

{

assert !isEmpty();

result = getNodeAt(givenPosition).getData();

} // end if

return result;

}//End method

//Method definition

public T[] toArray()

{

@SuppressWarnings("unchecked")

T[] result = (T[])new Object[numberOfEntries];

int index = 0;

Node currentNode = firstNode;

while ((index < numberOfEntries) &&

(currentNode != null))

{

result[index] = currentNode.getData();

currentNode = currentNode.getNextNode();

index++;

} // end while

return result;

}//End method

//Method definition

public boolean contains(T anEntry)

{

boolean found = false;

Node currentNode = firstNode;

while (!found && (currentNode != null))

{

if (anEntry.equals(currentNode.getData()))

found = true;

else currentNode =

currentNode.getNextNode();

} // end while

return found;

}//End method

//Method definition

public boolean add(int newPosition, T newEntry)

// OutOfMemoryError possible

{

boolean isSuccessful = true;

if ((newPosition >= 1) && (newPosition <=

numberOfEntries + 1))

{

Node newNode = new Node(newEntry);

Node nodeBefore = getNodeAt

(newPosition - 1);

Node nodeAfter = nodeBefore.getNextNode();

newNode.setNextNode(nodeAfter);

nodeBefore.setNextNode(newNode);

numberOfEntries++;

}

else isSuccessful = false;

return isSuccessful;

}//End method

//Method definition

public int getLength()

{

return numberOfEntries;

}//End method public static void main(String[] args) { System.out.println("Create an empty list."); ListInterface myList = new LinkedList<>(); System.out.println("List should be empty; isEmpty returns " + myList.isEmpty() + "."); System.out.println("Testing add method to end:"); myList.add(1, "30"); myList.add(2, "40"); myList.add(3, "58"); myList.add(1, "00"); myList.add(5, "60"); myList.add(2, "10"); System.out.println("List should contain 00 10 30 40 58 60."); System.out.println("The list have "); int numberOfEntries = myList.getLength(); for (int position = 1; position <= numberOfEntries; position++) {System.out.print(myList.getEntry(position) + " ");} System.out.println("List shouldn't be empty; isEmpty returns " + myList.isEmpty() + "."); System.out.println("Clear the list"); myList.clear(); System.out.println("Is my list empty?" + myList.isEmpty()); System.out.println("The list have " + myList.getLength()); } // end main

}//End class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!