public class LLInArrayNode implements Comparable<LLInArrayNode> { private int NU
ID: 3720819 • Letter: P
Question
public class LLInArrayNode implements Comparable<LLInArrayNode>
{
private int NUL = -1;
private String value;
private int next;
public LLInArrayNode() { } public LLInArrayNode(LLInArrayNode clone)
{
this.value = clone.value; this.next = clone.next;
}
public LLInArrayNode(String value)
{
this.value = value; this.next = NUL;
}
public LLInArrayNode(String value,int next)
{
this.value = value; this.next = next;
}
public int compareTo(LLInArrayNode other)
{
// calls the String compareToIgnoreCase() method return this.value.compareToIgnoreCase(other.value); }
public String getValue() { return value; }
public void setValue(String value) { this.value = value; }
public int getNext() { return next; }
public void setNext(int next) { this.next = next; }
public String toString() { return "Value = " + value + " " + " next " + next; }
}
import java.util.Scanner;
public class LinkedListInArray
{
static Scanner scan = new Scanner(System.in);
protected static final int NUL = -1;
// End of list symbol //*** debugging statements are left in and do not have 3 stars protected static LLInArrayNode[] nodeArray = new LLInArrayNode[1000]; //*** Array of AListNode holds the linked list
//*** This array is static, this one array is used by all //*** instances of the class protected int list = NUL; //*** Reference to the first node on the list protected static int free; //*** Reference to the first node on the free list private static boolean nodeArrayIsInitialized = false; //*** the boolean nodeArrayIsInitialized is set to true when the //*** first class instance is created. private void initializeStaticArray()
{
//*** fill array with nodes
for(int x = 0; x < nodeArray.length; x++)
{ nodeArray[x] = new LLInArrayNode(); }
//*** connect each node to its successor
for (int index = 1; index < nodeArray.length; index++)
{ nodeArray[index - 1].setNext(index); }
//*** the last node has its link set to NUL
nodeArray[ nodeArray.length - 1].setNext(NUL);
//*** the pointer free is set to the first node of the array
free = 0;
//*** this next line guarantees that the nodeArray is
//*** initialized only once.
nodeArrayIsInitialized = true; }
public LinkedListInArray()
{
if(!nodeArrayIsInitialized) initializeStaticArray();
}
public void read()
{
boolean moreNodes = true;
while(moreNodes) { System.out.println("Please enter a string." );
String s = scan.nextLine();
//*** a new node is assigned by the method getNode(); int newNodeIndex = getNode(); //*** the new node is given values nodeArray[newNodeIndex].setValue(s); nodeArray[newNodeIndex].setNext(NUL);
//System.out.println("new node is " + nodeArray[newNodeIndex]);
this.addTerm(newNodeIndex);
System.out.println("Are there more Strings? Y/N ");
String choice = scan.nextLine();
if(!choice.equalsIgnoreCase("y"))
{ moreNodes = false; //System.out.println("moreNodes " + moreNodes); } } }
private void addTerm(int term) {//*** add a term to this list. It is stored alphabetically //System.out.println("in addTerm"); int previous = NUL;
int location = this.list;
while(location != NUL) { // System.out.println("nodeArray[location].getValue() " + nodeArray[location].getValue());
// System.out.println("nodeArray[term].getValue() " + nodeArray[term].getValue()); if(nodeArray[location].getValue().compareTo(nodeArray[term].getValue()) > 0) { previous = location; location = nodeArray[location].getNext(); }
else
break;
}
if(previous == NUL)//*** insert as first node { nodeArray[term].setNext(this.list); this.list = term; } else {//*** location and previous have been set. Add in the middle of list nodeArray[term].setNext(location); nodeArray[previous].setNext(term); } //System.out.println("end of addTerm"); }
protected int getNode() //*** Returns the index of the next available node from the free list //*** and updates the free list index
{ int hold; hold = free;
free = nodeArray[free].getNext();
return hold; }
protected void freeNode(int index)
// Frees the node at array position index by linking it into the
// free list
{
nodeArray[index].setNext(free); free = index;
}
public String toString()
{
//System.out.println("in toString");
String output = "";
int temp = this.list;
while(temp != NUL)
{
//System.out.println("in while loop");
output = output + nodeArray[temp].getValue() + " "; temp = nodeArray[temp].getNext();
}
return output; }
}
ere is feStore. ublic class can guide you in the fourth implementation. It creates an array of nodes an inkedistInArraypolynomial implements PolynomialInterface private static final int NUL-1; Reference to the first node on the free private static int free private Static LLInArrayPolyNode[] nodeArray new LLInArrayPolyNode[10001; private String polystring,; te static boolean nodeArrayIsInitialized false; private int public LinkedListInArrayPolynomial() polynomialNUL; Reference to the first node on the list if(! initializeStaticArray() public LinkedListInArrayPolynomial(String polystring) ?f( ! nodeArrayIs Initialized) initializeStaticArray(); this.polystring polyStrings storePolynomial); private void initializestaticArray() // fill array with nodes for(intx nodeArray. length; x*+) nodeArray[x] - new LLInArrayPolyNode(); for (int index 1; indexExplanation / Answer
public LinkedListInArrayPolynomial add(LinkedListInArrayPolynomial b) {
LinkedListInArrayPolynomial result;
result = nodeArray.length > b.nodeArray.length ? this : b; // intitialize result to the list of greater size
int len = nodeArray.length > b.nodeArray.length ? b.nodeArray.length : nodeArray.length; // find the max size among the two lists using conditional operator
boolean flag = len == nodeArray.length; // this flag is true if the the current list has greater size than b
if(flag) {
for(int i=0; i<len; i++) {
result.nodeArray[i].setData(result.nodeArray[i].getData()+ nodeArray[i].data);
}
} else {
for(int i=0; i<len; i++) {
result.nodeArray[i].setData(result.nodeArray[i].getData()+ b.nodeArray[i].getData());
}
}
return result;
}
public LinkedListInArrayPolynomial subtract(LinkedListInArrayPolynomial b) {
LinkedListInArrayPolynomial result;
result = nodeArray.length > b.nodeArray.length ? this : b;
int len = nodeArray.length > b.nodeArray.length ? b.nodeArray.length : nodeArray.length;
boolean flag = len == nodeArray.length;
if(flag) {
for(int i=0; i<len; i++) {
result.nodeArray[i].setData(result.nodeArray[i].getData()- nodeArray[i].data);
}
} else {
for(int i=0; i<len; i++) {
result.nodeArray[i].setData(result.nodeArray[i].getData() - b.nodeArray[i].getData());
}
}
return result;
}
You need to provide the implementation of other required classes to get more accurate result;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.