Need Some With The NodeLIst Class The Output Should LIke This Length : 4 Length
ID: 3673027 • Letter: N
Question
Need Some With The NodeLIst Class
The Output Should LIke This
Length : 4
Length : 3
Length : 3
package com.list;
public class NodeList {
private int size = 0;
private Node root = null;;
/*
* It has to take a new Node and add that to the address og previous Node.
*
* @Param - Node
*/
public void add(Node node) {
}
/*
* It has to return the size of the NodeList
*
* @return size
*/
public int size() {
return size;
}
/*
* It has to take a Node and remove that node if you find it in the list
* from the existing nodes otherwise dont do anything.
*
* @Param - Node
*/
public void remove(Node node) {
}
}
--------------------------------------------------
package com.list;
public class Main {
public static void main(String[] args) {
NodeList list = new NodeList();
Node node = new Node(1, "Book");
Node node2 = new Node(2, "Lappy");
list.add(node);
list.add(node2);
list.add(node);
list.add(node2);
System.out.println("Length : "+list.size());
list.remove(node);
System.out.println("Length : "+list.size());
list.remove(node2);
Node node3 = new Node(3, "Glass");
list.remove(node3);
System.out.println("Length : "+list.size());
}
}
----------------------------------------
package com.list;
public class Node {
private int id = 0;
private String name = "";
private Node node;
public Node(int id, String name) {
this.id = id;
this.name = name;
this.node = null;
}
public Node getNode() {
return node;
}
public void setNode(Node node) {
this.node = node;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "ID : "+this.id+" Name : "+this.name;
}
}
Explanation / Answer
package com.list;
public class NodeList {
private int size = 0;
private Node root = null;;
public void add(Node node) {
Node head,tail;
if(root = = NULL) {
head = tail = new Node();
head.node = node;
root = head.node;
head.next = tail;
tail = root;
}
else {
tail.next = new Node();
tail = tail.next;
tail.node = node;
}
}
public int size() {
if(root = = NULL && node.List()!= EMPTY)
return size;
else return NULL;
}
public void remove(Node node) {
temp = root;
if (root = = NULL)
return NULL;
else
{
remove(root);
root = node;
temp = node.next;
temp = root;
}
}
}
package com.list;
public class Main {
public static void main(String[] args) throws Exception {
NodeList list = new NodeList();
Node node = new Node(1, "Book");
Node node2 = new Node(2, "Lappy");
list.add(node);
list.add(node2);
System.out.println("Length : "+list.size());
list.remove(node);
System.out.println("Length : "+list.size());
list.remove(node2);
Node node3 = new Node(3, "Glass");
list.add(node3);
System.out.println(“Length :” + list.size());
list.remove(node3);
System.out.println("Length : "+list.size());
}
}
package com.list;
public class Node {
private int id = 0;
private String name = "";
private Node node;
public Node(int id, String name) {
this.id = id;
this.name = name;
this.node = null;
}
public Node getNode() {
return node;
}
public void setNode(Node node) {
this.node = node;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "ID : "+this.id+" Name : "+this.name;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.