Java data structure linked list operation implementation code _java

Source: Internet
Author: User
Tags int size

The linked list is a kind of complex data structure, and the relationship between the data makes the list divided into three kinds: single linked list, circular chain list, bidirectional linked list , and the following will be introduced. Linked list in the data structure is the basis, but also an important point of knowledge, here is the implementation of the list in Java,

JAVA Linked list operation: Single linked list and double linked list

The main points to tell:

A brief introduction of the linked list

Second, the principle and necessity of the chain list realization

Example of single chain representation

Four, double chain example

A brief introduction of the linked list

The linked list is a kind of commonly used data structure, although the chain list is more complex, but it is convenient in the query time, in a variety of computer languages are applicable, the list has a variety of categories, the article for the single linked list and double linked list analysis. The data in the list is like being chained together by a chain, so that data can be accessed easily.

Second, the principle and necessity of the chain list realization

Only single linked lists and double linked lists are analyzed here. The process of realizing a linked list is somewhat complicated, but it can bring a lot of benefits. For example, now the advent of online shopping, merchants Send express will generally package the goods in the box and write address information, courier companies can find the information on the box buyers, the complete arrival of goods. If there is no protection of the box, the goods may be damaged on the way. The linked list is like the box that wrote the address information, which protects the information of the goods while also writing the logistics information. There is a head node in the list, similar to "locomotive", as long as the corresponding head node, you can operate on the linked list. In this analysis, the head node simply does the data header and does not save valid data.

The realization principle of single linked list is shown as follows:

  

Double Linked list realization principle:

Example of single chain representation

Icommoperate<t> interface Operation class:

Package linklisttest;
Import Java.util.Map;
Public interface Icommoperate<t> {public
  
  boolean Insertnode (T node);
  public boolean insertposnode (int pos, T node);
  public boolean deletenode (int pos);
  public boolean updatenode (int pos, map<string, object> Map);
  Public T getnode (int pos, map<string, object> Map);
  public void Printlink ();
}

Single linked table node:

Package linklisttest;
Single-Link table node public
class Snode {
  private String data;
  Private Snode NextNode;
  Public Snode () {
  } public
  Snode (String data) {
    this.data = data;
    This.nextnode = new Snode ();
  }
  
  Public String GetData () {return
    data;
  }
  public void SetData (String data) {
    this.data = data;
  }
  Public Snode Getnextnode () {return
    nextnode;
  }
  public void Setnextnode (Snode nextnode) {
    this.nextnode = nextnode;
  }
  @Override public
  String toString () {return
    "Snode [data=] + data +"] ";
  }
}

Single Link Action class:

