国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【一起學(xué)數(shù)據(jù)結(jié)構(gòu)與算法】快速教你了解并實(shí)現(xiàn)單鏈表

這篇具有很好參考價(jià)值的文章主要介紹了【一起學(xué)數(shù)據(jù)結(jié)構(gòu)與算法】快速教你了解并實(shí)現(xiàn)單鏈表。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

前言

此篇是對(duì)單鏈表知識(shí)的學(xué)習(xí)和實(shí)現(xiàn),基本上大體的方法實(shí)現(xiàn)和思路都已經(jīng)表達(dá),如果有不對(duì)的地方,還請(qǐng)各位大佬多多指教!

一、單鏈表的概念

單鏈表是一種鏈?zhǔn)酱嫒〉臄?shù)據(jù)結(jié)構(gòu),用一組地址任意的存儲(chǔ)單元存放線性表中的數(shù)據(jù)元素。鏈表中的數(shù)據(jù)是以結(jié)點(diǎn)來表示的,每個(gè)結(jié)點(diǎn)的構(gòu)成:元素(數(shù)據(jù)元素的映象) + 指針(指示后繼元素存儲(chǔ)位置),元素就是存儲(chǔ)數(shù)據(jù)的存儲(chǔ)單元,指針就是連接每個(gè)結(jié)點(diǎn)的地址數(shù)據(jù)。

二、單鏈表內(nèi)一些方法的實(shí)現(xiàn)

2.1 單鏈表長(zhǎng)什么樣子?

單鏈表不教鏈接嗎,一起學(xué)數(shù)據(jù)結(jié)構(gòu)與算法系列
像上面這樣式的就是一種單鏈表!

由上圖可以看出我們一些屬性來表示單鏈表!

class ListNode {
    public int val;
    public ListNode next;

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

2.2 創(chuàng)建單鏈表

創(chuàng)建鏈表之前我們需要定義一個(gè)成員變量head表示鏈表的頭結(jié)點(diǎn)!

