數(shù)據(jù)結(jié)構(gòu)(二)--循環(huán)鏈表結(jié)構(gòu)(Java實(shí)現(xiàn))

你是怎么把生活弄的一團(tuán)糟的 . . .
正常發(fā)揮罷了
接一下有什么打算 . . .
熬 !

生活充滿希望

用這段話形容自己的近況,簡(jiǎn)直了?。?!好吧,接下來(lái)搞定循環(huán)鏈表?。?!

概述

本文主要針對(duì)數(shù)據(jù)結(jié)構(gòu)中的循環(huán)鏈表結(jié)構(gòu)(包括單向和雙向)進(jìn)行詳細(xì)的講解并用java實(shí)現(xiàn)了鏈表結(jié)構(gòu)上的增刪改查操作。關(guān)于單鏈表結(jié)構(gòu)可參看作者的上一篇文章:數(shù)據(jù)結(jié)構(gòu)(一)---線性表(Java實(shí)現(xiàn))


單向循環(huán)鏈表

基本概念

如果把單鏈表的最后一個(gè)節(jié)點(diǎn)的指針指向鏈表頭部,而不是指向NULL,那么就構(gòu)成了一個(gè)單向循環(huán)鏈表。

帶頭節(jié)點(diǎn)的循環(huán)單鏈表結(jié)構(gòu)示意圖

代碼實(shí)現(xiàn)(Java)

package com.datastruct.learn.list;

/**
 * 循環(huán)單鏈表:表的最后一個(gè)結(jié)點(diǎn)的指針域指向頭結(jié)點(diǎn),整個(gè)表形成一個(gè)環(huán)
 *
 * @author zengsk
 * @date 2019/1/19
 **/

public class CycleLinkedList {

    class CycleNode {
        Object data;
        CycleNode next = null;

        CycleNode() {
            this.data = null;
        }

        CycleNode(Object data) {
            this.data = data;
        }
    }

    private CycleNode head, pos;
    private int size;

    //初始化時(shí),生成一個(gè)包含頭節(jié)點(diǎn)的空鏈表
    CycleLinkedList() {
        head = new CycleNode();
        head.next = head;
        pos = head;
        this.size = 0;

    }

    /**
     * 返回鏈表的大小
     * @return
     */
    public int len() {
        return this.size;
    }

    /**
     * 判斷鏈表是否為空
     * @return
     */
    public boolean isEmpty() {
        return this.size == 0;
    }

    /**
     * 添加元素,在末尾節(jié)點(diǎn)處添加
     * @param data
     */
    public void add(Object data) {
        CycleNode _node = new CycleNode(data);
        if (head.next == head) {
            head.next = _node;
            _node.next = head;
        } else {
            pos = head;
            while (pos.next != head) {
                pos = pos.next;
            }
            pos.next = _node;
            _node.next = head;
        }
        size++;
    }

    /**
     * 插入元素, 在指定索引位置處插入
     * @param index
     * @param data
     */
    public void insert(int index, Object data) {
        CycleNode _node = new CycleNode(data);
        if (index < 0 || index > size - 1)
            throw new RuntimeException("索引錯(cuò)誤" + index);
        int i = 0;
        pos = head;
        while (pos.next != head && i < index) {
            pos = pos.next;
            i++;
        }
        _node.next = pos.next;
        pos.next = _node;
        size++;
    }

    /**
     * 移除元素
     * @param index
     * @return
     */
    public Object remove(int index) {
        Object value = null;
        if (index < 0 || index > size - 1)
            throw new RuntimeException("索引錯(cuò)誤" + index);
        int i = 0;
        pos = head;
        while (pos.next != head && i < index) {
            pos = pos.next;
            i++;
        }
        value = pos.next.data;
        pos.next = pos.next.next;
        size--;
        return value;
    }

    /**
     * 替換元素
     * @param index
     * @param newObj
     * @return
     */
    public Object replace(int index, Object newObj) {
        Object value = null;
        if (index < 0 || index > size - 1)
            throw new RuntimeException("索引錯(cuò)誤" + index);
        int i = 0;
        pos = head;
        while (pos.next != head && i < index) {
            pos = pos.next;
            i++;
        }
        value = pos.next.data;
        pos.next.data = newObj;
        return value;
    }

