Home > Java > javaTutorial > Java data structure linked list operation implementation code

Java data structure linked list operation implementation code

高洛峰
Release: 2017-01-16 15:33:58
Original
1373 people have browsed it

A linked list is a complex data structure. The interrelationship between its data divides the linked list into three types: singly linked list, circular linked list, and doubly linked list, which will be introduced one by one below. Linked lists are the foundation in data structures and are also important knowledge points. Here we will talk about the implementation of linked lists in Java.

JAVA linked list operations: single linked lists and double linked lists

Mainly describe the following points:

1. Introduction to linked lists

2. Principles and necessity of linked list implementation

3. Examples of single-linked lists

4. Examples of double-linked lists

1. Introduction to linked lists

Linked lists are a commonly used data structure. Although linked lists are more complicated to save, they are more convenient for querying and are used in many computer languages. Linked lists have A variety of categories, the article analyzes singly linked lists and doubly linked lists. The data in the linked list is like being connected in series by a chain, and the data can be accessed easily.

2. Principle and necessity of linked list implementation

Here we only analyze single linked lists and double linked lists. The implementation process of linked lists is somewhat complicated, but it will bring many benefits. For example, now that the era of online shopping has arrived, merchants usually package goods in boxes and write address information when sending them express delivery. The courier company can use the information on the box to find the buyer, and the goods arrive intact. Without the protection of the box, the goods may be damaged in transit. The linked list is like the box with the address information written on it, which not only protects the product information, but also writes the logistics information. There is a HEAD node in the linked list, similar to a "locomotive". As long as the corresponding HEAD node is found, the linked list can be operated. In this analysis, the HEAD node only serves as the data header and does not save valid data.

The implementation principle of a single linked list is as shown in the figure:

Java 数据结构链表操作实现代码

The implementation principle of a double linked list:

Java 数据结构链表操作实现代码

Three , Single linked list example

ICommOperate 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() ;
}
Copy after login

Singly linked list node:

package LinkListTest;
// 单连表节点
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 + "]";
  }
}
Copy after login

Single link operation class:

package LinkListTest;
import java.util.HashMap;
import java.util.Map;
public class SingleLinkList implements ICommOperate<SNode>{
  private SNode head = new SNode("HEAD") ; // 公共头指针,声明之后不变
  private int size = 0 ;
  public int getSize() {
    return this.size;
  }
 
  /*
   * 链表插入,每次往末端插入
   * */
  @Override
  public boolean insertNode(SNode node) {
    boolean flag = false ;
    SNode current = this.head ;
    if( this.size==0 ){ // 空链表
      this.head.setNextNode(node) ;
      node.setNextNode(null) ;
    }else{        // 链表内节点
      while( current.getNextNode()!=null ){
        current = current.getNextNode() ;
      }
      current.setNextNode(node) ;
      node.setNextNode(null) ;
    }
    this.size++ ;
    flag = true ;
     
    return flag;
  }
 
  /*
   * 插入链表指定位置pos,从1开始,而pos大于size则插入链表末端
   * */
  @Override
  public boolean insertPosNode(int pos, SNode node){
    boolean flag = true;
    SNode current = this.head.getNextNode() ;
     
    if( this.size==0 ){          // 空链表
      this.head.setNextNode(node) ;
      node.setNextNode(null) ;
      this.size++ ;
    }else if( this.size<pos ){      // pos位置大于链表长度,插入末端
      insertNode(node) ;
    }else if( pos>0 && pos<=this.size) { // 链表内节点
      // 1、找到要插入pos位置节点和前节点
      int find = 0;
      SNode preNode = this.head; // 前节点
      SNode currentNode = current; // 当前节点
      while( find<pos-1 && currentNode.getNextNode()!=null ){
        preNode = current ;          // 前节点后移
        currentNode = currentNode.getNextNode() ; // 当前节点后移
        find++ ;
      }
//      System.out.println(preNode);
//      System.out.println(currentNode);
      // 2、插入节点
      preNode.setNextNode(node);
      node.setNextNode(currentNode);
      this.size++ ;
      System.out.println("节点已经插入链表中");
    }else{
      System.out.println("位置信息错误");
      flag = false ;
    }
     
    return flag;
  }
   