 //成員變量
    public ListNode head;//鏈表的頭引用

由于咱們是初步學(xué)習(xí)單鏈表,所有咱們就簡(jiǎn)單的創(chuàng)建的單鏈表

public void createList() {

        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(34);
        ListNode listNode4 = new ListNode(45);
        ListNode listNode5 = new ListNode(56);

        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;

    }

2.3 打印單鏈表

public void display() {
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

為什么會(huì)定義一個(gè)cur來指向head了?
那是因?yàn)槿绻胔ead來遍歷鏈表,那么最后head就指向了null,就找不到鏈表的頭了,所以使用cur來完成操作!

2.4 查找是否包含關(guān)鍵字key是否在單鏈表中

我們只需要拿cur去遍歷鏈表,看cur是否等于key,如果有就返回true,沒有就返回false。
實(shí)現(xiàn)cur遍歷的過程的代碼就是:cur = cur.next;

//查找是否包含關(guān)鍵字key是否在單鏈表中
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

2.5 得到單鏈表的長(zhǎng)度

一樣的操作,此處通過定義一個(gè)count作為計(jì)數(shù)器,直到cur遍歷完整個(gè)鏈表就停止.

//得到單鏈表的長(zhǎng)度
    public int size() {
        int count = 0;
        ListNode cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

2.6 addFirst – 頭插

//頭插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
        /*if (this.head == null) {
            this.head = node;
        } else {
            node.next = this.head;
            this.head = node;
        }*/
    }

單鏈表不教鏈接嗎,一起學(xué)數(shù)據(jù)結(jié)構(gòu)與算法系列

2.7 addLast – 尾插

//尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (this.head == null) {
            this.head = node;
        } else {
            ListNode cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

單鏈表不教鏈接嗎,一起學(xué)數(shù)據(jù)結(jié)構(gòu)與算法系列

2.8 addIndex – 任意位置插入

/**
     * 找到index - 1 位置結(jié)點(diǎn)的地址
     * @param index
     * @return
     */
    public ListNode findIndex(int index) {
        ListNode cur = this.head;
        while (index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //任意位置插入,第一個(gè)數(shù)據(jù)結(jié)點(diǎn)為0號(hào)下標(biāo)
    public void addIndex(int index, int data) {
        if (index < 0 || index > size()) {
            System.out.println("index位置不合法!");
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = findIndex(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }

單鏈表不教鏈接嗎,一起學(xué)數(shù)據(jù)結(jié)構(gòu)與算法系列

2.9 remove – 刪除第一次出現(xiàn)的key

 /**
     * 找到要?jiǎng)h除關(guān)鍵字的前驅(qū)
     * @param key
     * @return
     */
    public ListNode searchPrev(int key) {
        ListNode cur = this.head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //刪除第一次出現(xiàn)的關(guān)鍵字key的結(jié)點(diǎn)
    public void remove(int key) {
        if (this.head == null) {
            System.out.println("單鏈表為空,不能刪除!");
            return;
        }
        if (this.head.val == key) {
            this.head = this.head.next;
            return;
        }
        ListNode cur = searchPrev(key);
        if (cur == null) {
            System.out.println("沒有你要?jiǎng)h除的結(jié)點(diǎn)!");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }

單鏈表不教鏈接嗎,一起學(xué)數(shù)據(jù)結(jié)構(gòu)與算法系列

2.10 removeAllkey – 刪除所有值為key的結(jié)點(diǎn)

//刪除所有值為key的結(jié)點(diǎn)
    public ListNode removeAllKey(int key) {
        if (this.head == null) return null;

        ListNode prev = this.head;
        ListNode cur = this.head.next;

        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        //最后處理頭
        if (this.head.val == key) {
            this.head = this.head.next;
        }
        return this.head;
    }

1.如果先處理頭,則需要寫成循環(huán),因?yàn)楫?dāng)鏈表所有結(jié)點(diǎn)都是待刪除的情況時(shí),一個(gè)if條件語(yǔ)句處 理不了

2.while循環(huán)里面的條件不能寫成cur.next == null,因?yàn)閞emoveSubOne函數(shù)如果沒找到待刪除 的結(jié)點(diǎn),它返回的是一個(gè)null,如果寫成cur.next != null,則可能會(huì)報(bào)空指針異常文章來源地址http://www.zghlxwxcb.cn/news/detail-827737.html

2.11 清空單鏈表

public void clear() {
        this.head = null;
    }

三、MyLinkedList.java

import java.util.LinkedList;

@SuppressWarnings({"all"})

class ListNode {
    public int val;
    public ListNode next;

    public ListNode(int val) {
        this.val = val;
    }
}
public class MyLinkedList {

    //成員變量
    public ListNode head;//鏈表的頭引用

    public void createList() {

        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(34);
        ListNode listNode4 = new ListNode(45);
        ListNode listNode5 = new ListNode(56);

        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;

    }

    public void display() {
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //查找是否包含關(guān)鍵字key是否在單鏈表中
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //得到單鏈表的長(zhǎng)度
    public int size() {
        int count = 0;
        ListNode cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //頭插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
        /*if (this.head == null) {
            this.head = node;
        } else {
            node.next = this.head;
            this.head = node;
        }*/
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (this.head == null) {
            this.head = node;
        } else {
            ListNode cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    /**
     * 找到index - 1 位置結(jié)點(diǎn)的地址
     * @param index
     * @return
     */
    public ListNode findIndex(int index) {
        ListNode cur = this.head;
        while (index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //任意位置插入,第一個(gè)數(shù)據(jù)結(jié)點(diǎn)為0號(hào)下標(biāo)
    public void addIndex(int index, int data) {
        if (index < 0 || index > size()) {
            System.out.println("index位置不合法!");
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = findIndex(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }

    /**
     * 找到要?jiǎng)h除關(guān)鍵字的前驅(qū)
     * @param key
     * @return
     */
    public ListNode searchPrev(int key) {
        ListNode cur = this.head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //刪除第一次出現(xiàn)的關(guān)鍵字key的結(jié)點(diǎn)
    public void remove(int key) {
        if (this.head == null) {
            System.out.println("單鏈表為空,不能刪除!");
            return;
        }
        if (this.head.val == key) {
            this.head = this.head.next;
            return;
        }
        ListNode cur = searchPrev(key);
        if (cur == null) {
            System.out.println("沒有你要?jiǎng)h除的結(jié)點(diǎn)!");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }

    //刪除所有值為key的結(jié)點(diǎn)
    public ListNode removeAllKey(int key) {
        if (this.head == null) return null;

        ListNode prev = this.head;
        ListNode cur = this.head.next;

        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        //最后處理頭
        if (this.head.val == key) {
            this.head = this.head.next;
        }
        return this.head;
    }

    public void clear() {
        this.head = null;
    }
}

四、Test.java

public class Test {
    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
       /* myLinkedList.createList();
        myLinkedList.display();
        boolean flag = myLinkedList.contains(34);
        System.out.println(flag);
        int size = myLinkedList.size();
        System.out.println(size);*/
        myLinkedList.addFirst(12);
        myLinkedList.addFirst(23);
        myLinkedList.addLast(39);
        myLinkedList.addLast(37);
        myLinkedList.addIndex(0,99);
        myLinkedList.addIndex(5,99);
        myLinkedList.addIndex(3,99);
        myLinkedList.display();
        myLinkedList.remove(12);
        myLinkedList.removeAllKey(99);
        myLinkedList.display();
        myLinkedList.clear();
        myLinkedList.display();

    }
}

到了這里,關(guān)于【一起學(xué)數(shù)據(jù)結(jié)構(gòu)與算法】快速教你了解并實(shí)現(xiàn)單鏈表的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 手把手教你 ,帶你徹底掌握八大排序算法【數(shù)據(jù)結(jié)構(gòu)】

    手把手教你 ,帶你徹底掌握八大排序算法【數(shù)據(jù)結(jié)構(gòu)】

    直接插入排序是一種簡(jiǎn)單的插入排序法,其基本思想:是把待排序的記錄按其關(guān)鍵碼值的大小逐個(gè)插入到一個(gè)已經(jīng)排好序的有序序列中,直到所有的記錄插入完為止,得到一個(gè)新的有序序列 可以理解為一遍摸撲克牌,一邊進(jìn)行排序 在待排序的元素中,假設(shè)前面n-1(其中n=2)個(gè)數(shù)

    2024年02月02日
    瀏覽(106)
  • 數(shù)據(jù)結(jié)構(gòu)--》深入了解棧和隊(duì)列,讓算法更加高效

    數(shù)據(jù)結(jié)構(gòu)--》深入了解棧和隊(duì)列,讓算法更加高效

    ????????本文將帶你深入了解數(shù)據(jù)結(jié)構(gòu)棧和隊(duì)列,這兩種基礎(chǔ)的線性數(shù)據(jù)結(jié)構(gòu)在算法中的重要性不言而喻。我們將會(huì)詳細(xì)介紹棧和隊(duì)列的概念、分類、實(shí)現(xiàn)以及應(yīng)用場(chǎng)景,在理解棧和隊(duì)列的基礎(chǔ)上,還將探討如何通過棧和隊(duì)列來高效地解決算法問題。 ????????無論你是

    2024年02月10日
    瀏覽(20)
  • 數(shù)據(jù)結(jié)構(gòu)與算法:快速排序

    數(shù)據(jù)結(jié)構(gòu)與算法:快速排序

    荷蘭國(guó)旗問題 想要理解快速排序,就先理解這個(gè)問題: [LeetCode75.顏色分類] 荷蘭國(guó)旗是由紅白藍(lán)三色組成的: 現(xiàn)在將其顏色打亂 然后根據(jù)一定的算法,將其復(fù)原為紅白藍(lán)三色,這就叫做荷蘭國(guó)旗問題。 在LeetCode的題目中,其將荷蘭國(guó)旗的三個(gè)顏色用0,1,2來表達(dá),也就是說

    2024年01月15日
    瀏覽(24)
  • 數(shù)據(jù)結(jié)構(gòu)——排序算法之快速排序

    數(shù)據(jù)結(jié)構(gòu)——排序算法之快速排序

    ? ??個(gè)人主頁(yè): 日刷百題 系列專欄 : 〖C/C++小游戲〗 〖Linux〗 〖數(shù)據(jù)結(jié)構(gòu)〗 ? 〖C語(yǔ)言〗 ?? 歡迎各位 → 點(diǎn)贊 ??+ 收藏 ??+ 留言 ??? ? ? 快速排序是Hoare于1962年提出的一種二叉樹結(jié)構(gòu)的交換排序方法。 基本思想: 任取待排序元素序列中 的某元素作為基準(zhǔn)值,按照

    2024年01月21日
    瀏覽(23)
  • 數(shù)據(jù)結(jié)構(gòu)與算法之快速排序

    快速排序 (Quick Sort),又稱劃分交換排序(partition-exchange sort),通過一趟排序?qū)⒁判虻臄?shù)據(jù)分割成獨(dú)立的兩部分,其中一部分的所有數(shù)據(jù)都比另外一部分的所有數(shù)據(jù)都要小,然后再按此方法對(duì)這兩部分?jǐn)?shù)據(jù)分別進(jìn)行快速排序,整個(gè)排序過程可以遞歸進(jìn)行,以此達(dá)到整個(gè)數(shù)

    2024年02月10日
    瀏覽(21)
  • 【數(shù)據(jù)結(jié)構(gòu)與算法】三個(gè)經(jīng)典案例帶你了解動(dòng)態(tài)規(guī)劃

    【數(shù)據(jù)結(jié)構(gòu)與算法】三個(gè)經(jīng)典案例帶你了解動(dòng)態(tài)規(guī)劃

    從表中我們可以看到,最大的公共子串長(zhǎng)度為2,一共有兩個(gè)長(zhǎng)度為2的公共子串,分別是第一個(gè)字符串的第2個(gè)字符到第3個(gè)字符和第一個(gè)字符串的第3個(gè)字符到第4個(gè)字符,即 ba 和 ac 根據(jù)上面的方法,我們來用代碼封裝一下求取最大公共子串的函數(shù) function publicStr(s1, s2) { // 創(chuàng)建

    2024年04月09日
    瀏覽(20)
  • 【數(shù)據(jù)結(jié)構(gòu)與算法】十大經(jīng)典排序算法-快速排序

    【數(shù)據(jù)結(jié)構(gòu)與算法】十大經(jīng)典排序算法-快速排序

    ?? 個(gè)人博客: www.hellocode.top ?? Java知識(shí)導(dǎo)航: Java-Navigate ?? CSDN: HelloCode. ?? 知乎 :HelloCode ?? 掘金 :HelloCode ?如有問題,歡迎指正,一起學(xué)習(xí)~~ 快速排序(Quick Sort)是一種高效的排序算法,是對(duì)冒泡排序的優(yōu)化。它采用分治法(Divide and Conquer)的思想,將待排序序列

    2024年02月13日
    瀏覽(24)
  • 【數(shù)據(jù)結(jié)構(gòu)與算法】:選擇排序與快速排序

    【數(shù)據(jù)結(jié)構(gòu)與算法】:選擇排序與快速排序

    ?? 個(gè)人主頁(yè) : Quitecoder ?? 專欄 :數(shù)據(jù)結(jié)構(gòu)與算法 我的博客即將同步至騰訊云開發(fā)者社區(qū),邀請(qǐng)大家一同入駐:騰訊云 歡迎來到排序的第二個(gè)部分:選擇排序與快速排序! 選擇排序是一種簡(jiǎn)單直觀的比較排序算法。該算法的基本思想是在每一輪中選出當(dāng)前未排序部分的最

    2024年03月17日
    瀏覽(25)
  • 【數(shù)據(jù)結(jié)構(gòu)與算法】【約瑟夫問題】還在用遞歸?教你用鏈表秒殺約瑟夫

    ?????歡迎光臨???? ??我是蘇澤,一位對(duì)技術(shù)充滿熱情的探索者和分享者。???? ??特別推薦給大家我的最新專欄 《數(shù)據(jù)結(jié)構(gòu)與算法:初學(xué)者入門指南》???? 本專欄純屬為愛發(fā)電永久免費(fèi)?。?! 這是蘇澤的個(gè)人主頁(yè)可以看到我其他的內(nèi)容哦???? 努力的蘇澤 http://su

    2024年02月19日
    瀏覽(18)
  • 數(shù)據(jù)結(jié)構(gòu)和算法——快速排序(算法概述、選主元、子集劃分、小規(guī)模數(shù)據(jù)的處理、算法實(shí)現(xiàn))

    數(shù)據(jù)結(jié)構(gòu)和算法——快速排序(算法概述、選主元、子集劃分、小規(guī)模數(shù)據(jù)的處理、算法實(shí)現(xiàn))

    目錄 算法概述 圖示 偽代碼 選主元 子集劃分 小規(guī)模數(shù)據(jù)的處理 算法實(shí)現(xiàn) 快速排序和歸并排序有一些相似,都是用到了分而治之的思想: ? 通過初步的認(rèn)識(shí),我們能夠知道快速排序算法最好的情況應(yīng)該是: 每次都正好中分 ,即每次選主元都為元素的中位數(shù)的位置。 最好情

    2024年02月15日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包