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
{
//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
}//End class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