  /*
   * 指定链表的节点pos,删除对应节点。方式:找到要删除节点的前后节点,进行删除。从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("位置信息错误或链表无信息");
    }else{
      // 1、找到要删除节点的前后节点
      int find = 0;
      SNode preNode = this.head; // 前节点
      SNode nextNode = current.getNextNode(); // 后节点
      while( find<pos-1 && nextNode.getNextNode()!=null ){
        preNode = current ;          // 前节点后移
        nextNode = nextNode.getNextNode() ; // 后节点后移
        find++ ;
      }
//      System.out.println(preNode);
//      System.out.println(nextNode);
       
      // 2、删除节点
      preNode.setNextNode(nextNode);
      System.gc();
      this.size-- ;
      flag = true ;
    }
     
    return flag;
  }
 
  /*
   * 指定链表的节点pos,修改对应节点。 从1开始
   * */
  @Override
  public boolean updateNode(int pos, Map<String, Object> map) {
    boolean flag = false ;
    SNode node = getNode(pos, map); // 获得相应位置pos的节点
    if( node!=null ){
      String data = (String) map.get("data") ;
      node.setData(data);
      flag = true ;
    }
    return flag;
  }
 
  /*
   * 找到指定链表的节点pos,从1开始
   * */
  @Override
  public SNode getNode(int pos, Map<String, Object> map) {
    SNode current = this.head.getNextNode() ;
    if( pos<=0 || pos>this.size || current==null ){
      System.out.println("位置信息错误或链表不存在");
      return null;
    }
    int find = 0 ;
    while( find<pos-1 && current!=null ){
      current = current.getNextNode() ;
      find++ ;
    }
    return current;
  }
 
  /*
   * 打印链表
   * */
  @Override
  public void printLink() {
    int length = this.size ;
    if( length==0 ){
      System.out.println("链表为空!");
      return ;
    }
    SNode current = this.head.getNextNode() ;
    int find = 0 ;
    System.out.println("总共有节点数: " + length +" 个");
    while( current!=null ){
      System.out.println("第 " + (++find) + " 个节点 :" + current);
      current=current.getNextNode() ;
    }
  }
   
  public static void main(String[] args) {
    SingleLinkList sll = new SingleLinkList() ;
    SNode node1 = new SNode("节点1");
    SNode node2 = new SNode("节点2");
    SNode node3 = new SNode("节点3");
    SNode node4 = new SNode("节点4");
    SNode node5 = new SNode("节点5");
    SNode node6 = new SNode("插入指定位置");
    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("*******************输出链表*******************");
    sll.printLink();
     
    System.out.println("*******************获得指定链表节点*******************");
    int pos = 2 ;
    System.out.println("获取链表第 "+pos+" 个位置数据 :"+sll.getNode(pos, null));
     
    System.out.println("*******************向链表指定位置插入节点*******************");
    int pos1 = 2 ;
    System.out.println("将数据插入第 "+pos1+" 个节点:");
    sll.insertPosNode(pos1, node6) ;
    sll.printLink();
     
    System.out.println("*******************删除链表指定位置节点*******************");
    int pos2 = 2 ;
    System.out.println("删除第 "+pos2+" 个节点:");
    sll.deleteNode(pos2) ;
    sll.printLink();
     
    System.out.println("*******************修改链表指定位置节点*******************");
    int pos3 = 2 ;
    System.out.println("修改第 "+pos3+" 个节点:");
    Map<String, Object> map = new HashMap<>() ;
    map.put("data", "this is a test") ;
    sll.updateNode(pos3, map) ;
    sll.printLink();
  }
}
Copy after login

4. Double linked table example

ICommOperate 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() ;
}
Copy after login

Double linked list node:

