package TestTwo.demo1;
public class Node {
int data;
Node next;
public Node(int data){
this.data = data;
}
public Node append(Node node) {
Node currentNode = this;
while (true) {
if (currentNode.next == null) {
break;
}
currentNode = currentNode.next;
}
currentNode.next = node;
return currentNode.next;
}
public boolean isLast(){
return this.next==null;
}
public int getData() {
return data;
}
public void deleteNode(){
if (this.next==null){
System.out.println("Null Pointer!");
}else {
this.next = this.next.next;
}
}
public Node getNext() {
return next;
}
public void show(){
Node currentNode = this;
while (true){
System.out.println(currentNode.getData()+" ");
currentNode = currentNode.next;
if (currentNode==null){
break;
}
}
System.out.println();
}
public void insert(Node node2){
if (this.next != null) {
node2.next = this.next;
}
this.next = node2;
}
}
package TestTwo.demo1.test;
import TestTwo.demo1.Node;
public class TestNode {
public static void main(String[] args) {
Node node1 = new Node(1);
Node node2 = new Node(2);
Node node3 = new Node(3);
node1.append(node2).append(node3).append(new Node(4));
System.out.println(node1.getNext().getNext().isLast());
System.out.println(node1.getNext().getNext().getNext().getData());
node3.deleteNode();
System.out.println(node1.getNext().getNext().isLast());
node3.deleteNode();
node1.show();
node3.insert(new Node(5));
node3.show();
}
}