1. 程式人生 > >數據結構之鏈表操作

數據結構之鏈表操作

index stat nbsp ID prev set append his 改進

對鏈表的增刪改查簡單實現

public class LinkedList<E> {

    private class Node{
        public Node next;
        public E e;

        public Node(E e){
            this.next = null;
            this.e = e;
        }

        public Node(E e, Node node){
            this.next = node;
            this.e = e;
        }
    }

    
private Node head; // 頭節點 private int size; public void addFirst(E e){ // 先變node.next的值,然後把node的值賦給head. /*Node node = new Node(e); node.next = head; head = node;*/ //OR Node node = new Node(e,head); head = node; size++; } public void
addLast(E e){ Node prev = head; Node node = new Node(e); while (prev.next != null){ prev = prev.next; } prev.next = node; size++; } public void add(int index ,E e){ if(index < 0 || index > size){ return; } Node node
= new Node(e); if(index == 0){ node.next = head; head = node; size++; }else{ Node prev = head; for (int i = 0; i < index-1; i++) { prev = prev.next; } node.next = prev.next; prev.next = node; size++; } } public E remove(int index){ Node del = new Node(null,null); if(index < 0 || index > size){ return del.e; } Node prev = head; if(index == 0){ del = head; head = del.next; size--; return del.e; }else{ for (int i = 0; i < index - 1; i++) { prev = prev.next; } del = prev.next; prev.next = del.next; del.next = null; size--; return del.e; } } public String toString(){ StringBuffer sb = new StringBuffer(); Node curr = head; for (int i = 0; i < size; i++) { sb.append(curr.e).append(" --> "); curr = curr.next; } sb.append("NULL"); return sb.toString(); } public static void main(String[] args) { LinkedList list = new LinkedList(); list.addFirst(1); System.out.println(list); list.addFirst(2); System.out.println(list); list.addFirst(3); System.out.println(list); list.addLast(4); System.out.println(list); list.addLast(5); System.out.println(list); list.add(1,6); System.out.println(list); list.add(2,7); System.out.println(list); list.add(7,8); System.out.println(list); list.remove(2); System.out.println(list); list.remove(0); System.out.println(list); } }

對上面實現的改進,增加一個虛擬頭結點,方便了增刪改查的實現

public class LinkedList2<E> {

    private class Node{
        public Node next;
        public E e;

        public Node(E e){
            this.next = null;
            this.e = e;
        }

        public Node(){
            this.next = null;
            this.e = e;
        }

    }

    private Node dummyHead;
    private int size;

    public LinkedList2(){
        this.dummyHead = new Node();
        this.size = 0;
    }

    public void addFist(E e){
       /* Node node = new Node(e);
        node.next = dummyHead.next;
        dummyHead.next = node;
        size++;*/
        add(0,e);
    }

    public void addLast(E e){
        /*Node node = new Node(e);
        Node prev = dummyHead.next;
        while (prev.next!= null){
            prev = prev.next;
        }
        prev.next = node;
        size++;*/
        add(size,e);
    }

    /**
     * 添加元素到第index位置
     * @param index
     * @param e
     */
    public void add(int index,E e){
        if(index < 0 || index > size){
            return;
        }
        Node node = new Node(e);
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        node.next = prev.next;
        prev.next = node;
        size++;
    }

    /**
     * 得到第index的值
     * @param index
     * @return
     */
    public E get(int index){
        if(index < 0 || index > size){
            return null;
        }
        Node curr = dummyHead.next;
        for (int i = 0; i < index; i++) {
            curr = curr.next;
        }
        return curr.e;
    }

    public E getFirst(){
        return get(0);
    }

    /**
     * 移出鏈表中第index
     * @param index
     * @return
     */
    public E remove(int index){
        if(index < 0 || index > size){
            return null;
        }
        Node del = null;
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        del = prev.next;
        prev.next = del.next;
        del.next = null;
        size--;
        return del.e;
    }

    /**
     * 更改鏈表中第index個的值
     * @param index
     * @param e
     */
    public void set(int index,E e){
        if(index < 0 || index > size){
            return;
        }
        Node curr = dummyHead.next;
        for (int i = 0; i < index; i++) {
            curr = curr.next;
        }
        curr.e = e;
    }

    public String toString(){
        StringBuffer sb = new StringBuffer();
        Node curr = dummyHead.next;
        while (curr!= null){
            sb.append(curr.e).append(" --> ");
            curr = curr.next;
        }
        sb.append("NULL");
        return sb.toString();
    }

    public static void main(String[] args) {
        LinkedList2 list = new LinkedList2();
        list.addFist(1);
        System.out.println(list);
        list.addFist(2);
        System.out.println(list);
        list.addFist(3);
        System.out.println(list);
        list.addLast(4);
        System.out.println(list);
        list.addLast(5);
        System.out.println(list);
        list.add(0,6);
        System.out.println(list);
        list.add(6,7);
        System.out.println(list);
        System.out.println(list.get(1));
        System.out.println(list.get(5));
        list.remove(1);
        System.out.println(list);
        list.remove(5);
        System.out.println(list);
        list.addLast(8);
        System.out.println(list);
        list.set(0,9);
        System.out.println(list);

    }
}

  

數據結構之鏈表操作