    /**
     * 獲取元素
     * @param index
     * @return
     */
    public Object get(int index) {

        if (index < 0 || index > size - 1)
            throw new RuntimeException("索引錯(cuò)誤" + index);
        int i = 0;
        pos = head;
        while (pos.next != head && i < index) {
            pos = pos.next;
            i++;
        }
        return pos.next.data;
    }

    /**
     * 查詢?cè)厮饕?     * @param data
     * @return
     */
    public int indexOf(Object data) {

        int i = 0;
        pos = head;
        while (pos.next != head) {
            pos = pos.next;
            if (pos.data.equals(data))
                return i;
            i++;
        }
        return -1;
    }

    /**
     * 判斷是否包含指定元素
     * @param data
     * @return
     */
    public boolean contains(Object data) {

        int i = 0;
        pos = head;
        while (pos.next != head) {
            pos = pos.next;
            if (pos.data.equals(data))
                return true;
        }
        return false;
    }

    /**
     * 打印鏈表中的元素
     */
    public void show() {
        for (int i = 0; i < size; i++) {
            System.out.print(this.get(i) + "-->");
        }
    }

}


雙向循環(huán)鏈表

基本概念

雙向鏈表也叫雙鏈表,是鏈表的一種,它的每個(gè)數(shù)據(jù)結(jié)點(diǎn)中都有兩個(gè)指針,分別指向直接后繼和直接前驅(qū)。所以,從雙向鏈表中的任意一個(gè)結(jié)點(diǎn)開(kāi)始,都可以很方便地訪問(wèn)它的前驅(qū)結(jié)點(diǎn)和后繼結(jié)點(diǎn)。一般我們都構(gòu)造雙向循環(huán)鏈表。

  • 在雙向鏈表中,每個(gè)結(jié)點(diǎn)包括三個(gè)域,分別是data域、next域和prior域,其中data域?yàn)閿?shù)據(jù)元素域,next域?yàn)橹赶蚝罄^結(jié)點(diǎn)的對(duì)象引用,prior域?yàn)橹赶蚯膀?qū)結(jié)點(diǎn)的對(duì)象引用。
    帶頭節(jié)點(diǎn)的雙向循環(huán)鏈表示意圖

    代碼實(shí)現(xiàn)(Java)
package com.datastruct.learn.list;

/**
 * 設(shè)計(jì)實(shí)現(xiàn)一個(gè)雙向鏈表結(jié)構(gòu)
 *
 * @author zengsk
 * @date 2019/1/19
 **/

public class DualLinkedList {

    class DualNode {
        private Object data;
        private DualNode prior = null;
        private DualNode next = null;

        DualNode() {
            this.data = null;
        }

        DualNode(Object data) {
            this.data = data;
        }
    }

    private DualNode head, rear; //頭尾指針
    private int size;

    /**
     * 初始化一個(gè)空鏈表
     */
    DualLinkedList() {
        head = new DualNode();
        head.next = head;
        head.prior = head;
        this.rear = head.prior;
        this.size = 0;
    }

    /**
     * 獲取鏈表長(zhǎng)度
     *
     * @return
     */
    public int len() {
        return this.size;
    }

    /**
     * 判斷鏈表是否為空
     *
     * @return
     */
    public boolean isEmpty() {
        if (head.prior == head)
            return true;
        return false;
    }

    /**
     * 添加結(jié)點(diǎn)到表尾
     *
     * @param data
     */
    public void add(Object data) {
        DualNode _node = new DualNode(data);
        if (isEmpty()) { //鏈表為空情況
            _node.prior = head;
            _node.next = head;
            head.next = _node;
            head.prior = _node;
            this.rear = _node;
        } else {
            _node.prior = this.rear;
            _node.next = head;
            this.rear.next = _node;
            this.head.prior = _node;
            this.rear = _node;
        }
        size++;
    }

