The implementation example of Java one-way linked list _java

Source: Internet
Author: User
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);
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.