1. 程式人生 > 其它 >3-單向連結串列

3-單向連結串列

單向連結串列


基本介紹

應用

package com.company.linkedlist;

/**
 * @Function :
 * date 2021/5/8 - 16:35
 * How :
 */
public class LinkedList {
    public static void main(String[] args) {
        HeroNode hero1 = new HeroNode(1, "李逵", "黑旋風");
        HeroNode hero2 = new HeroNode(2, "宋江", "及時雨");
        HeroNode hero3 = new HeroNode(2, "宋江", "及時雨");
        HeroNode hero4 = new HeroNode(3, "林沖", "豹子頭");
        HeroNode hero5 = new HeroNode(4, "吳用", "智多星");
        HeroNode hero6 = new HeroNode(4, "吳用(update)", "智多星(update)");
        SimplelinkedList simplelinkedList = new SimplelinkedList();
//        simplelinkedList.addNode(hero1);
//        simplelinkedList.addNode(hero2);
//        simplelinkedList.addNode(hero3);
        simplelinkedList.addNodeByOrder(hero1);
        simplelinkedList.addNodeByOrder(hero3);
        simplelinkedList.addNodeByOrder(hero4);
        simplelinkedList.addNodeByOrder(hero2);
        simplelinkedList.addNodeByOrder(hero5);
        simplelinkedList.showList();
        System.out.println("---------------------");
        simplelinkedList.update(hero6);
        simplelinkedList.showList();
        System.out.println("---------------------");
        simplelinkedList.delNode(3);
        simplelinkedList.showList();

        simplelinkedList.delNode(3);
        simplelinkedList.showList();

    }
}

//連結串列
class SimplelinkedList{
    //頭節點 不定義具體資料
    private HeroNode head = new HeroNode(0,"","");

    //新增節點到單項鍊表尾
    public void addNode(HeroNode node){
        //引入輔助變數
        HeroNode temp = this.head;
        while (true){
            //查詢連結串列尾
            if (temp.nextNode == null){
                break;
            }
            temp = temp.nextNode;
        }
        temp.nextNode = node;
    }
    //修改節點
    //根據no修改
    public void update(HeroNode node){
        HeroNode temp = this.head;
        boolean flag = false;
        while (true){
            //連結串列尾
            if (this.head.nextNode == null){
                break;
            }
            if (temp.id == node.id){
                flag = true;
                break;
            }
            temp = temp.nextNode;
        }
        if (flag){
            temp.name = node.name;
            temp.nickName = node.nickName;
        }else {
            System.out.println("沒有找到該編號");
        }

    }
    //刪除節點
    public void delNode(int n){
        HeroNode temp = this.head;
        boolean flag = false;
        while (true){
            if (temp.nextNode == null){
                break;
            }
            if (temp.nextNode.id == n){
                flag = true;
                break;
            }
            temp = temp.nextNode;
        }
        if (flag){
            temp.nextNode = temp.nextNode.nextNode;
        }else {
            System.out.println("要刪除的節點不存在");
            return ;
        }
    }
    //按順序新增節點
    public void addNodeByOrder(HeroNode node){
        //查詢的temp位於新增位置的前一個
        HeroNode temp = this.head;
        //新增的編號是否存在
        boolean flag = false;
        while (true){
            //連結串列尾
            if (temp.nextNode == null){
                break;
            }
            if(temp.nextNode.id>node.id){
                break;
            }else if (temp.nextNode.id == node.id){
                flag = true;
                break;
            }
            temp = temp.nextNode;
        }
        if (flag){
            System.out.println("待插入的序號已經存在 無法插入");
            return;
        }else {
            node.nextNode = temp.nextNode;
            temp.nextNode = node;
        }
    }
    //顯示列表
    public void showList(){

        if (head.nextNode == null){
            System.out.println("連結串列為空");
            return;
        }

        HeroNode temp = this.head.nextNode;

        while (true){
            System.out.println(temp);
            //查詢連結串列尾
            if (temp.nextNode == null){
                break;
            }
            temp = temp.nextNode;
        }
    }
}

//定義heroNode  物件是一個節點
class HeroNode{
    public int id;
    public String name;
    public String nickName;
    public HeroNode nextNode;

    public HeroNode(int id, String name, String nickName) {
        this.id = id;
        this.name = name;
        this.nickName = nickName;
    }
    //顯示

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

雙向連結串列

基本介紹

新增一個指向前一個節點的指標

約瑟夫環

介紹

實現

package com.company.linkedlist;

import java.util.function.BiConsumer;

/**
 * @Function :
 * date 2021/5/9 - 13:10
 * How :
 */
public class Josepfu {
    public static void main(String[] args) {
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        circleSingleLinkedList.addChildNode(10);
        circleSingleLinkedList.showCircleList();
    }
}

//建立一個環形單向連結串列
class CircleSingleLinkedList{
    //建立第一個節點
    private Boy firstNode = new Boy(-1);

    public void addChildNode(int num){
        if (num<1){
            System.out.println("輸入的值不正確");
            return;
        }
        Boy currBoy = null;
        for (int i = 1; i <= num; i++) {
            Boy boy = new Boy(i);
            if (i==1){
                firstNode = boy;
                firstNode.setNextNode(firstNode);
                currBoy = firstNode;
            }else {
                currBoy.setNextNode(boy);
                boy.setNextNode(firstNode);
                currBoy = boy;
            }
        }
    }

    public void showCircleList(){
        Boy currBoy = null;
        currBoy = this.firstNode;
        while (true){
            System.out.printf("id = : %d \n", currBoy.getId());
            if (currBoy.getNextNode()==this.firstNode){
                break;
            }
            currBoy = currBoy.getNextNode();
        }

    }

}

//建立一個類 表示節點
class Boy{
    private int id;
    private Boy nextNode;
    public Boy (int id){
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Boy getNextNode() {
        return nextNode;
    }

    public void setNextNode(Boy nextNode) {
        this.nextNode = nextNode;
    }
}

出圈