    /**
     * 在指定索引位置插入元素
     *
     * @param index
     * @param data
     */
    public void insert(int index, Object data) {

        DualNode _node = new DualNode(data);
        try {
            if (index < 0 || index > size - 1)
                throw new RuntimeException("索引錯(cuò)誤" + index);
            if (head.prior == head) {
                add(data);
            } else {
                int i = 0;
                DualNode pos = head;
                while (pos.next != rear && i < index) {
                    pos = pos.next;
                    i++;
                }
                _node.next = pos.next;
                _node.prior = pos;
                pos.next.prior = _node;
                pos.next = _node;
                size++;
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }

    /**
     * 移除指定索引位置的元素
     *
     * @param index
     * @return
     */
    public Object remove(int index) {
        Object data = null;
        if (index < 0 || index > size - 1)
            throw new RuntimeException("索引錯(cuò)誤" + index);
        if (head.prior == head)
            throw new RuntimeException("鏈表為空!!");
        DualNode pos = head;
        int i = 0;
        while (pos.next != rear && i < index) {
            pos = pos.next;
            i++;
        }

        data = pos.next.data;
        pos.next.next.prior = pos;
        pos.next = pos.next.next;
        size--;
        return data;
    }

    /**
     * 獲取指定索引位置的元素
     *
     * @param index
     * @return
     */
    public Object get(int index) {

        if (index < 0 || index > size - 1)
            throw new RuntimeException("索引錯(cuò)誤!!" + index);
        if (head.prior == head) {
            throw new RuntimeException("鏈表為空");
        }
        DualNode pos = head;
        int i = 0;
        while (pos.next != rear && i < index) {
            pos = pos.next;
            i++;
        }
        Object obj = pos.next.data;
        return obj;
    }

    /**
     * @param data
     * @return
     */
    public boolean contains(Object data) {
        DualNode pos = head;
        while (pos.next != rear) {
            pos = pos.next;
            if (pos.data.equals(data)) {
                return true;
            }

        }
        return false;
    }

    /**
     * 獲取指定元素所在的索引
     *
     * @param data
     * @return
     */
    public int indexOf(Object data) {
        DualNode pos = head;
        int i = 0;
        while (pos.next != rear) {
            pos = pos.next;
            if (pos.data.equals(data)) {
                return i;
            }
            i++;
        }
        return -1;
    }

    /**
     * 打印鏈表的所有元素
     */
    public void show() {
        for (int i = 0; i < size; i++) {
            System.out.print(this.get(i) + "-->");
        }
    }

}
每天進(jìn)步一點(diǎn)點(diǎn)就好?。?!睡覺(jué) . . .
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一些概念 數(shù)據(jù)結(jié)構(gòu)就是研究數(shù)據(jù)的邏輯結(jié)構(gòu)和物理結(jié)構(gòu)以及它們之間相互關(guān)系,并對(duì)這種結(jié)構(gòu)定義相應(yīng)的運(yùn)算,而且確保經(jīng)過(guò)這...
    Winterfell_Z閱讀 6,575評(píng)論 0 13
  • 目錄 1、屬性 2、鏈表和數(shù)組的區(qū)別 2.1、數(shù)組概述 2.2、數(shù)組和鏈表優(yōu)缺點(diǎn) 2.3、鏈表和數(shù)組的比較 3、單...
    我哈啊哈啊哈閱讀 2,935評(píng)論 1 41
  • 一、線性表的順序存儲(chǔ)設(shè)計(jì)與實(shí)現(xiàn)(順序表) 1.1 順序存儲(chǔ)結(jié)構(gòu)的設(shè)計(jì)原理概要 順序存儲(chǔ)結(jié)構(gòu)底層是利用數(shù)組來(lái)實(shí)現(xiàn)的,...
    千涯秋瑟閱讀 1,566評(píng)論 2 4
  • 投射 今天是富足豐盛的一天,自己心態(tài)平和,耐心細(xì)致做事,輕松狀態(tài)做人,歡迎并接納問(wèn)題的存在。投射兒子學(xué)習(xí)輕松,心情...
    靜_8ec3閱讀 245評(píng)論 2 0
  • “這凳子太討厭了,干嘛要擋在這兒……”悅滴滴咕咕說(shuō)了一堆凳子的不是。 “我聽(tīng)到小凳子說(shuō)它也摔疼了!它在這兒都沒(méi)動(dòng)就...
    sweet_smelling閱讀 269評(píng)論 0 2

友情鏈接更多精彩內(nèi)容