zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    HTTP请求头的具体含意
    Python 之类与对象及继承
    PHP的一个牛逼的数组排序函数array_multisort
    mysqli返回受影响行数
    转:JS判断值是否是数字(两种方法)
    转:php中判断某个IP地址是否存在范围内
    php Closure::bind的用法(转)
    Drupal8入门文章推荐
    PHP通过api上传图片
    转:PHP中的使用curl发送请求(GET请求和POST请求)
  • 原文地址:https://www.cnblogs.com/excellencesy/p/8647849.html
Copyright © 2011-2022 走看看