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());
    }
}

 

posted @ 2018-09-25 19:15  刘冠  阅读(196)  评论(0编辑  收藏  举报