链表-备忘录

跳转到总目录


链表

链表是有序的列表,但是它在内存中是存储如下:
在这里插入图片描述
由上图小结:

  • 链表是以节点的方式来存储,是链式存储.
  • 每个节点包含 data 域, next 域:指向下一个节点.
  • 如图:发现链表的各个节点不一定是连续存储.
  • 链表分带头节点的链表和没有头节点的链表,根据实际的需求来确定.

单向链表

在这里插入图片描述
举例示范: 使用带head头的单向链表实现 –水浒英雄排行榜管理.
完成对英雄人物的增删改查操作.

示例图如下:
第一种方法在添加英雄时,直接添加到链表的尾部
在这里插入图片描述
第二种方式在添加英雄时,根据排名将英雄插入到指定位置(如果有这个排名,则添加失败,并给出提示)
在这里插入图片描述
链表删除
在这里插入图片描述
代码示例如下:

//写一个单链表类
class SingleLinkedList {
    //先初始化一个头结点 不存放数据
    private HeroNode head = new HeroNode(0, "", "");

    public HeroNode getHead() {
        return head;
    }


    //添加节点到单向链表 不考虑编号顺序
    public void add(HeroNode heroNode) {
        //因为头结点不能动 需要辅助节点temp遍历
        HeroNode temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = heroNode;
    }

    //通过编号顺序添加节点到单向链表
    public void addByOrder(HeroNode heroNode) {
        HeroNode temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no > heroNode.no) {
                break;
            }
            if (temp.next.no == heroNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            System.out.printf("编号%d重复\n", heroNode.no);
        } else {
            //重点 链表中间加入节点的写法
            heroNode.next = temp.next;
            temp.next = heroNode;
        }
    }

    //修改节点信息,根据no来修改,即no编号不能改
    public void update(HeroNode heroNode) {
        if (head.next == null) {
            System.out.println("空的");
            return;
        }
        HeroNode temp = head.next;
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.no == heroNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.name = heroNode.name;
            temp.nickName = heroNode.nickName;
        } else {
            System.out.println("没有该编号的节点 无法修改");
        }
    }

    //删除节点
    public void delete(int no) {
        if (head.next == null) {
            System.out.println("空的");
            return;
        }
        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;
        } else {
            System.out.println("没有找到该编号 无法删除");
        }
    }

    //遍历显示
    public void show() {
        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;
        }
    }
}

//每一个HeroNode对象就是一个节点
class HeroNode {
    public int      no;
    public String   name;
    public String   nickName;
    public HeroNode next;

    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 + '\'' +
                '}';
    }
}

双向链表

管理单向链表的缺点分析:

单向链表,查找的方向只能是一个方向,而双向链表可以向前或者向后查找。
单向链表不能自我删除,需要靠辅助节点,
而双向链表,则可以自我删除,
所以前面我们单链表删除时节点,总是找到temp,temp是待删除节点的前一个节点.

示意图帮助理解删除
在这里插入图片描述
分析 双向链表的遍历,添加,修改,删除的操作思路===》代码实现

  1. 遍历 方和 单链表一样,只是可以向前,也可以向后查找
  2. 添加 (默认添加到双向链表的最后)
    (1) 先找到双向链表的最后这个节点
    (2) temp.next = newHeroNode
    (3) newHeroNode.pre = temp;
  3. 修改 思路和 原来的单向链表一样.
  4. 删除
    (1) 因为是双向链表,因此,我们可以实现自我删除某个节点
    (2) 直接找到要删除的这个节点,比如temp
    (3) temp.pre.next = temp.next
    (4) temp.next.pre = temp.pre;

示例代码如下:

//双向链表
public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        //双向链表测试
        HeroNode2 hero1 = new HeroNode2(1, "宋江", "及时雨");
        HeroNode2 hero2 = new HeroNode2(2, "卢俊义", "玉麒麟");
        HeroNode2 hero3 = new HeroNode2(3, "吴用", "智多星");
        HeroNode2 hero4 = new HeroNode2(4, "林冲", "豹子头");

        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
//        doubleLinkedList.add(hero1);
//        doubleLinkedList.add(hero2);
//        doubleLinkedList.add(hero3);
//        doubleLinkedList.add(hero4);

        doubleLinkedList.addByOrder(hero4);
        doubleLinkedList.addByOrder(hero3);
        doubleLinkedList.addByOrder(hero2);
        doubleLinkedList.addByOrder(hero1);


        doubleLinkedList.show();
        System.out.println("====================");

        //修改测试
//        HeroNode2 heroNode2 = new HeroNode2(4, "1", "11");
//        doubleLinkedList.update(heroNode2);
//        doubleLinkedList.show();
//        System.out.println("====================");

        //删除测试
//        doubleLinkedList.delete(4);
//        doubleLinkedList.show();
    }


}

class DoubleLinkedList {
    //先初始化一个头结点 不存放数据
    private HeroNode2 head = new HeroNode2(0, "", "");

    public HeroNode2 getHead() {
        return head;
    }

