The diagram above shows that an Item consists of two data attributes: a String a
ID: 3759295 • Letter: T
Question
The diagram above shows that an Item consists of two data attributes: a String and an integer: An Info contains a Node and an integer. The Node class is implemented as an internal or private class in Info. This is shown above by the circle with the lines.
Note that the toArray method has now been replaced by the toString method.
Info must be implemented as a singly linked list using the internal Node class. The variable list is a reference to the first node in the list and numNodes hold the current count of the nodes. Using another implementation including the Java Collections Framework is not acceptable.
Test your code using your Junit tests. Make changes/improvements to the tests as needed.
Explanation / Answer
Class : Info.java
import java.util.LinkedList;
public class Info
{
LinkedList<Node> list=new LinkedList<Node>();
private int numNodes;
Info()
{
}
public void insert(Item i)
{
Node n=new Node(i);
list.add(n);
}
public void delete(Item i)
{
int j=0;
for(Node n:list)
{
if(n.data.equals(i))
{
break;
}
j++;
}
list.remove(j);
}
public Item get(int index)
{
return list.get(index).data;
}
public int count()
{
numNodes=list.size();
return numNodes;
}
public Info copy(int i, int j)
{
list.add(i,this.list.get(i));
return this;
}
public Info merge(Info in)
{
for(Node n:in.list)
{
this.list.addLast(n);
}
return this;
}
public int indexOf(Item i)
{
return list.indexOf(i);
}
public String toString()
{
return "Total Elements in List : "+numNodes;
}
private class Node
{
Item data;
Node next;
Node(Item i)
{
data=i;
}
}
}
Class : Item.java
public class Item
{
private int val;
private String str;
public int compareTo(Item i)
{
if(this.val==i.val)
{
return i.val;
}
else
{
return -1;
}
}
public String toString()
{
return str;
}
}