zoukankan      html  css  js  c++  java
  • Java(链表)

    class Link {
        //定义为内部类是因为Node是为Link服务的
        private class Node {
            private String data;
            private Node next;
            public Node(String data) {
                this.data = data;
            }
            public void addNode(Node newNode) {
                if(this.next == null) {
                    this.next = newNode;
                } else {
                    this.next.addNode(newNode);
                }
            }
            public boolean containsNode(String data) {
                if(data.equals(this.data)) {
                    return true;
                } else {
                    if(this.next != null) {
                        return this.next.containsNode(data);
                    } else {
                        return false;
                    }
                }
            }
            public String getNode(int index) {
                //内部类访问外部类的属性
                if((Link.this.foot ++) == index) {
                    return this.data;
                } else {
                    return this.next.getNode(index);
                }
            }
            public void setNode(int index, String data) {
                //内部类访问外部类的属性
                if((Link.this.foot ++) == index) {
                    this.data = data;
                } else {
                    this.next.setNode(index, data);
                }
            }
            public void removeNode(Node previous, String data) {
                if(data.equals(this.data)) {
                    previous.next = this.next;
                } else {
                    this.next.removeNode(this, data);
                }
            }
            public void toArrayNode() {
                Link.this.retArray[Link.this.foot ++] = this.data;
                if(this.next != null) {
                    this.next.toArrayNode();
                }
            }
            public void printNode() {
                System.out.println(this.data);
                if(this.next != null) {
                    this.next.printNode();
                }
            }
        }
    
        private Node root;
        private int count = 0;
        private int foot = 0;
        private String[] retArray;
    
        public void add(String data) {
            if(data == null) {
                return;
            }
            Node newNode = new Node(data);
            if(this.root == null) {
                this.root = newNode;
            } else {
                this.root.addNode(newNode);
            }
            this.count ++;
        }
        public int size() {
            return this.count;
        }
        public boolean isEmpty() {
            return this.root == null;
        }
        public boolean contains(String data) {
            if(data == null || this.root == null) {
                return false;
            }
            return this.root.containsNode(data);
        }
        public String get(int index) {
            if((this.root == null) || (index > this.count)) {
                return null;
            }
            this.foot = 0; //索引使用的
            return this.root.getNode(index);
        }
        public void set(int index, String data) {
            if(index > this.count){
                return;
            }
            this.foot = 0; //索引使用的
            this.root.setNode(index, data);
        }
        public void remove(String data) {
            if(this.contains(data)) {
                if(data.equals(this.root.data)) {
                    this.root = this.root.next;
                } else {
                    this.root.next.removeNode(this.root, data);
                }
                this.count --;
            }
        }
        public String[] toArray() {
            if(this.root == null) {
                return null;
            }
            this.foot = 0;
            this.retArray = new String[this.count];
            this.root.toArrayNode();
            return this.retArray;
        }
        public void print() {
            if(this.root != null) {
                this.root.printNode();
            }
        }
    }
    
    public class Main {
    
        public static void main(String[] args) {
    
            Link link = new Link();
            link.add("0");
            link.add("1");
            link.add("2");
            link.add("3");
            link.add("4");
            link.add("5");
            link.add("6");
            link.set(3, "34");
            link.remove("4");
            link.print();
            System.out.println(link.size());
            System.out.println(link.isEmpty());
            System.out.println(link.contains("你好"));
            System.out.println(link.get(3));
            System.out.println(link.toArray());
        }
    }
  • 相关阅读:
    vue子组件向父组件传值
    定义特有属性的对象
    电话号码的校验
    多个图片的显示与隐藏
    原生的ajax请求----(播放托管到爱奇艺上的视频)
    将视频托管到爱奇艺 (第一步)
    2018年春季个人阅读计划
    《软件需求与分析》阅读笔记
    Ngers天气——开发进程4
    软件需求与分析读书笔记3
  • 原文地址:https://www.cnblogs.com/liuguan/p/9702645.html
Copyright © 2011-2022 走看看