    //添加节点到双向链表 不考虑编号顺序
    public void add(HeroNode2 heroNode) {
        //因为头结点不能动 需要辅助节点temp遍历
        HeroNode2 temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = heroNode;
        heroNode.pre = temp;
    }

    //通过编号顺序添加节点到双向链表
    public void addByOrder(HeroNode2 heroNode) {
        HeroNode2 temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no > heroNode.no) {
                break;
            }
            if (temp.next.no == heroNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            System.out.printf("编号%d重复\n", heroNode.no);
        } else {
            if (temp.next == null) {
                temp.next = heroNode;
                heroNode.pre = temp;
            } else {
                heroNode.next = temp.next;
                temp.next.pre = heroNode;
                temp.next = heroNode;
                heroNode.pre = temp;
            }
        }
    }

    //修改双向链表节点信息,根据no来修改,即no编号不能改 与单项链表基本一样
    public void update(HeroNode2 heroNode) {
        if (head.next == null) {
            System.out.println("空的");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.no == heroNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.name = heroNode.name;
            temp.nickName = heroNode.nickName;
        } else {
            System.out.println("没有该编号的节点 无法修改");
        }
    }

    //双向链表删除节点
    public void delete(int no) {
        if (head.next == null) {
            System.out.println("空的");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true) {
            if (temp == null) { // 已经到链表最后
                break;
            }
            if (temp.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.pre.next = temp.next;
            //防止删除的节点是最后的节点
            if (temp.next != null) {
                temp.next.pre = temp.pre;
            }
        } else {
            System.out.println("没有找到该编号 无法删除");
        }
    }

    // //遍历显示双向链表
    public void show() {
        if (head.next == null) {
            System.out.println("空的");
            return;
        }
        HeroNode2 temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }
}

//每一个HeroNode2对象就是一个节点
class HeroNode2 {
    public int       no;
    public String    name;
    public String    nickName;
    public HeroNode2 next; //下一个节点
    public HeroNode2 pre; //上一个节点

    public HeroNode2(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 + '\'' +
                '}';
    }
}

环形链表(约瑟夫环)

在这里插入图片描述
提示:用一个不带头结点的循环链表来处理Josephu 问题:先构成一个有n个结点的单循环链表,然后由k结点起从1开始计数,计到m时,对应结点从链表中删除,然后再从被删除结点的下一个结点又从1开始计数,直到最后一个结点从链表中删除算法结束。

思路图解:
在这里插入图片描述
在这里插入图片描述
代码示例:

//约瑟夫问题
public class Josephu {
    public static void main(String[] args) {
        //测试环形链表
        CirCleSingleLinkedList cirCleSingleLinkedList = new CirCleSingleLinkedList();
        cirCleSingleLinkedList.addBoy(5);
        cirCleSingleLinkedList.showBoy();

        //测试小孩出圈
        cirCleSingleLinkedList.conutBoy(1,2,5);
    }
}

class CirCleSingleLinkedList {
    //创建头节点,当前没有编号
    private Boy first= null;
    //添加子节点,构建环形链表
    public void addBoy(int nums){
        //nums 数据校验
        if (nums<1){
            System.out.println("编号的值不正确");
            return;
        }
        Boy curBoy =null;
        for (int i = 1; i <= nums; i++) {
            Boy boy = new Boy(i);
            if (i==1){
                first=boy;
                first.setNext(boy);
                curBoy=boy;
            }else {
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy=boy;
            }
        }
    }
    //遍历
    public void showBoy(){
        if (first==null){
            System.out.println("空的");
            return;
        }
        Boy temp=first;
        while (true){
            System.out.println("编号"+temp.getNo());
            if (temp.getNext()==first){
                break;
            }
            temp=temp.getNext();
        }
    }


    /**
     * 根据用户输入,计算出小孩出圈顺序
     * @param startNo 表示从第几个小孩开始数
     * @param countNum 表示数几下
     * @param nums 表示最初有多少小孩在圈中
     */
    public void conutBoy(int startNo,int countNum,int nums){
        if (first==null||startNo<1||startNo>nums){
            System.out.println("空的");
            return;
        }
        //辅助指针,帮助出圈 指向环形链表的最后一个节点
        Boy helper=first;
        while (true){
            if (helper.getNext()==first){
                break;
            }
            helper=helper.getNext();
        }
        //小孩报数前,先让first和helper移动startNo-1次
        for (int i = 0; i < startNo - 1; i++) {
            first=first.getNext();
            helper=helper.getNext();
        }
        //开始报数,让first和helper移动countNum-1次,然后出圈
        while (true){
            if (helper==first){
                break;
            }
            for (int i = 0; i < countNum - 1; i++) {
                first=first.getNext();
                helper=helper.getNext();
            }
            System.out.println("出圈的小孩编号:"+first.getNo());
            first=first.getNext();
            helper.setNext(first);
        }
        System.out.println("最后留在圈中的小孩编号"+first.getNo());
    }
}

//表示一个节点
class Boy {
    private int no;
    private Boy next;

    public Boy(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public Boy getNext() {
        return next;
    }

    public void setNext(Boy next) {
        this.next = next;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值