java implementation of single linked list two way linked list

  • 2020-05-07 19:41:53
  • OfStack

In this article, the example of java for you to share the implementation of single linked list, two-way linked list code, for your reference, the specific content is as follows

java implements single linked list:


package code;

class Node
{
 Node next;
 int data;
 public Node(int data)
 {
  this.data=data;
 }
 
}
class LinkList
{
 Node first;
 // The head 
 public LinkList()
 {
  this.first=null;
 }
 public void addNode(Node no)
 {
  no.next=first;
  first=no;// Add in the header 
 }
 public void delectNode()
 {
  Node n=first.next;
  first=null;
  first=n;// Delete in the header 
 }
 // Delete the specified location 
 public int Number()
 {
  int count=1;
  // See how many elements there are 
  Node nd=first;
  while(nd.next!=null)
  {
   nd=nd.next;
   count++;
  }
  return count;
 }
 public void delectExact(int n)
 {
  // Delete the specified location 
  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 location 
  {
   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;// To find the 1 The position of the number 
  Node de=first;
  while(de.data!=n)
  {
   de=de.next;
   count++;
   if(de==null)
   {
    return -1;
   }
  }
  return count;
 }
 public void print()
 {
  Node no=first;// Print 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 implements two-way 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 first;
 public Node2 end;
 public Node2 left;
 public Node2 right;
 int data=0;
 public Node2(int n)
 {
  
  first=this;
  end=this;
  
  first.data=n;
 }
 // Add from the head 
 public void addLeft(Node2 before)
 {
  first.left=before;
  before.right=first;
  first=before;
 }
 // Add from the tail 
 public void addRight(Node2 after)
 {
  end.right=after;
  after.left=end;
  end=after;
 }
 // Insert a positive number ( The first 3 The sound ) Which one of 
 public void addExact(int n,Node2 no)
 {
  int count=0;
  if(n==0)
  {
   addLeft(no);
  }
  else
  { 
   Node2 f=first;
   while(true)
   {
    f=f.right;
    count++;
    if(count==n)
    {
     // In this case 4 The change of the pointer 
     no.left=f.left;
     f.left.right=no;
 //    first.left=no;
     no.right=f;
     f.left=no;
     break;
    }
 
   }
  }
 }
 // Insert the number of the reciprocal 
 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 
 public void print2()
 {
  System.out.println(end.data);
  while(end.left!=null)
  {
   System.out.println(end.left.data);
   end=end.left;
  }
 }
 

}
/* It's worth noting that each 1 Time to insert 1 When you have a new object, you need to pay attention to the pointer changes. 
 The first is the pointing to both sides of the new object (left and right), and the second is the pointing to the right of the left object 
 And to the left of the right object. 
 this 4 The Pointers must be pointed correctly, otherwise the positive or reverse order traversal may not be possible. 
*/
/* Compared to single linked list, single linked list can only be from 1 I'm going to walk in all directions, because there's only one 1 Headers, while bidirectional linked lists, have heads and tails that can be accessed from 
 *  You can go through it first, you can go through it last, and you can go through it 1 Because the object has Pointers in two directions, it can get the one on the left 
 *  The object can also get the object on the right. 
 *  But a single linked list, because there's only one 1 So you can only go left or right. When you add an object, you can either add it from the beginning or from the end. 
 *  If a single linked list is difficult to add in two directions, or it doesn't work, because it only has to go left or right 1 A pointer in four directions 
 *  Bidirectional lists, where each object has a pointer in two directions, are less flexible, but this has its drawbacks, too, because each object has a pointer in two directions 
 *  Both contain two Pointers, which also consumes more memory. 
 * 
 * */

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


Related articles: