Here is a code provided by the professor for the Second one which will guide you
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() + "\n"; temp = nodeArray[temp].getNext();
}
return output; }
}
DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma
Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t
Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th
1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of
1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of