Linked list and bidirectional linked list in Java language

Time:2021-11-21

Linked list is an important data structure, which plays a very important role in program design. C language and C + + language use pointers to realize the linked list structure. Because the Java language does not provide pointers, some people think that the linked list cannot be realized in the Java language. In fact, it is not. The Java language is easier to realize the linked list structure than C and C + +. The object reference in the Java language is actually a pointer (the pointers in this paper are conceptual meanings rather than the data types provided by the language), so we can write such a class to implement the nodes in the linked list.

  class Node
  {
  Object data;
  Node next;// Point to the next node
  }

The data field is defined as the object class because the object class is a generalized superclass. Any class object can assign values to it, which increases the generality of the code. In order to make the linked list accessible, you also need to define a header. The header must contain a pointer to the first node and a pointer to the current node. In order to add nodes at the end of the linked list, you can also add a pointer to the end of the linked list. In addition, you can use a field to represent the size of the linked list. When the caller wants to get the size of the linked list, he does not have to traverse the whole linked list. The following figure is a schematic diagram of this linked list:

Data structure of linked list

We can use the class list to realize the linked list structure, and use the variables head, tail, length and pointer to realize the header. There are certain skills when storing the pointer of the current node. Pointer does not store the pointer to the current node, but stores the pointer to its forward node. When its value is null, it means that the current node is the first node. So why? This is because after deleting the current node, it is still necessary to ensure that the remaining nodes form a linked list. If the pointer points to the current node, it will bring great difficulties to the operation. So how to get the current node? We define a method cursor (), and the return value is a pointer to the current node. Class list also defines some methods to realize the basic operations on the linked list. By using these basic operations, we can perform various operations on the linked list. For example, the reset () method makes the first node the current node. Insert (object d) method inserts a node before the current node and makes it the current node. The remove () method deletes the current node, returns its contents, and makes its successor node the current node. If the last node is deleted, the first node becomes the current node.

The source code of the linked list class list is as follows:

  import java.io.*;
  public class List
  {
/ * Implement header with variable*/
  private Node Head=null;
  private Node Tail=null;
  private Node Pointer=null;
  private int Length=0;
  public void deleteAll()
/ * empty the entire linked list*/
  {
  Head=null;
  Tail=null;
  Pointer=null;
  Length=0;
  }
  public void reset()
/ * the linked list is reset to make the first node the current node*/
  {
  Pointer=null;
  }
  public boolean isEmpty()
/ * judge whether the linked list is empty*/
  {
  return(Length==0);
  }
  public boolean isEnd()
/ * judge whether the current node is the last node*/
  {
  if(Length==0)
   throw new java.lang.NullPointerException();
  else if(Length==1)
   return true;
  else
   return(cursor()==Tail);
  }
  public Object nextNode()
/ * returns the value of the next node of the current node and makes it the current node*/
  {
  if(Length==1)
   throw new java.util.NoSuchElementException();
  else if(Length==0)
   throw new java.lang.NullPointerException();
  else
  {
   Node temp=cursor();
   Pointer=temp;
   if(temp!=Tail)
    return(temp.next.data);
   else
    throw new java.util.NoSuchElementException();
  }
  }
  public Object currentNode()
/ * returns the value of the current node*/
  {
  Node temp=cursor();
  return temp.data;
  }
  
  public void insert(Object d)
/ * insert a node before the current node and make it the current node*/
  {
  Node e=new Node(d);
  if(Length==0)
  {
   Tail=e;
   Head=e;
  }
  else
  {
   Node temp=cursor();
   e.next=temp;
   if(Pointer==null)
    Head=e;
   else
    Pointer.next=e;
  }
  Length++;
  }
  public int size()
/ * returns the size of the linked list*/
  {
  return (Length);
  }
  public Object remove()
/ * move the current node out of the linked list, and the next node becomes the current node. If the moved out node is the last node, the first node becomes the current node*/
  {
  Object temp;
  if(Length==0)
   throw new java.util.NoSuchElementException();
  else if(Length==1)
  {
   temp=Head.data;
   deleteAll();
  }
  else
  {
   Node cur=cursor();
   temp=cur.data;
   if(cur==Head)
    Head=cur.next;
   else if(cur==Tail)
   {
    Pointer.next=null;
    Tail=Pointer;
    reset();
   }
   else
    Pointer.next=cur.next;
    Length--;
  }
  return temp;
  }
  private Node cursor()
/ * returns the pointer of the current node*/
  {
  if(Head==null)
   throw new java.lang.NullPointerException();
  else if(Pointer==null)
   return Head;
  else
   return Pointer.next;
  }
  public static void main(String[] args)
/ * simple application example of linked list*/
  {
  List a=new List ();
  for(int i=1;i<=10;i++)
   a.insert(new Integer(i));
   System.out.println(a.currentNode());
   while(!a.isEnd())
    System.out.println(a.nextNode());
    a.reset();
    while(!a.isEnd())
    {
     a.remove();
    }
    a.remove();
    a.reset();
    if(a.isEmpty())
     System.out.println(“There is no Node in List \n”);
     System.in.println(“You can press return to quit\n”);
    try
    {
     System.in.read();
/ / ensure that the user can see the running results of the program
    }
    catch(IOException e)
    {}
   }
  }
  class Node
/ * node definition constituting the linked list*/
  {
   Object data;
   Node next;
   Node(Object d)
   {
    data=d;
    next=null;
   }
  }

Readers can also define new methods to operate the linked list according to their actual needs. The bidirectional linked list can be implemented in a similar way, but the node class adds a pointer to the forward node.

It can be implemented with such code:

  class Node
  {
  Object data;
  Node next;
  Node previous;
  Node(Object d)
  {
  data=d;
  next=null;
  previous=null;
  }
  }

Of course, the implementation of the basic operation of the two-way linked list is slightly different. The implementation methods of linked list and bidirectional linked list can also be used in the implementation of stack and queue. There is no more writing here. Interested readers can change the code of list class slightly.

Recommended Today

Single case mode can also play flowers

1、 Singleton mode 1. What is singleton mode (1) Singleton mode [singleton pattern:] definition: Ensure a class has only one instance, and provide a global point of access to it. Ensure that a class has only one instance and provide a global access point to it (only instance objects are allowed to be obtained through […]