最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)雙鏈表的示例代碼

 更新時(shí)間:2022年09月26日 09:10:37   作者:熬夜磕代碼丶  
雙向鏈表也叫雙鏈表,是鏈表的一種,它的每個(gè)數(shù)據(jù)結(jié)點(diǎn)中都有兩個(gè)指針,分別指向直接后繼和直接前驅(qū)。本文將用Java語言實(shí)現(xiàn)雙鏈表,需要的可以參考一下

一、雙向鏈表是什么

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

LinkedList底層就是一個(gè)雙向鏈表,我們來實(shí)現(xiàn)一個(gè)雙向鏈表。

這里多一個(gè)尾指針,方便我們對(duì)尾插操作從O(n)降到O(1).每個(gè)結(jié)點(diǎn)多了前驅(qū)結(jié)點(diǎn),方便我們對(duì)鏈表進(jìn)行操作。

二、具體方法實(shí)現(xiàn)

定義結(jié)點(diǎn)

class ListNode {
        int value;
        ListNode next;
        ListNode prev;

        public ListNode(int value) {
            this.value = value;
        }
    }

下標(biāo)訪問異常

public class IndexWrongException extends RuntimeException{
    public IndexWrongException() {
    }

    public IndexWrongException(String message) {
        super(message);
    }
}

獲取鏈表長(zhǎng)度

class ListNode {
        int value;
        ListNode next;
        ListNode prev;

        public ListNode(int value) {
            this.value = value;
        }
    }

打印鏈表

class ListNode {
        int value;
        ListNode next;
        ListNode prev;

        public ListNode(int value) {
            this.value = value;
        }
    }

清空鏈表

public void clear(){
        if(this.head == null) {
            return;
        }
        ListNode cur = this.head;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        tail = null;
    }

頭插法

public void addFirst(int data){
        ListNode node = new ListNode(data);
        if(head == null) {
            this.head = node;
            this.tail = node;
            return;
        }
        node.next = this.head;
        this.head.prev = node;
        this.head = node;
    }

尾插法

public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head == null) {
            this.head = node;
            this.tail = node;
            return;
        }
        tail.next = node;
        node.prev = tail;
        tail = node;
    }

指定位置插入

public void addIndex(int index,int data) throws IndexWrongException{
        if(index < 0 || index > size()) {
            throw new IndexWrongException("輸入下標(biāo)不合法");
        }
        ListNode node = new ListNode(data);
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = this.head;
        while(index != 0) {
            cur = cur.next;
            index--;
        }
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }

查找元素

