Java linked list creation

Source: Internet
Author: User

Java has no pointer, but it can achieve the same effect as the pointer.

//// // Node // class Node {public Double X; public node next; Public node pre; node (Double X, node N) {This. X = x; this. pre = NULL; this. next = N;} node (Double X) {This. X = x;} public node getnext () {return next;} public void setnext (node next) {This. next = next;} public node getpre () {return pre;} public void setpre (node pre) {This. pre = pre;}/* Public node (Double X, node next) {This. X = x; this. next = Next ;} * //} // Linked List Implementation //////////////////// /// class list {public node head, tail; Public int length; List () {head = tail = NULL;} public Boolean isempty () // determines whether it is null {return head = NULL ;} public void addhead (Double X) // you can call {head = new node (x, null) once to add a head node. // PT. setnext (head. getnext (); // head. setnext (PT); If (tail = NULL) tail = head;} public void addtail (Double X) // Add nodes {If (! Isempty () {tail. next = new node (x); tail = tail. next;} else {head = tail = new node (x) ;}} public void print () {for (node temp = head; temp! = NULL; temp = temp. next) {system. out. print (temp. X + "");} system. out. println ("");} public void addnow (Double X, Double Y) // insert a node {for (node temp = head; temp! = NULL; temp = temp. next) {If (temp. X = x) {node now = temp. next; node Ne = new node (y); temp. next = ne; ne. next = now ;}}}} //////////////////////////////////////// // public class test {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stublist K = new list (); K. addtail (1); K. addtail (2); K. addtail (4); K. addtail (5); K. addnow (2, 3); // after 2, insert the node K as 3. print ();}}

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.