單鏈表及其應用

1.鏈表是一種常見的基礎數據結構,是一種線性表,可是並不會按線性的順序存儲數據,而是在每個節點裏存到下一個節點的指針(Pointer)。數組

使用鏈表結構能夠克服數組鏈表須要預先知道數據大小的缺點,鏈表結構能夠充分利用計算機內存空間,實現靈活的內存動態管理。可是鏈表失去了數組隨機讀取的優勢,同時鏈表因爲增長告終點的指針域,空間開銷比較大。數據結構

2.單鏈表是鏈表中結構最簡單的。一個單鏈表的節點(Node)分爲兩個部分,第一個部分(data)保存或者顯示關於節點的信息,另外一個部分存儲下一個節點的地址。最後一個節點存儲地址的部分指向空值。app

 

 3.鏈表實現ui

package signle;

/**
 * @Author lizhilong
 * @create 2019/11/7 17:12
 * @desc
 */
public class SignleLinkList {

    private int size;

    private Node head;

    public SignleLinkList() {
        this.size = 0;
        this.head = null;
    }

    /**
     * 鏈表頭後面增長節點
     *
     * @param object
     * @return
     */
    public Object addAfterHead(Object object) {
        Node newNode = new Node(object);
        if (size == 0) {
            head = newNode;
        } else {
            Node current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = newNode;
        }
        size++;
        return object;
    }

    /**
     * 在鏈表頭前增長節點
     *
     * @param object
     * @return
     */
    public Object addbeforeHead(Object object) {
        Node newNode = new Node(object);
        if (size == 0) {
            head = newNode;
        } else {
            newNode.next = head;
            head = newNode;
        }
        size++;
        return object;
    }

    /**
     * 從頭刪除
     *
     * @return
     */
    public Object remove() {
        Object data = head.data;
        head = head.next;
        size--;
        return data;
    }


    /**
     * 查找位置
     *
     * @param object
     * @return
     */
    public int find(Object object) {
        Node h = head;
        int index = -1;
        while (h.data != object) {
            index++;
            h = h.next;
        }
        return index + 1;
    }

    /**
     * 刪除指定元素
     *
     * @param object
     * @return
     */
    public boolean delete(Object object) {
        if (size == 0) {
            return false;
        }
        Node current = head;
        Node preNode = head;
        while (current.data != object) {
            if (current.next == null) {
                return false;
            } else {
                preNode = current;
                current = current.next;
            }
        }
        //目標元素是頭節點
        if (current == head) {
            head = current.next;
            size--;
        } else {
            preNode.next = current.next;
            size--;
        }
        return true;
    }

    /**
     * 打印鏈表全部元素
     */
    public void display() {
        Node current = head;
        StringBuilder builder = new StringBuilder();
        builder.append("[");
        while (current.next != null) {
            builder.append(current.data.toString() + "->");
            current = current.next;
        }
        builder.append(current.data.toString());
        builder.append("]");
        System.out.println(builder.toString());
    }

    /**
     * 單鏈表反轉
     */
    public SignleLinkList reverse() {
       SignleLinkList rev = new SignleLinkList();
       Node h = head;
       while (h.next != null){
           rev.addbeforeHead(h.data);
           h = h.next;
       }
       rev.addbeforeHead(h.data);
       return  rev;
    }


    public  int getSize(){
        return  size;
    }
    /**
     * @Author lizhilong
     * @create 2019/11/7 17:10
     * @desc
     */
    public class Node {

        private Object data;

        private Node next;

        public Node(Object data) {
            this.data = data;
        }
    }
}

4.鏈表應用之實現棧this

package signle;

/**
 * @Author lizhilong
 * @create 2019/11/8 15:32
 * @desc
 */
public class Stack {

    private  SignleLinkList linkList;

    public Stack(){
        linkList = new SignleLinkList();
    }

    /**
     * 入棧
     * @param data
     */
    public  void  push(Object data){
        linkList.addbeforeHead(data);
    }


    public Object  pop(){
        return  linkList.remove();
    }


    public  void  display(){
        linkList.display();
    }
}

5.鏈表應用之實現隊列spa

package signle;

/**
 * @Author lizhilong
 * @create 2019/11/8 16:00
 * @desc
 */
public class Queue {

    private SignleLinkList linkList;

    public  Queue(){
        linkList = new SignleLinkList();
    }

    public void  put(Object data){
        linkList.addAfterHead(data);
    }

    public Object take(){
        return  linkList.remove();
    }

    public void display(){
        linkList.display();
    }
}
相關文章
相關標籤/搜索