JAVA数据结构-LinkedList,ArrayList,Tree,

来源:互联网 发布:spss面板数据分析 编辑:程序博客网 时间:2024/04/29 16:52

最近看了部分Java的源码后,稍微理解了一点源码对于LinkedList,ArrayList,Tree的写法。所以在此,记录自己对于这些数据结构的仿写,以方便以后的复习。
(一)Tree

public class Node<T> {    T t;    Node<T> left;    Node<T> right;    public Node(Node<T> left,T t,Node<T> right){        this.left = left;        this.t = t;        this.right = right;    }}public class Tree<T> implements Comparable<T>{    Node<T> now;    Node<T> root;    Integer size = 0;    public Tree(T t){        root = new Node<T>(null,t,null);        size++;    }    public void add(T t){        if(!checkRoot(t)){            now = root;            judge(t);            size++;        }    }    /**     * @param t     *  判断当前值和t的大小,t小就在左,大则在右     */    public void judge(T t){        int compare = this.compareTo(t);        if(compare<0){            if(now.right!=null){                now = now.right;                judge(t);            }else{                now.right = new Node<T>(null,t,null);            }        }else if(compare>0){            //小,如果下一个left不为空,则跟下一个left比较,一直到放置在最合理的位置            if(now.left!=null){                now = now.left;                judge(t);            }else{                now.left = new Node<T>(null,t,null);            }        }else if(compare == 0){            now.t = t;            size--;        }    }    /**     * @param t     *  判断是否有根节点,没有就自动创建一个     */    public boolean checkRoot(T t){        if(root==null){            root = new Node<>(null,t,null);            return true;        }        return false;    }    /*      * 重写该方法,判断根据char的大小来决定     */    @Override    public int compareTo(T o) {        String s1 = this.now.t.toString();        String s2 = o.toString();        int i1 = s1.length();        int i2 = s2.length();        int min = Math.min(i1, i2);        for(int i =0;i<min;i++){            char c1 = s1.charAt(i);            char c2 = s2.charAt(i);            if(c1>c2){                return 1;            }else if(c1<c2){                return -1;            }        }        return 0;    }}

(二)LinkedList

public class Node<T> {    public T node;    public Node<T> pre;    public Node<T> next;    public Node(Node<T> pre,T node,Node<T> next){        this.pre = pre;        this.node = node;        this.next = next;    }}public class MyLink<T> {    private Node<T> node;    public MyLink(T[] ts) throws MyException {        if (ts.length == 0) {            throw new MyException("初始化不能为空");        } else {            node = new Node<T>(null,ts[0],null);            for (int i = 1; i < ts.length; ++i) {                add(ts[i]);            }        }    }    /**     * @param now     * @throws MyException     * 新添加一个节点到末尾     */    public void add(T now) throws MyException {        node = last();        Node<T> nowNode = new Node<T>(node, now, null);        node.next = nowNode;    }    public void delete(){    }    public Integer size() throws MyException {        if (this.node != null) {            Node<T> mynode = first();            Integer i = 1;            while (mynode.next != null) {                i++;                mynode = mynode.next;            }            return i;        } else {            throw new MyException(this + "为空");        }    }    public String toString() {        Node<T> now = null;        try {            now = first();        } catch (MyException e) {            e.printStackTrace();        }        do {            System.out.print(now.node+",");            now = now.next;        } while (now!= null);        System.out.println();        return null;    }    /**     * @param index     * @return     * @throws MyException     * 循环index的次数,获取node的对象     */    public Node<T> get(Integer index) throws MyException {        Node<T> now = first();        int i = 0;        do{            if(i==index){                return now;            }            now = now.next;            i++;        }while(i<size());        return now;    }    /**     * @return     * @throws MyException     * 获取第一个元素     */    public Node<T> first() throws MyException {        if (node != null) {            if (node.next == null) {                return node;            } else {                Node<T> first = node;                while (first.pre != null) {                    first = first.pre;                }                return first;            }        } else {            throw new MyException(node + "不能为空");        }    }    /**     * @return     * @throws MyException     * 获取最后一个元素     */    public Node<T> last() throws MyException {        Node<T> last = node;        if (node != null) {            while (last.next != null) {                last = last.next;            }        } else {            throw new MyException(node + "不能为空");        }        return last;    }    /**     * @param index     * @param t     * @throws MyException     *  插入一个元素     */    public void insert(int index,T t) throws MyException{        if(index<size()-1&&index>1){            Node<T> now = get(index);            Node<T> before = get(index-1);            Node<T> insertNode = new Node<T>(before,t,now);            before.next = insertNode;            now.pre = insertNode;        }else{            throw new MyException("不在范围之类");        }    }

(三)ArrayList这里仿写的方法,是按照自己的想法写的,没有源码考虑的问题的多,相对来说很简陋。

public class MyList<T> {    Object[] ts;    Integer size=0;    Integer i = 0;    final Integer DEFAULT_CAPACITY  = 4;    Integer LARGER_CAPACITY;    public MyList(){        LARGER_CAPACITY = DEFAULT_CAPACITY;        ts = new Object[DEFAULT_CAPACITY ];    }    public void add(T t){        judgeSize();        ts[i++] = t;         size++;    }    /**     * @param t     * 根据元素 删除     */    public void remove(T t){        for(int i=0;i<size;i++){            if(ts[i]==t){                Object[] before = subArray(ts, 0, i-1);                Object[] after = subArray(ts, i+1, size);                ts = contact(before, after);                size--;                return;            }        }    }    /**     * 判断当前数组是否越界,越界则扩容     */    public void judgeSize(){        if(size>=LARGER_CAPACITY){            LARGER_CAPACITY = LARGER_CAPACITY*2;            Object[] desc = new Object[LARGER_CAPACITY];            ts = copy(ts, desc, 0, ts.length-1);        }    }    /**     * @param src       被复制的数组     * @param des       复制到该数组     * @param start     复制数组的起始位置     * @param end       复制的末尾数字     * @return     */    public Object[] copy(Object[] src,Object[] des,Integer start,Integer end){        if(start>=0 && start<end && end<src.length){            for(int i=start;i<=end;i++){                int j = i-start;                des[j] = src[i];            }        }        return des;    }    /**     * @param before     * @param after     * @return     * 连接两个数组     */    public Object[] contact(Object[] before,Object[] after){        Object[] result = new Object[LARGER_CAPACITY];        for(int i=0;i<result.length;i++){            if(i<before.length){                result[i] = before[i];            }else if(i<after.length){                int j = i-before.length;                result[i] = after[j];            }        }        return result;    }    public Object[] subArray(Object[] src,Integer start,Integer end){        Object[] des = new Object[end-start+1];        if(start>=0 && start<end && end<src.length){            for(int i=start,j=0;i<end;i++,j++){                des[j] = src[i];            }        }else if(start == end){            des[0] = src[start];        }        return des;    }    public String toString(){        System.out.print("[");        for(int i=0;i<size;i++){            System.out.print(ts[i]+",");        }        System.out.println("]");        return null;    }

(四)当要创建同步的List,而不用Vector时,可以用Collections中的内部类,创建同步的List,Set,Map,这里可以参考API。
(五)对于LinkedHashMap,HashMap,他们的区别:前者继承于HashMap,所以有HashMap的特性,同时它的内部类Entity多了两个成员变量,一个before,一个after。它还重写了部分方法。LinkedHashMap创建时,会有一个参数accessOrder,它决定是否以双向链表的形式存储数据,如果不以双向链表存储数据,则和HashMap性能相同。

0 0
原创粉丝点击