In Java The methods should work as addNode It has to take a
In Java....
The methods should work as: add(Node) It has to take a new Node and add that to the next address of previous Node. findNode(Node) It has to take a node and checks if the node is in the list. size() It should return the size of the NodeList.
public class NodeList {
private int size = 0;
private Node root = null;
/*
* It has to take a new Node and add that to the next address of previous Node.
* If the list is empty, assign it as the \"root\"
* @Param - Node
*/
public void add(Node node) {
// Implement this method!!!
}
/*
* It has to return the size of the NodeList
*
* @return size
*/
public int size() {
// Implement this method!!!
}
/*
* It has to take a Node and checks if the node is in the list.
* If it finds the node, it returns true, otherwise false
*
* @param - Node
* @return boolean true/false
*/
public boolean findNode(Node node){
// Implement this method!!!
}
}
Solution
public class NodeList {
private int size = 0;
private Node root = null;
/*
* It has to take a new Node and add that to the next address of previous Node.
* If the list is empty, assign it as the \"root\"
* @Param - Node
*/
public void add(Node node) {
// Implement this method!!!
if(node==null)
return;
if(root == null)
root=node;
else{
Node temp = root;
while(temp.next!=null){
temp = temp.next;
}
temp.next = node;
}
}
/*
* It has to return the size of the NodeList
*
* @return size
*/
public int size() {
// Implement this method!!!
int count =0;
if(root==null)
return count;
else{
Node temp = root;
while(temp!=null){
temp= temp.next;
count++;
}
}
return count;
}
/*
* It has to take a Node and checks if the node is in the list.
* If it finds the node, it returns true, otherwise false
*
* @param - Node
* @return boolean true/false
*/
public boolean findNode(Node node){
// Implement this method!!!
boolean flag = false;
if(root == null)
return false;
else{
Node temp = root;
while(temp!=null){
if(temp==node){
flag = true;
break;
}
}
return flag;
}
}
}
also find the defination of class Node
class Node{
int data;
Node next;
}


