Question: I need to test this program but I'm confused how to test it. Adding nodes to or removing nodes from a linked chain requires a

I need to test this program but I'm confused how to test it.

Adding nodes to or removing nodes from a linked chain requires a special case when the operation is at the beginning of the chain. To eliminate this special case, you can add a dummy node at the beginning of the chain. the dummy node is always present but does not contain a list entry. The chain then is never empty and so the head reference is never null, even when the list is empty. Modify the class LList as presented in that chapter, by adding the dummy node to the chain. Test your implementation.

import java.util.*;

/** A linked implementation of the ADT list. */ public class LList implements ListInterface { private Node firstNode; // Reference to first node of chain private int numberOfEntries;

public LList() { //create a dummy node @SuppressWarnings("unchecked") T dummyEntry = (T)"0"; Node dummyNode = new Node (dummyEntry); firstNode = dummyNode; clear(); } // end default constructor

// Clear method public final void clear() { //Set numberOfEntries equal to 0 numberOfEntries = 0; } // end clear //Add method public void add(T newEntry) { Node newNode = new Node(newEntry); if (isEmpty()) { firstNode = newNode; } else // Add to end of nonempty list { Node lastNode = getNodeAt(numberOfEntries); lastNode.setNextNode(newNode); // Make last node reference new node } // end if numberOfEntries++; } // end add public T remove (int givenPosition) { T result = null; if((givenPosition >= 1) && (givenPosition <= numberOfEntries)) { assert !isEmpty(); Node beforeNode = getNodeAt(givenPosition-1); Node removeNode = beforeNode.getNextNode(); Node afterNode = removeNode.getNextNode(); beforeNode.setNextNode(afterNode); result = removeNode.getData(); numberOfEntries--; } return result; }//end remove method public boolean isEmpty() { boolean result; if(numberOfEntries == 0) { assert firstNode == null; result = true; } else { assert firstNode != null; result = false; }//end if return result; } // Initializes the classs data fields to indicate an empty list.

private Node getNodeAt(int givenPosition) { assert !isEmpty() && (0 <= givenPosition) && (givenPosition <= numberOfEntries); Node currentNode = firstNode; for (int count = 0; count < givenPosition; count++) currentNode = currentNode.getNextNode(); assert currentNode != null; return currentNode; } // end getNodeAt

private class Node // Private inner class { private T data; private Node next; //Contructor private Node(T dataPortion) { this(dataPortion, null); }//end contructor private Node(T dataPortion, Node nextNode) { data = dataPortion; next = nextNode; }//end contructor private T getData() { return data; }//ennd getData method private void setData(T newData) { data = newData; }//end setData method private Node getNextNode() { return next; }// end getNextNode method private Node setNextNode(Node nextNode) { next = nextNode; }// end setNextNode method } // end Node public boolean replace (int givenPosition, T newEntry) { boolean successful = true; if((givenPosition >= 1) && (givenPosition <= numberOfEntries)) { assert !isEmpty(); Node nthNode = getNodeAt(givenPosition); nthNode.setData(newEntry); } else {successful =false;} return successful; }//end replace method public T getEntry(int givenPosition) { T result = null; if ((givenPosition >=1)&&(givenPosition <= numberOfEntries)) { assert !isEmpty(); result = getNodeAt(givenPosition).getData(); }//end if return result; }// end getEntry metod public T[] toArray() { 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 toArray method 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 contain method public boolean add(int newPosition, T newEntry)// the possibility of OutOfMemory Error { boolean successful = true; if ((newPosition >= 1)&& (newPosition <= numberOfEntries +1)) { Node newNode = new Node(newEntry); Node beforeNode = getNodeAt (newPosition-1); Node afterNode = beforeNode.getNextNode(); newNode.setNextNode(afterNode); beforeNode.setNextNode(newNode); numberOfEntries++; } else successful = false; return successful; }// end add method public int getLength() { return numberOfEntries; }// end getLength method } // end LList

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!