LinkedList重寫(5)LinkedList簡單的封裝和增加泛型

古道西風_哥哥發表於2018-12-23
package Study.Collection;

/**
 * <p>Description: </p>
 * 封裝,全部加泛型
 * @author zhangyan
 * @date 2018/12/23 10:05
 */
public class YanLinkedList04<E> {

    private   Node  first;
    private  Node  last;

    private  int size;


    public  void  add(int index, E  element) {   //alt+shift+R

        checkRange(index);

        Node  newNode = new Node(element);
        Node  temp  =  getNode(index);

        if(temp!=null){
            Node up = temp.previous;


            if(index==size){               //最後一個元素
                last.next = newNode;
                newNode.previous = last;
            }
            if(index==0){                  //第一個元素
                newNode.next = first;
                first.previous = newNode;
            }else {                        //正常情況下

                up.next = newNode;
                newNode.previous = up;

                newNode.next = temp;
                temp.previous = newNode;
            }

        }


    }


    public   void  remove(int index){

        checkRange(index);

        Node  temp  =  getNode(index);

        if(temp!=null){
            Node  up = temp.previous;
            Node down = temp.next;

            if(up!=null){
                up.next = down;
            }

            if(down!=null){
                down.previous = up;
            }

            //被刪除的元素是第一個元素時
            if(index==0){
                first = down;
            }

            //被刪除的元素是最後一個元素時
            if(index == size-1){
                last = up;
            }

            size--;
        }

    }

    //[]
    //["a","b","c","d","e","f"]           2
    public  E  get(int index) {

        checkRange(index);

        Node temp = getNode(index);

        return temp!=null?(E)temp.element:null;
    }

    //封裝一個checkRange() 方法
    private  void  checkRange(int index){
        if(index<0||index>size-1){
            throw  new RuntimeException("索引數字不合法:"+index);
        }
    }



    private   Node  getNode(int index){

        checkRange(index);

        Node  temp = null;

        if(index<=(size>>1)){   //size>>1相當於除以2
            temp = first;
            for(int i=0;i<index;i++){
                temp = temp.next;
            }
        }else{
            temp = last;
            for(int i=size-1; i>index;i--){
                temp = temp.previous;
            }
        }

        return temp;
    }




    public  void  add(E  element) {
        Node  node  = new Node(element);

        if(first==null){
            first = node;
            last = node;
        }else{

            node.previous = last;
            node.next = null;

            last.next = node;
            last = node;

        }
        size++;

    }

    public String toString() {

        StringBuilder   sb = new StringBuilder("[");
        Node  temp = first;
        while(temp!=null){
            sb.append(temp.element+",");
            temp = temp.next;
        }
        sb.setCharAt(sb.length()-1, ']');

        return sb.toString();
    }

    //測試
    public static void main(String[] args) {
        //增加泛型String約束
        YanLinkedList04<String>  list = new YanLinkedList04<>();

        list.add("a");
        list.add("b");

        System.out.println(list);

        System.out.println(list.get(1));


    }
}

解析:最後一步是對之前的優化,具體實現看程式碼!很簡單的

相關文章