package LinkListTest;
// 双连表节点
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 + "]";
  } 
}
Copy after login

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;
  }
   
  /*
   * 链表插入,每次往末端插入
   * */
  @Override
  public boolean insertNode(DNode node) {
    boolean flag = false;
     
    DNode current = this.head ;
    if( this.size==0 ){ // 空链表
      this.head.setNextNode(node) ;
      node.setPriorNode(this.head);
      node.setNextNode(null) ;
    }else{        // 链表内节点
      while( current.getNextNode()!=null ){
        current = current.getNextNode() ;
      }
      current.setNextNode(node);
      node.setNextNode(null);
      node.setPriorNode(current);
    }
    this.size++ ;
    flag = true ;
   
    return flag;
  }
   
  /*
   * 插入链表指定位置pos,从1开始,而pos大于size则插入链表末端
   * */
  @Override
  public boolean insertPosNode(int pos, DNode node) {
    boolean flag = true;
     
    DNode current = this.head.getNextNode() ;
    if( this.size==0){             // 链表为空
      this.head.setNextNode(node) ;
      node.setNextNode(null) ;
      node.setPriorNode(this.head);
      this.size++ ;
    }else if( pos>this.size ){         // pos位置大于链表长度,插入末端
      insertNode(node) ;
    }else if( pos>0 && pos<=this.size ){  // 链表内节点
      // 1、找到要插入位置pos节点,插入pos节点当前位置
      int find = 0;
      while( find<pos-1 && current.getNextNode()!=null ){
        current = current.getNextNode() ;
        find++ ;
      }
      // 2、插入节点
      if( current.getNextNode()==null ){ // 尾节点
        node.setPriorNode(current);
        node.setNextNode(null);
        current.setNextNode(node);
      } else if( current.getNextNode()!=null ) { //中间节点
        node.setPriorNode(current.getPriorNode());
        node.setNextNode(current);
        current.getPriorNode().setNextNode(node);
        current.setPriorNode(node);
      }
      this.size++ ;
    }else{
      System.out.println("位置信息错误");
      flag = false ;
    }
     
    return flag;
  }
   
  /*
   * 指定链表的节点pos,删除对应节点,从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("位置信息错误或链表不存在");
    }else{
      // 1、找到要删除位置pos节点
      int find = 0;
      while( find<pos-1 && current.getNextNode()!=null ){
        current = current.getNextNode() ;
        find++ ;
      }
      // 2、删除节点
      if( current.getNextNode()==null ){ // 尾节点
        current.getPriorNode().setNextNode(null) ;
      } else if( current.getNextNode()!=null ) { //中间节点
        current.getPriorNode().setNextNode(current.getNextNode()) ;
        current.getNextNode().setPriorNode(current.getPriorNode()) ;
      }
      System.gc();
      this.size-- ;
      flag = true ;
    }
    return flag;
  }
 
  /*
   * 指定链表的节点pos,修改对应节点。 从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;
  }
   
  /*
   * 找到指定链表的节点pos,从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("位置信息错误或链表不存在");
      return null;
    }
    int find = 0 ;
    while( find<pos-1 && current!=null ){
      current = current.getNextNode() ;
      find++ ;
    }
    return current;
  }
   
  /*
   * 打印链表
   * */
  @Override
  public void printLink() {
    int length = this.size ;
    if( length==0 ){
      System.out.println("链表为空!");
      return ;
    }
    DNode current = this.head.getNextNode() ;
    int find = 0 ;
    System.out.println("总共有节点数: " + length +" 个");
    while( current!=null ){
      System.out.println("第 " + (++find) + " 个节点 :" + current);
      current=current.getNextNode() ;
    }
  }
   
  public static void main(String[] args) {
    DoubleLinkList dll = new DoubleLinkList() ;
    DNode node1 = new DNode("节点1");
    DNode node2 = new DNode("节点2");
    DNode node3 = new DNode("节点3");
    DNode node4 = new DNode("节点4");
    DNode node5 = new DNode("节点5");
    DNode node6 = new DNode("插入指定位置");
    dll.insertPosNode(10, node1) ;
    dll.insertPosNode(10, node2) ;
    dll.insertPosNode(10, node3) ;
    dll.insertPosNode(10, node4) ;
    dll.insertPosNode(10, node5) ;
//    dll.insertNode(node1);
//    dll.insertNode(node2);
//    dll.insertNode(node3);
//    dll.insertNode(node4);
//    dll.insertNode(node5);
     
    System.out.println("*******************输出链表*******************");
    dll.printLink();
     
    System.out.println("*******************获得指定链表节点*******************");
    int pos = 2 ;
    System.out.println("获取链表第 "+pos+" 个位置数据 :"+dll.getNode(pos, null));
     
    System.out.println("*******************向链表指定位置插入节点*******************");
    int pos1 = 2 ;
    System.out.println("将数据插入第"+pos1+"个节点:");
    dll.insertPosNode(pos1, node6) ;
    dll.printLink();
     
    System.out.println("*******************删除链表指定位置节点*******************");
    int pos2 = 7 ;
    System.out.println("删除第"+pos2+"个节点:");
    dll.deleteNode(pos2) ;
    dll.printLink();
     
    System.out.println("*******************修改链表指定位置节点*******************");
    int pos3 = 2 ;
    System.out.println("修改第"+pos3+"个节点:");
    Map<String, Object> map = new HashMap<>() ;
    map.put("data", "this is a test") ;
    dll.updateNode(pos3, map) ;
    dll.printLink();
  }
}
Copy after login

Thanks for reading, I hope it can help everyone, thank you for your support of this site!

For more Java data structure linked list operation implementation code related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template