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

Java實(shí)現(xiàn)單鏈表基礎(chǔ)操作

 更新時間:2022年02月09日 10:13:25   作者:緣分锝天空(李延勝)  
大家好,本篇文章主要講的是Java實(shí)現(xiàn)單鏈表基礎(chǔ)操作,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下

關(guān)于鏈表

鏈表是有序的列表鏈表是以節(jié)點(diǎn)的方式來存儲每個節(jié)點(diǎn)包含data域,next域(指向下一個節(jié)點(diǎn))分帶頭節(jié)點(diǎn)的鏈表和沒有頭節(jié)點(diǎn)的鏈表

image

定義一個節(jié)點(diǎn):

package linkedQueue;

public class HeroNode {
    public int no;
    public String name;
    public String nickname;
    public HeroNode next;//指向下一個節(jié)點(diǎn)

    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }


}

定義一個鏈表:

實(shí)現(xiàn)以下功能:

添加節(jié)點(diǎn)

按序添加節(jié)點(diǎn)

刪除節(jié)點(diǎn)

修改節(jié)點(diǎn)

遍歷節(jié)點(diǎn)

反向打印節(jié)點(diǎn)

鏈表反轉(zhuǎn)

統(tǒng)計節(jié)點(diǎn)個數(shù)

打印倒數(shù)第n個節(jié)點(diǎn)

程序:

package linkedQueue;


import java.util.Stack;

public class SingleLinkedList {
    private HeroNode head = new HeroNode(0, "", "");

    public HeroNode getHead() {
        return head;
    }

    //反向打印節(jié)點(diǎn)
    public static void reversePrint(HeroNode head) {
        if (head.next == null) {
            return;
        }
    //    創(chuàng)建一個棧
        Stack<HeroNode> heroNodes = new Stack<>();
        HeroNode cur = head.next;
        while (cur != null) {
            heroNodes.push(cur); //入棧
            cur = cur.next;
        }
    //    出棧打印
        while (heroNodes.size() > 0) {
            System.out.println(heroNodes.pop());
        }
    }

    /**
     * 添加節(jié)點(diǎn)
     * @param heroNode
     */
    public void add(HeroNode heroNode) {
        HeroNode temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = heroNode;
    }

    /**
     * 有序添加節(jié)點(diǎn)
     * @param herNode
     */
    public void addByOrder(HeroNode herNode) {
        HeroNode temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no > herNode.no) {
                break;
            } else if (temp.next.no==herNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            System.out.println("你要插入的節(jié)點(diǎn)的編號已經(jīng)存在?。。。?);
        } else {
            herNode.next = temp.next;
            temp.next = herNode;
        }
    }

    /**
     * 更新節(jié)點(diǎn)數(shù)據(jù)
     * @param newHeroNode
     */
    public void update(HeroNode newHeroNode) {
        if (head.next == null) {
            System.out.println("鏈表為空?。。。?);
            return;
        }
        HeroNode temp = head.next;
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.no == newHeroNode.no) {
            //    找到
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.name = newHeroNode.name;
            temp.nickname = newHeroNode.nickname;
        } else {
        //    沒有找到
            System.out.println("沒有找到編號" + newHeroNode.no + "的節(jié)點(diǎn),不能修改");
        }

    }

    /**
     * 刪除節(jié)點(diǎn)
     * @param no
     */
    public void del(int no) {
        HeroNode temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no == no) {
                flag = true;
                break;
            }
            temp=temp.next;
        }
        if (flag) {
            temp.next = temp.next.next;
        }

    }

    /**
     * 遍歷節(jié)點(diǎn)
     */
    public void showList() {

        if (head.next == null) {
            System.out.println("鏈表為空");
            return;
        }

        HeroNode temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }

    }

    /**
     * 統(tǒng)計有效節(jié)點(diǎn)的個數(shù)
     */
    public static int getLength(HeroNode head) {
        if (head.next == null) { //空鏈表
            return 0;
        }
        int length = 0;
        HeroNode herNode = head.next;
        while (herNode != null) {
            length++; // 計數(shù)
            herNode=herNode.next; // 指針后移
        }
        return length;
    }

    /**
     * 求倒數(shù)第index個節(jié)點(diǎn)
     * @param head 頭節(jié)點(diǎn)
     * @param index 倒數(shù)第k個
     * @return
     */
    public static HeroNode findLastIndexNode(HeroNode head, int index) {
        // 判斷是否是空鏈表
        if (head.next == null) {
            return null;
        }

        // 拿到鏈表的長度
        int size = getLength(head);
        // index校驗(yàn),看是否在范圍內(nèi)
        if (index <= 0 || index > size) {
            return null;
        }
        // 定位倒數(shù)第index個節(jié)點(diǎn)
        HeroNode herNode = head.next;
        for (int i = 0; i < size - index; i++) {
            herNode = herNode.next;
        }
        return herNode;

    }

    //單鏈表反轉(zhuǎn)
    public static void reverseList(HeroNode head) {
    //    如果當(dāng)前鏈表為空或者只有一個節(jié)點(diǎn),直接返回
        if (head.next == null || head.next.next == null) {
            return;
        }

    //    定義輔助變量來遍歷鏈表
        HeroNode cur = head.next;
        HeroNode next = null;//指向當(dāng)前節(jié)點(diǎn)[cur]的下一個節(jié)點(diǎn)
        HeroNode reverseHead = new HeroNode(0, "", "");
    //    遍歷原來節(jié)點(diǎn),每遍歷一個節(jié)點(diǎn),將其取出,并放在新的鏈表的最前端
        while (cur != null) {
            next = cur.next;//暫時保存當(dāng)前節(jié)點(diǎn)的下一個節(jié)點(diǎn)
            cur.next = reverseHead.next;//將cur的下一個節(jié)點(diǎn)指向新的鏈表的最前端
            reverseHead.next=cur;//將cur鏈接到新的鏈表
            cur = next;
        }
        head.next = reverseHead.next;

    }

}

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

