zoukankan      html  css  js  c++  java
  • 用内部类实现链表的增删改查

    public class 链表{
        public static void main(String[] args){
            NodeManager nm = new NodeManager();
            System.out.println("---------------add---------------");
            nm.add(5);
            nm.add(4);
            nm.add(3);
            nm.add(2);
            nm.add(1);
            nm.add(0);
            nm.print();
            System.out.println("--------------del----------------");
            nm.del(3);
            nm.print();
            System.out.println("--------------find----------------");
            System.out.println(nm.find(3));
            System.out.println("--------------update----------------");
            nm.update(2,250);
            nm.print();
            System.out.println("--------------insert----------------");
            nm.insert(4,20);
            nm.print();
            System.out.println(nm.len);
        }
    }
    class NodeManager{
        private Node root;//根节点
        private int currentIndex = -1;//节点序号,每次操作从-1开始
        public int len = -1;
        //因为外部无法访问内部类,所以需要提供访问内部类方法
        public void add(int data){
            if(root == null){
                len++;
                root = new Node(data);
            }else{
                root.addNode(data);
            }
        }  
        public void del(int data){
            if(root==null){
                return;
            }
            if(root.getData()==data){
                root = root.next;//根节点后移
                len--;
            }else{
                root.delNode(data);
            }
        }
        public void print(){
            if(root!=null){
                System.out.print("root:"+root.getData()+"->");
                root.printNode();
                System.out.println("打印完毕");
            }
        }
        //查找是否存在节点
        public boolean find(int data){
            if(root==null) return false;
            if(root.getData()==data)
                return true;
            else{
                root.findNode(data);
            }
            return false;
        }
        public boolean update(int oldData,int newData){
            if(root== null){
                return false;
            }
            if(root.getData()==oldData){
                root.setData(newData);
                return true;
            }else{
                root.updateNode(oldData,newData);
                return false;
            }
        }
        public void insert(int index,int data){
            currentIndex = -1;
                if(index<=len){
                    
                        if(root==null){
                            return;
                        }
                        if(inde x<0){
                            return;
                        }
                        if(index == currentIndex){
                            len++;
                            Node newNode = new Node(data);
                            newNode.next = root;
                            root = newNode;
                        }else{
                            root.insertData(index,data);
                        }        
                    }
                else{
                    System.out.println("请输入正确的长度");
                }
        }
        private class Node{
            private int data;
            private Node next;//把当前类型作为属性
            public Node(int data){
                this.data = data;
            }
            public void setData(int data){
                this.data = data;
            }
            public int getData(){//给外部类用的
                return data;
            }
            //添加节点
            public void addNode(int data){
                if(this.next ==null)
                {
                    len++;
                    this.next = new Node(data);
                }else{
                    this.next.addNode(data);//this是root
                }
            }
            //删除节点
            public void delNode(int data){
                if(this.next!=null){
                    if(this.next.data!=data){
                        this.next.delNode(data);            
                    }else{
                        this.next = this.next.next;
                        len--;
                    }
                }
            }
            //输出所有节点
            public void printNode(){
                if(this.next!=null)
                {
                    System.out.print(this.next.data+"->");//此处不用getData(),是因为getData是给外部用,而内部可以直接用data
                    this.next.printNode();
                }
            }
            //查找节点是否存在
            public boolean findNode(int data){
                if(this.next == null){
                    return false;
                }
                if(this.next.data==data){
                    return true;
                }else{
                    this.next.findNode(data);
                }
                return false;
            }
            //修改节点
            public boolean updateNode(int oldData,int newData){
                if(this.next==null){
                    return false;
                }
                if(this.next.data==oldData){
                    this.next.data =newData;
                    return true;
                }else{
                     this.next.updateNode(oldData,newData);
                     return false;
                }
                    
            }
            //插入节点
            public void insertData(int index,int data){
                    currentIndex++;
                    if(index  == currentIndex){
                        len++;
                        Node newNode = new Node(data);
                        if(index<len){
                            newNode.next = this.next;
                            this.next = newNode;
                        }else{
                            newNode.next=null;
                            this.next = newNode;
                        }
                    }else{
                        this.next.insertData(index,data);//谁调用inserData方法那么this就是谁
                    }
            }
        }
    }
  • 相关阅读:
    百度网盘提速方法
    2020年北京某企Java校招真题
    scrapy中选择器的用法
    scrapy框架基础篇
    selenium模拟浏览器爬取淘宝产品信息
    python连接MongoDB
    pyquery库
    BeautifulSoup4库
    Locust
    【Java】类赋值的使用方式
  • 原文地址:https://www.cnblogs.com/MySweetheart/p/12286947.html
Copyright © 2011-2022 走看看