Package linklisttest;
Import Java.util.HashMap;
Import Java.util.Map; public class Singlelinklist implements icommoperate<snode>{private Snode head = new Snode ("heads");//Common header pointer, after declaration
  Invariant private int size = 0;
  public int GetSize () {return this.size; 
    * * * * list INSERT, insert every time to the end */@Override public boolean Insertnode (Snode node) {Boolean flag = false;
    Snode current = This.head;
      if (this.size==0) {//Empty link Table this.head.setNextNode (node);
    Node.setnextnode (NULL);
      }else{//Linked list while (Current.getnextnode ()!=null) {current = Current.getnextnode ();
      } current.setnextnode (node);
    Node.setnextnode (NULL);
    } this.size++;
    
    Flag = true;
  return flag;
    * * * Inserts a list at the specified location POS, starting at 1, and the POS is larger than size to insert the end of the list * */@Override public boolean insertposnode (int pos, snode node) { 
    Boolean flag = true;
    
    Snode current = This.head.getNextNode (); if (this.size==0) {//Empty Chain table This.head.setNextNode (node);
      Node.setnextnode (NULL);
    this.size++;
    }else if (this.size<pos) {//POS position is greater than the chain table length, insert end insertnode (node);
      }else if (pos>0 && pos<=this.size) {//Linked list within node//1, find to insert POS position node and front node int found = 0; Snode Prenode = This.head; Front node Snode currentnode = current;          The current node while (find<pos-1 && currentnode.getnextnode ()!=null) {prenode = present; The front node moves currentnode = Currentnode.getnextnode ();
      The current node moves find++;
}//System.out.println (Prenode);
      System.out.println (CurrentNode);
      2. Inserting nodes Prenode.setnextnode (node);
      Node.setnextnode (CurrentNode);
      this.size++;
    System.out.println ("node has been inserted into the list");
      }else{System.out.println ("Position information error");
    Flag = false;
  return flag; * * * Specifies the node POS for the linked list and deletes the corresponding node. How to: Locate and delete the node before and after it is deleted. Starting from 1 */@Override public boolean DeletenOde (int pos) {Boolean flag = false;
    Snode current = This.head.getNextNode ();
    if (pos<=0 | | pos>this.size | | current==null) {SYSTEM.OUT.PRINTLN ("Location information error or no information on linked list");
      }else{//1, find to delete node before and after the node int found = 0; Snode Prenode = This.head; Front node Snode nextnode = Current.getnextnode ();          The Back node while (find<pos-1 && nextnode.getnextnode ()!=null) {prenode = current; The front node moves nextnode = Nextnode.getnextnode ();
      After the node to move find++;
}//System.out.println (Prenode);
      
      System.out.println (NextNode);
      2, delete node Prenode.setnextnode (nextnode);
      System.GC ();
      this.size--;
    Flag = true;
  return flag; * * * Specify the node pos for the linked list and modify the corresponding node.
    Starting from 1 */@Override public boolean updatenode (int pos, map<string, Object> Map) {Boolean flag = false; Snode node = getnode (pos, map); Obtain the corresponding position POS node if (node!=null) {String data= (String) map.get ("Data");
      Node.setdata (data);
    Flag = true;
  return flag; * * * Find node POS for specified list, starting at 1 */@Override public snode getnode (int pos, map<string, object> Map) {SN
    Ode current = This.head.getNextNode ();
      if (pos<=0 | | pos>this.size | | current==null) {SYSTEM.OUT.PRINTLN ("Location information error or list does not exist");
    return null;
    int find = 0;
      while (find<pos-1 && current!=null) {current = Current.getnextnode ();
    find++;
  return to current;
    /* * Print List */@Override public void Printlink () {int length = This.size; if (length==0) {System.out.println ("The linked list is empty!")
      ");
    return;
    } Snode current = This.head.getNextNode (); 
    int find = 0;
    System.out.println ("A total number of nodes:" + length + "");
      while (Current!=null) {System.out.println ("first" + (++find) + "node:" + current);
    Current=current.getnextnode (); }} public static void Main (String[] args {singlelinklist sll = new Singlelinklist ();
    Snode Node1 = new Snode ("Node 1");
    Snode Node2 = new Snode ("Node 2");
    Snode node3 = new Snode ("Node 3");
    Snode node4 = new Snode ("Node 4");
    Snode node5 = new Snode ("Node 5");
    Snode node6 = new Snode ("Insert specified position");
    Sll.insertposnode (Sll.getsize () +1, Node1);
    Sll.insertposnode (Sll.getsize () +1, node2);
    Sll.insertposnode (Sll.getsize () +1, node3);
    Sll.insertposnode (Sll.getsize () +1, node4);
    
Sll.insertposnode (Sll.getsize () +1, NODE5);
Sll.insertnode (Node1);
Sll.insertnode (Node2);
Sll.insertnode (NODE3);
Sll.insertnode (NODE4);
    
    Sll.insertnode (NODE5);
    System.out.println ("******************* Output chain table *******************");
    
    Sll.printlink ();
    System.out.println ("******************* Gets the specified list node *******************");
    int pos = 2;
    
    System.out.println ("Get the list" +pos+ "position data:" +sll.getnode (POS, null)); SYSTEM.OUT.PRINTLN ("******************* inserts a node into the list at the specified location * ******************");
    int pos1 = 2;
    System.out.println ("Insert data into the" +pos1+ "node:");
    Sll.insertposnode (POS1, node6);
    
    Sll.printlink ();
    System.out.println ("******************* Delete a list specify location node *******************");
    int pos2 = 2;
    System.out.println ("Delete the first" +pos2+ "node:");
    Sll.deletenode (POS2);
    
    Sll.printlink ();
    SYSTEM.OUT.PRINTLN ("******************* modify the linked list to specify the location node *******************");
    int POS3 = 2;
    System.out.println ("Modify the first" +pos3+ "node:");
    map<string, object> map = new hashmap<> ();
    Map.put ("Data", "This is a Test");
    Sll.updatenode (POS3, map);
  Sll.printlink (); }
}

Four, double chain example

Icommoperate<t> interface Operation class:

Package linklisttest;
Import Java.util.Map;
Public interface Icommoperate<t> {public  
  boolean Insertnode (T node);
  public boolean insertposnode (int pos, T node);
  public boolean deletenode (int pos);
  public boolean updatenode (int pos, map<string, object> Map);
  Public T getnode (int pos, map<string, object> Map);
  public void Printlink ();
}

Double Linked table node:

Package linklisttest;
Dual-Link Table node public
class Dnode {
  private dnode priornode;
  Private String data;
  Private Dnode NextNode;
  
  Public Dnode () { 
  } public
  Dnode (String data) {
    This.priornode = new Dnode ();
    This.data = data;
    This.nextnode = new Dnode ();
  }

  Public Dnode Getpriornode () {return
    priornode;
  }
  public void Setpriornode (Dnode priornode) {
    this.priornode = Priornode;
  }

  Public String GetData () {return
    data;
  }
  public void SetData (String data) {
    this.data = data;
  }

  Public Dnode Getnextnode () {return
    nextnode;
  }
  public void Setnextnode (Dnode nextnode) {
    this.nextnode = nextnode;
  }

  @Override public
  String toString () {return
    "Dnode [data=] + data +"] ";
  }  
}

Double linked list implementation class:

Package linklisttest;
Import Java.util.HashMap;
Import Java.util.Map;
  public class Doublelinklist implements icommoperate<dnode>{private Dnode head = new Dnode ("Head");
  private int size = 0;
  public int GetSize () {return this.size; 
    
    * * * * list INSERT, insert every time to the end */@Override public boolean Insertnode (Dnode node) {Boolean flag = false;
    Dnode current = This.head;
      if (this.size==0) {//Empty link Table this.head.setNextNode (node);
      Node.setpriornode (This.head);
    Node.setnextnode (NULL);
      }else{//Linked list while (Current.getnextnode ()!=null) {current = Current.getnextnode ();
      } current.setnextnode (node);
      Node.setnextnode (NULL);
    Node.setpriornode (current);
    } this.size++;
  
    Flag = true;
  return flag; * * * Inserts a list at the specified position pos, starting at 1, while POS is larger than size and is inserted at the end of the list * */@Override public boolean insertposnode (int pos, dnode node)
    
    {Boolean flag = true; Dnode current = ThiS.head.getnextnode ();
      if (this.size==0) {//linked list is empty this.head.setNextNode (node);
      Node.setnextnode (NULL);
      Node.setpriornode (This.head);
    this.size++;
    }else if (pos>this.size) {//POS position is greater than the chain table length, insert end insertnode (node);
      }else if (pos>0 && pos<=this.size) {//linked list of nodes//1, locate POS node to insert position, insert POS node current position int find = 0;
        while (Find<pos-1 && current.getnextnode ()!=null) {current = Current.getnextnode ();
      find++;
        }//2, insert node if (Current.getnextnode () ==null) {//Tail node Node.setpriornode (current);
        Node.setnextnode (NULL);
      Current.setnextnode (node);
        else if (Current.getnextnode ()!=null) {//Intermediate node Node.setpriornode (Current.getpriornode ());
        Node.setnextnode (current);
        Current.getpriornode (). Setnextnode (node);
      Current.setpriornode (node);
    } this.size++; }else{System.out.println ("Location information error");
    Flag = false;
  return flag;  
    * * * Specify the node pos for the linked list, delete the corresponding node, starting at 1 */@Override public boolean deletenode (int pos) {Boolean flag = false;
    Dnode current = This.head.getNextNode ();
    if (pos<=0 | | pos>this.size | | current==null) {SYSTEM.OUT.PRINTLN ("Location information error or list does not exist");
      }else{//1, locate the location to remove pos node int find = 0;
        while (Find<pos-1 && current.getnextnode ()!=null) {current = Current.getnextnode ();
      find++;
      }//2, delete node if (Current.getnextnode () ==null) {//Tail node Current.getpriornode (). Setnextnode (NULL);
        else if (Current.getnextnode ()!=null) {//Intermediate node Current.getpriornode (). Setnextnode (Current.getnextnode ());
      Current.getnextnode (). Setpriornode (Current.getpriornode ());
      } System.GC ();
      this.size--;
    Flag = true;
  return flag; * * * Specify the node pos for the linked list and modify the corresponding node. Starting from 1 */@Override public boolean UpdatenodE (int pos, map<string, Object> Map) {Boolean flag = false;
    Dnode node = getnode (pos, map);
      if (node!=null) {String data = (string) map.get ("Data");
      Node.setdata (data);
    Flag = true;
  return flag;
    * * * Find node POS for specified list, starting from 1 */@Override public dnode getnode (int pos, map<string, object> Map) {
    Dnode current = This.head.getNextNode ();
      if (pos<=0 | | pos>this.size | | current==null) {SYSTEM.OUT.PRINTLN ("Location information error or list does not exist");
    return null;
    int find = 0;
      while (find<pos-1 && current!=null) {current = Current.getnextnode ();
    find++;
  return to current;
    /* * Print List */@Override public void Printlink () {int length = This.size; if (length==0) {System.out.println ("The linked list is empty!")
      ");
    return;
    } Dnode current = This.head.getNextNode (); 
    int find = 0;
    System.out.println ("A total number of nodes:" + length + ""); while (current!=null) {System.out.println ("first" + (++find) + "node:" + current);
    Current=current.getnextnode ();
    } public static void Main (string[] args) {doublelinklist dll = new doublelinklist ();
    Dnode Node1 = new Dnode ("Node 1");
    Dnode Node2 = new Dnode ("Node 2");
    Dnode node3 = new Dnode ("Node 3");
    Dnode node4 = new Dnode ("Node 4");
    Dnode node5 = new Dnode ("Node 5");
    Dnode node6 = new Dnode ("Insert specified position");
    Dll.insertposnode (Node1);
    Dll.insertposnode (Node2);
    Dll.insertposnode (NODE3);
    Dll.insertposnode (NODE4);
Dll.insertposnode (NODE5);
Dll.insertnode (Node1);
Dll.insertnode (Node2);
Dll.insertnode (NODE3);
Dll.insertnode (NODE4);
    
    Dll.insertnode (NODE5);
    System.out.println ("******************* Output chain table *******************");
    
    Dll.printlink ();
    System.out.println ("******************* Gets the specified list node *******************");
    int pos = 2; System.out.println ("Get the list" +pos+ "position data:" +DLL.GEtnode (POS, null));
    System.out.println ("******************* insert node to the list specified *******************");
    int pos1 = 2;
    System.out.println ("Insert data into the" +pos1+ "node:");
    Dll.insertposnode (POS1, node6);
    
    Dll.printlink ();
    System.out.println ("******************* Delete a list specify location node *******************");
    int pos2 = 7;
    System.out.println ("Delete the first" +pos2+ "node:");
    Dll.deletenode (POS2);
    
    Dll.printlink ();
    SYSTEM.OUT.PRINTLN ("******************* modify the linked list to specify the location node *******************");
    int POS3 = 2;
    System.out.println ("Modify the first" +pos3+ "node:");
    map<string, object> map = new hashmap<> ();
    Map.put ("Data", "This is a Test");
    Dll.updatenode (POS3, map);
  Dll.printlink ();

 } 
}

Thank you for reading, I hope to help you, thank you for your support for this site!

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.