相關(guān)文章

  • 如何在Mac上安裝并配置JDK環(huán)境變量詳細(xì)步驟

    如何在Mac上安裝并配置JDK環(huán)境變量詳細(xì)步驟

    這篇文章主要介紹了如何在Mac上安裝并配置JDK環(huán)境變量詳細(xì)步驟,包括下載JDK、安裝JDK、配置環(huán)境變量、驗(yàn)證JDK配置以及可選地設(shè)置PowerShell為默認(rèn)終端,需要的朋友可以參考下
    2025-04-04
  • IDEA設(shè)置背景為自定義照片的操作方法

    IDEA設(shè)置背景為自定義照片的操作方法

    這篇文章主要介紹了IDEA設(shè)置背景為自定義照片,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-12-12
  • 解決RestTemplate 請求url中包含百分號 會被轉(zhuǎn)義成25的問題

    解決RestTemplate 請求url中包含百分號 會被轉(zhuǎn)義成25的問題

    這篇文章主要介紹了解決RestTemplate 請求url中包含百分號 會被轉(zhuǎn)義成25的問題,具有很好的參考價值,希望對大家有所幫助。
    2021-10-10
  • @ConfigurationProperties在IDEA中出現(xiàn)紅色波浪線問題解決方法

    @ConfigurationProperties在IDEA中出現(xiàn)紅色波浪線問題解決方法

    本文介紹了在Springboot項(xiàng)目中,當(dāng)@ConfigurationProperties注解出現(xiàn)紅色波浪線時的解決方法,文中有詳細(xì)的解決方案供大家參考,需要的朋友可以參考下
    2024-09-09
  • 簡述springboot及springboot cloud環(huán)境搭建

    簡述springboot及springboot cloud環(huán)境搭建

    這篇文章主要介紹了簡述springboot及springboot cloud環(huán)境搭建的方法,包括spring boot 基礎(chǔ)應(yīng)用環(huán)境搭建,需要的朋友可以參考下
    2017-07-07
  • Java唯一訂單編號生成代碼例子

    Java唯一訂單編號生成代碼例子

    在項(xiàng)目中,我們經(jīng)常遇到需要生成訂單編號、字典編號等唯一值場景,下面這篇文章主要給大家介紹了關(guān)于Java唯一訂單編號生成的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-07-07
  • Java中Collection、List、Set、Map之間的關(guān)系總結(jié)

    Java中Collection、List、Set、Map之間的關(guān)系總結(jié)

    今天小編就為大家分享一篇關(guān)于Java中Collection、List、Set、Map之間的關(guān)系總結(jié),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • SpringBoot整合Redis將對象寫入redis的實(shí)現(xiàn)

    SpringBoot整合Redis將對象寫入redis的實(shí)現(xiàn)

    本文主要介紹了SpringBoot整合Redis將對象寫入redis的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • Spring針對AOP詳細(xì)講解

    Spring針對AOP詳細(xì)講解

    Spring是一個廣泛應(yīng)用的框架,SpringAOP則是Spring提供的一個標(biāo)準(zhǔn)易用的aop框架,依托Spring的IOC容器,提供了極強(qiáng)的AOP擴(kuò)展增強(qiáng)能力,對項(xiàng)目開發(fā)提供了極大地便利
    2022-06-06
  • 淺析SpringBoot2底層注解@Conditional@ImportResource

    淺析SpringBoot2底層注解@Conditional@ImportResource

    這篇文章主要為大家介紹了SpringBoot2底層注解@Conditional@ImportResource的分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05

最新評論

吴川市| 静乐县| 旬邑县| 成安县| 视频| 梁山县| 犍为县| 南投市| 宁明县| 富蕴县| 奉贤区| 泰安市| 安化县| 大冶市| 玛曲县| 澜沧| 肥乡县| 邯郸县| 曲麻莱县| 凤山市| 墨江| 白水县| 汉中市| 崇信县| 岢岚县| 巴里| 育儿| 英吉沙县| 定日县| 清涧县| 元阳县| 金阳县| 神池县| 潜江市| 普兰店市| 崇明县| 江达县| 湘乡市| 建昌县| 北碚区| 休宁县|