On the Code ~
Copy Code code as follows:
Package ncu.com.app.chatpter_5;
Import Java.util.Random;
Node class
Class Node {
Object data;
Node Next;
}
Action class
Class listnode{
public Node i;
public int size;
Public ListNode () {
i = null;
size = 0;
}
public void Insertnode (Object node) {
Node No = new node ();
no.data = node;
No.next = A;
i = no;
size++;
}
public void DisPlay () {
if (size==0) {
SYSTEM.OUT.PRINTLN ("Linked list is empty");
}
Node Currnode = i;
while (Currnode!=null) {
System.out.print (currnode.data+ ",");
Currnode = Currnode.next;
}
System.out.println ("");
}
Delete I node
public void Delect (int i) {
if (i<=size) {
for (int m=0;m<i;m++) {
i = First.next;
size--;
DisPlay ();
}
}
}
Clear the linked list
public void Delectall () {
size = 0;
i = null;
DisPlay ();
}
Get the data from the list in I-j
public void GetNode (int i,int j) {
for (int m=0;m<i-1;m++) {
i = First.next;
}
Node Currnode = i;
for (int m=0;m<j-i+1;m++) {
System.out.print (currnode.data+ ",");
Currnode = Currnode.next;
}
}
}
public class Nodetree {
public static void Main (String args[]) {
ListNode ListNode = new ListNode ();
for (int i = 0;i<10;i++) {
int k = new Random (). Nextint (10);
Listnode.insertnode (k);
System.out.print (+ + ",");
}
System.out.println ("");
Listnode.display ();
Listnode.delect (10);
Listnode.delectall ();
Listnode.getnode (2,8);
}
}