public boolean contains(int key){
        if(head == null) {
            return false;
        }
        ListNode cur = this.head;
        while(cur != null) {
            if(cur.value == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

刪除第一次出現(xiàn)的關(guān)鍵字

public void remove(int key){
        ListNode cur = head;
        while(cur != head) {
            if(cur.value == key) {
                if(cur == head) {
                    head = head.next;
                    if(head.next != null) {
                        head.prev = null;
                    }else {
                        tail = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next != null) {
                        cur.next.prev = cur.prev;
                    }else {
                        tail = cur.prev;
                        tail.next = null;
                        }
                    }
                return;
                }
            cur = cur.next;
            }
        }

刪除所有值為key的節(jié)點(diǎn)

public void removeAllKey(int key){
        ListNode cur = head;
        while(cur != head) {
            if(cur.value == key) {
                if(cur == head) {
                    head = head.next;
                    if(head.next != null) {
                        head.prev = null;
                    }else {
                        tail = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next != null) {
                        cur.next.prev = cur.prev;
                    }else {
                        tail = cur.prev;
                        tail.next = null;
                    }
                }
            }
            cur = cur.next;
        }
    }

三、完整代碼

public class LinkedList {
    static class ListNode {
        int value;
        ListNode next;
        ListNode prev;

        public ListNode(int value) {
            this.value = value;
        }
    }
    ListNode head;
    ListNode tail;
    //頭插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if(head == null) {
            this.head = node;
            this.tail = node;
            return;
        }
        node.next = this.head;
        this.head.prev = node;
        this.head = node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head == null) {
            this.head = node;
            this.tail = node;
            return;
        }
        tail.next = node;
        node.prev = tail;
        tail = node;
    }
    //任意位置插入,第一個(gè)數(shù)據(jù)節(jié)點(diǎn)為0號(hào)下標(biāo)
    public void addIndex(int index,int data) throws IndexWrongException{
        if(index < 0 || index > size()) {
            throw new IndexWrongException("輸入下標(biāo)不合法");
        }
        ListNode node = new ListNode(data);
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = this.head;
        while(index != 0) {
            cur = cur.next;
            index--;
        }
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }
    //查找是否包含關(guān)鍵字key是否在單鏈表當(dāng)中
    public boolean contains(int key){
        if(head == null) {
            return false;
        }
        ListNode cur = this.head;
        while(cur != null) {
            if(cur.value == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //刪除第一次出現(xiàn)關(guān)鍵字為key的節(jié)點(diǎn)
    public void remove(int key){
        ListNode cur = head;
        while(cur != head) {
            if(cur.value == key) {
                if(cur == head) {
                    head = head.next;
                    if(head.next != null) {
                        head.prev = null;
                    }else {
                        tail = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next != null) {
                        cur.next.prev = cur.prev;
                    }else {
                        tail = cur.prev;
                        tail.next = null;
                        }
                    }
                return;
                }
            cur = cur.next;
            }
        }
    //刪除所有值為key的節(jié)點(diǎn)
    public void removeAllKey(int key){
        ListNode cur = head;
        while(cur != head) {
            if(cur.value == key) {
                if(cur == head) {
                    head = head.next;
                    if(head.next != null) {
                        head.prev = null;
                    }else {
                        tail = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next != null) {
                        cur.next.prev = cur.prev;
                    }else {
                        tail = cur.prev;
                        tail.next = null;
                    }
                }
            }
            cur = cur.next;
        }
    }
    //得到單鏈表的長(zhǎng)度
    public int size(){
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }
    public void display(){
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.value+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void clear(){
        if(this.head == null) {
            return;
        }
        ListNode cur = this.head;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        tail = null;
    }
}

到此這篇關(guān)于Java實(shí)現(xiàn)雙鏈表的示例代碼的文章就介紹到這了,更多相關(guān)Java雙鏈表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring延遲Bean初始化的實(shí)現(xiàn)示例

    Spring延遲Bean初始化的實(shí)現(xiàn)示例

    延遲初始化也叫做惰性初始化,指不提前初始化Bean,而是只有在真正使用時(shí)才創(chuàng)建及初始化Bean,本文主要介紹了Spring延遲Bean初始化的實(shí)現(xiàn)示例,感興趣的可以了解一下
    2024-06-06
  • SpringBoot 集成 Druid過程解析

    SpringBoot 集成 Druid過程解析

    這篇文章主要介紹了SpringBoot 集成 Druid過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Java數(shù)字簽名算法DSA實(shí)例詳解

    Java數(shù)字簽名算法DSA實(shí)例詳解

    這篇文章主要介紹了Java數(shù)字簽名算法DSA,結(jié)合實(shí)例形式分析了Java數(shù)字簽名算法DSA具體定義與使用技巧,需要的朋友可以參考下
    2018-05-05
  • JDK的一個(gè)Bug監(jiān)聽文件變更的初步實(shí)現(xiàn)思路

    JDK的一個(gè)Bug監(jiān)聽文件變更的初步實(shí)現(xiàn)思路

    這篇文章主要介紹了JDK的一個(gè)Bug監(jiān)聽文件變更要小心了,本篇文章就帶大家簡(jiǎn)單實(shí)現(xiàn)一個(gè)對(duì)應(yīng)的功能,并分析一下對(duì)應(yīng)的Bug和優(yōu)缺點(diǎn),需要的朋友可以參考下
    2022-05-05
  • Nacos?Discovery服務(wù)治理解決方案

    Nacos?Discovery服務(wù)治理解決方案

    DiscoveryClient是專門負(fù)責(zé)服務(wù)注冊(cè)和發(fā)現(xiàn)的,我們可以通過它獲取到注冊(cè)到注冊(cè)中心的所有服務(wù),這篇文章主要介紹了Nacos?Discovery服務(wù)治理,需要的朋友可以參考下
    2022-11-11
  • Java 時(shí)間格式轉(zhuǎn)換之impleDateFormat與Data API解析與使用

    Java 時(shí)間格式轉(zhuǎn)換之impleDateFormat與Data API解析與使用

    想必大家對(duì) SimpleDateFormat 并不陌生。SimpleDateFormat 是 Java 中一個(gè)非常常用的類,他是以區(qū)域敏感的方式格式化和解析日期的具體類。 它允許格式化 (date -> text)、語法分析 (text -> date)和標(biāo)準(zhǔn)化
    2021-11-11
  • JPA中@JoinColumn的name和referencedColumnName屬性的區(qū)別及說明

    JPA中@JoinColumn的name和referencedColumnName屬性的區(qū)別及說明

    這篇文章主要介紹了JPA中@JoinColumn的name和referencedColumnName屬性的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Java多線程中斷機(jī)制三種方法及示例

    Java多線程中斷機(jī)制三種方法及示例

    這篇文章主要介紹了Java多線程中斷機(jī)制三種方法及示例,向大家分享了這三種方法的介紹幾代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • SpringBoot Mybatis批量插入Oracle數(shù)據(jù)庫數(shù)據(jù)

    SpringBoot Mybatis批量插入Oracle數(shù)據(jù)庫數(shù)據(jù)

    這篇文章主要介紹了SpringBoot Mybatis批量插入Oracle數(shù)據(jù)庫數(shù)據(jù),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • 本真的REST架構(gòu)風(fēng)格理解

    本真的REST架構(gòu)風(fēng)格理解

    這篇文章主要為大家介紹了本真的REST架構(gòu)風(fēng)格的深入理解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03

最新評(píng)論

宁武县| 牟定县| 虎林市| 韩城市| 白朗县| 蒙山县| 泰来县| 巩留县| 开封县| 年辖:市辖区| 佛山市| 上犹县| 连山| 库车县| 元氏县| 尼玛县| 永德县| 昆山市| 安图县| 东光县| 马边| 墨竹工卡县| 二手房| 永定县| 甘德县| 台山市| 嘉义市| 老河口市| 邓州市| 盖州市| 临桂县| 孝昌县| 沙雅县| 屏东市| 于田县| 龙江县| 吕梁市| 黄平县| 呼图壁县| 闽清县| 新乡县|