JAVA实现双向链表的增删功能的方法

 更新时间:2018年03月26日 10:10:58   作者:song.yan  
本篇文章主要介绍了JAVA实现双向链表的增删功能的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

JAVA实现双向链表的增删功能,完整代码

package linked;
class LinkedTable{  
}
public class LinkedTableTest {
   //构造单链表
  static Node node1 = new Node("name1");
  static Node node2 = new Node("name2");
  static Node node3 = new Node("name3");
  static Node node4 = new Node("name4");
  static Node node5 = new Node("name5");
  public static void main(String[] args)
  {
    //设置指针
    setPoint();
    
    //循环遍历
    System.out.println("*******初始链表*******");
    out(node1,node5);
    System.out.println();
    
    //插入节点在node2的后面
    addNode(node2,node3);
    
    // 循环遍历
    System.out.println("*******插入node2.5*******");
    out(node1, node5);
    System.out.println();
        
    //删除节点
    node2.setNextNode(node3);
    node3.setNextNodeF(node2);
    
    // 循环遍历
    System.out.println("*******删除node2.5*******");
    out(node1, node5);
    System.out.println();
    
  }
  
  //设置指针
  public static void setPoint()
  {
    //设置正向指针
    node1.setNextNode(node2);
    node2.setNextNode(node3);
    node3.setNextNode(node4);
    node4.setNextNode(node5);
    //设置反向指针
    node5.setNextNodeF(node4);
    node4.setNextNodeF(node3);
    node3.setNextNodeF(node2);
    node2.setNextNodeF(node1);
  }
  
  //循环遍历单链表
  public static void outLinked(Node startNode){
    Node node= new Node();
    node.setNextNode(startNode);
    do
    {
      node=node.getNextNode();
      System.out.print(node.getName()+"----");  
    }while(node.getNextNode()!=null);
  }
  
  //反向循环遍历单链表
  public static void outLinkedF(Node endNode){
    Node node= new Node();
    node.setNextNodeF(endNode);
    do
    {
      node=node.getNextNodeF();
      System.out.print(node.getName()+"----");  
    }while(node.getNextNodeF()!=null);
  }
  
  //循环遍历
  public static void out(Node startNode,Node endNode)
  {
    outLinked(startNode);
    System.out.println();
    outLinkedF(endNode);    
  }
  
  //插入节点
  public static void addNode(Node preNode,Node nextNode)
  {
    Node node_add = new Node("name2.5");
    node_add.setNextNode(preNode.getNextNode());
    preNode.setNextNode(node_add);    
    node_add.setNextNodeF(nextNode.getNextNodeF());
    nextNode.setNextNodeF(node_add);
  }  
}

class Node {
  private String name;
  private Node nextNode;
  private Node nextNodeF;
  public void setName(String name)
  {
    this.name=name;
  }
  public void setNextNode(Node nextNode)
  {
    this.nextNode=nextNode;
  }
  public void setNextNodeF(Node nextNodeF)
  {
    this.nextNodeF=nextNodeF;
  }
  public String getName()
  {
    return this.name;
  }
  public Node getNextNode()
  {
    return this.nextNode;
  }
  public Node getNextNodeF()
  {
    return this.nextNodeF;
  }
  public Node(String name)
  {
    this.name=name;
    this.nextNode=null;
  }
  public Node( )
  {    
  }  
}

1,构造node节点,需要两个指针,一个正向存储下一个元素的位置,一个反向存储下一个元素的位置

参数说明:

  name:用于存储node自身的信息

  nextNode:用于存储正向指针

  nextNodeF:用于存储反向指针

class Node {
  private String name;
  private Node nextNode;
  private Node nextNodeF;
  public void setName(String name)
  {
    this.name=name;
  }
  public void setNextNode(Node nextNode)
  {
    this.nextNode=nextNode;
  }
  public void setNextNodeF(Node nextNodeF)
  {
    this.nextNodeF=nextNodeF;
  }
  public String getName()
  {
    return this.name;
  }
  public Node getNextNode()
  {
    return this.nextNode;
  }
  public Node getNextNodeF()
  {
    return this.nextNodeF;
  }
  public Node(String name)
  {
    this.name=name;
    this.nextNode=null;
  }
  public Node( )
  {    
  }  
}

2,创建节点,设置指针连接节点

正向指针:指向下一个节点

反向节点:指向上一个节点

