Java implementation of single linked list, bidirectional linked list _java

Source: Internet
Author: User

This example for you to share the Java implementation of a single linked list, two-way linked list of related code for your reference, the specific content as follows

Java implementation single linked list:

Package code;
 Class Node {node next;
 int data;
 public Node (int data) {this.data=data;
 Class Linklist {Node i;
 Head public linklist () {this.first=null;
  public void AddNode (Node no) {no.next=first;
  first=no;//add} public void Delectnode () {Node n=first.next in the header;
  First=null;
  first=n;//in head Delete}//delete specified location public int number () {int count=1;
  See how many element Node Nd=first is available;
   while (nd.next!=null) {nd=nd.next;
  count++;
 return count;
   public void delectexact (int n) {//delete specified position if (n>1) {int count=1;
   Node De=first;
    while (count<n-1) {de=de.next;
    
   count++;
  } De.next=de.next.next;
  
 else First=first.next;
   public void addexact (int n,node nd) {if (n>1)//Add the specified position {int count=1;
   Node De=first;
    while (count<n-1) {de=de.next;
    
   count++;
   } Nd.next=de.next;

  De.next=nd;
 else First=first.next; public int Findnode (int n) {int Count=1;//find a number corresponding to the location Node De=first;
   while (de.data!=n) {de=de.next;
   count++;
   if (de==null) {return-1;
 } return count;
   The public void print () {Node no=first;//prints all while (no!=null) {System.out.println (no.data);
  No=no.next;
  }} public class Textnode {public static void main (string[] args) {linklist ll=new linklist ();
  Ll.addnode (New Node (12));
  Ll.addnode (New Node (15));
  Ll.addnode (New Node (18));
  Ll.addnode (New Node (19));
  Ll.addnode (New Node (20));

  /*system.out.println (Ll.first.data);
  Ll.delectnode (); System.out.println (ll.first.data); * * SYSTEM.OUT.PRINTLN (LL.
  Number ());
  Ll.delectexact (3);
  Ll.addexact (3, New Node (100)); System.out.println (LL.
Number ());
  Ll.print ();
  
 System.out.println (Ll.findnode (112));
 }
}

Java implementation bidirectional linked list:

public class Doublelink {public static void main (String[]args) {Node2 no=new Node2 (5);
  No.addleft (New Node2 (6));
  No.addright (New Node2 (7));
  /*no.print ();
  No.print2 (); */No.addexact2 (1, New Node2 (8));
  No.print ();
  System.out.println ("--------------");
 No.print2 ();
 } class Node2 {public Node2-I;
 Public Node2 end;
 Public Node2 left;
 public Node2 right;
 int data=0;
  public Node2 (int n) {first=this;
  
  End=this;
 First.data=n;
  ///Add public void Addleft (Node2 before) {First.left=before from head;
  Before.right=first;
 First=before;
  ///Add public void addright from tail (Node2 after) {end.right=after;
  After.left=end;
 End=after;
  }//Insert the first few public void addexact (int n,node2 no) {int count=0 of a positive number (third sound);
  if (n==0) {addleft (no);
   else {Node2 F=first;
    while (true) {f=f.right;
    count++;
     if (count==n) {//Here is the direction of the four pointer change no.left=f.left;
 F.left.right=no;
     First.left=no;
     No.right=f;F.left=no;
    Break
  The first few public void addExact2 (int n,node2 no) {int count=0;
  if (n==0) {addright (no);
   else {Node2 f=end;
    while (true) {f=f.left;
    count++;
     if (count==n) {no.left=f;
     No.right=f.right;
     F.right.left=no;
     F.right=no;
     
    Break
  }}//positive sequence traversal public void print () {System.out.println (first.data);
   while (First.right!=null) {System.out.println (first.right.data);
  First=first.right;
 }//System.out.println (End.data);
  }//Reverse traversal of the public void Print2 () {System.out.println (end.data);
   while (End.left!=null) {System.out.println (end.left.data);
  End=end.left;
}}//* It is noteworthy that each time you insert a new object, you need to be aware of the changes that the pointer points to.
The first is the point on both sides of the new object (left and right), followed by the point to the right of the object on the left and the right object pointing to the left.
These four pointers must be pointed correctly, or they may result in a positive sequence or a reverse traversal that cannot be implemented.
 * */////////////////////////Because there is only one head, and the two-way linked list, with the head and tail, can be traversed from the * head, can also be traversed from the tail, and one of the objects because there are two directional pointers, so he can get to the left of the * object can also get the object on the right. * But a single linked list, because there is only one direction, so can only left or right. When you add an object, bidirectional can also be added from scratch, alsoCan be added from the tail.
 * If a single linked list to achieve two directions to add more rare, or not, because he only has a pointer to the left or to the right, and the two-way list each object has a two-direction pointer that is not more flexible, but this is also flawed, because each object * will contain two pointers, which also consumes more memory.
 * 
 * */

The above is the entire content of this article, I hope to learn Java program to help you.

Related Article

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.