//构造单链表
  static Node node1 = new Node("name1");
  static Node node2 = new Node("name2");
  static Node node3 = new Node("name3");
  static Node node4 = new Node("name4");
  static Node node5 = new Node("name5");
public static void setPoint()
  {
    //设置正向指针
    node1.setNextNode(node2);
    node2.setNextNode(node3);
    node3.setNextNode(node4);
    node4.setNextNode(node5);
    //设置反向指针
    node5.setNextNodeF(node4);
    node4.setNextNodeF(node3);
    node3.setNextNodeF(node2);
    node2.setNextNodeF(node1);
  }

3,将链表循环遍历输出

public static void outLinked(Node startNode){
    Node node= new Node();
    node.setNextNode(startNode);
    do
    {
      node=node.getNextNode();
      System.out.print(node.getName()+"----");  
    }while(node.getNextNode()!=null);
  }

  public static void outLinkedF(Node endNode){
    Node node= new Node();
    node.setNextNodeF(endNode);
    do
    {
      node=node.getNextNodeF();
      System.out.print(node.getName()+"----");  
    }while(node.getNextNodeF()!=null);
  }

4,添加节点

  public static void addNode(Node preNode,Node nextNode)
  {
    Node node_add = new Node("name2.5");
    node_add.setNextNode(preNode.getNextNode());
    preNode.setNextNode(node_add);
    
    node_add.setNextNodeF(nextNode.getNextNodeF());
    nextNode.setNextNodeF(node_add);
  }

5,删除节点

node2.setNextNode(node3);
node3.setNextNodeF(node2);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java压缩文件工具类ZipUtil使用方法代码示例

    Java压缩文件工具类ZipUtil使用方法代码示例

    这篇文章主要介绍了Java压缩文件工具类ZipUtil使用方法代码示例,具有一定借鉴价值,需要的朋友可以参考下。
    2017-11-11
  • SpringBoot集成Sharding-JDBC实现分库分表方式

    SpringBoot集成Sharding-JDBC实现分库分表方式

    这篇文章主要介绍了SpringBoot集成Sharding-JDBC实现分库分表方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • SpringBoot启动失败的原因及其解决方法

    SpringBoot启动失败的原因及其解决方法

    对于springboot的启动失败,相信大家都有经历,但是为什么会启动失败,以及怎么解决都只能通过日志进行查看,在这里,我会将常见的springboot启动失败的报错一一展示,需要的朋友可以参考下
    2024-06-06
  • Java编程发展历史(动力节点Java学院整理)

    Java编程发展历史(动力节点Java学院整理)

    Java的历史可以追溯到1991年4月,Sun公司的James Gosling领导的绿色计划(Green Project)开始着力发展一种分布式系统结构,使其能够在各种消费性电子产品上运行,他们使用了C/C++/Oak语言。由于多种原因,绿色计划逐渐陷于停滞状态
    2017-03-03
  • Java C++题解leetcode1441用栈操作构建数组示例

    Java C++题解leetcode1441用栈操作构建数组示例

    这篇文章主要为大家介绍了Java C++题解leetcode1441用栈操作构建数组示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • idea关联maven的使用详解

    idea关联maven的使用详解

    这篇文章主要介绍了idea关联maven的使用详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • Spring Initializr只能创建为Java 17版本以上的问题解决

    Spring Initializr只能创建为Java 17版本以上的问题解决

    这篇文章主要给大家介绍了关于Spring Initializr只能创建为Java 17版本以上问题的解决办法,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2024-01-01
  • 使用Assembly打包和部署SpringBoot工程方式

    使用Assembly打包和部署SpringBoot工程方式

    文章介绍了SpringBoot项目的两种部署方式:Docker容器部署和FatJar直接部署,FatJar部署存在配置文件隐藏和启动脚本复杂的问题,而Assembly打包方式可以解决这些问题,使得SpringBoot能够加载jar外的配置文件并提供服务化的启动脚本
    2024-12-12
  • java进行远程部署与调试及原理详解

    java进行远程部署与调试及原理详解

    这篇文章主要介绍了java进行远程部署与调试及原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • 使用Java Socket实现GPS定位数据处理

    使用Java Socket实现GPS定位数据处理

    在许多应用场景中,如车辆追踪、移动设备定位等,GPS定位数据的实时获取和处理至关重要,本文将介绍如何使用Java Socket编程来接收GPS设备发送的数据并进行处理,需要的朋友可以参考下
    2024-07-07

最新评论