通过数组理解链表

本文通过数组模拟链表结构,详细解释了链表的特点和逻辑结构,包括带头节点的单链表的创建、增删改查操作。介绍了如何按编号顺序和非顺序添加节点,并提供了删除、更新节点的方法。此外,还探讨了查找单链表倒数第k个节点的算法。
摘要由CSDN通过智能技术生成

数组模拟链表

此文章是通过数组来实现链表,主要帮助理解链表这一结构,若文中有错误之处,望指出,谢谢。

一、链表(Linked List)

​ 有序1的列表,内存存储如下:

头指针地址Data域Next域
150110a2170
120a4160
130
140a6null
150a1110
160a5140
170a3120

如图总结:

(1)链表是以节点的方式来存储,是链式存储。

(2)每个节点包含data域,next域:指向下一个节点。

(3)链表各个节点不一定是连续存放的。

(4)链表分带头节点的链表和没有头节点的链表,具体根据实际需求来定。

单链表(带头节点)逻辑结构图:

在这里插入图片描述
(图片是作者自己画的,很渣😂,请不要介意)

二、首先是带头节点的单链表实现(此案例为水浒英雄排名):

​ (1)单链表的创建,以及增删改查。

​ (2)方法1:添加英雄,直接添加到链表尾部(有局限性)

​ (3)方法2:根据排名添加到指定位置(如果有这个排名则添加失败并给出提示。)

注意事项:

head节点:1.不存放具体数据

​ 2.作用是表示单链表的表头

创建:1.先创建一个head头节点

​ 2.每添加一个节点,直接加入到链表最后。

遍历:通过一个辅助变量,遍历链表。

方法1(不考虑编号顺序):

创建节点对象:
1.每一个节点对象中应包含:
(1)英雄编号
(2)英雄名字
(3)英雄别名
(4)指向下一节点的指针(next)

//定义一个HeroNode,每个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 + '\'' +
                ", next=" + next +
                '}';
    }
}

创建单链表并创建添加、显示方法:

//创建SingleLinkedList(单链表)
class SingleLinkedList{
    //先创建一个头节点,头节点不能动,不存放具体数据
    private HeroNode head=new HeroNode(0,"","");
    //添加节点到单向链表(添加方法)
    //当不考虑编号顺序时
    //1.找到当前链表最后节点(最后一个节点的next为null)
    //2.将最后这个节点的next指向新的节点
    public void add(HeroNode heroNode){
        //因为head节点不能动,所以我们需要一个辅助变量temp
        HeroNode temp =head;//初始值应为head
        //遍历链表,找到最后
        while (true){
            //找到最后
            if (temp.next==null){
                break;
            }
            //没有找到最后将temp后移
            temp=temp.next;
        }
        //当退出while循环时,temp到了链表最后
        //将最后这个节点的next指向新的节点
        temp.next=heroNode;
    }
    //显示链表
    public void list(){
        //判断链表是否为空
        if (head.next==null){
            System.out.println("链表为空");
            return;
        }
        //因为头节点不能动,因此我们需要一个辅助变量来遍历
        HeroNode temp =head.next;
        while (true){
            //判断是否到链表最后
            if (temp==null){
                break;
            }
            //输出节点信息
            System.out.println(temp);
            //将temp后移
            temp=temp.next;
        }
    }


}

方法1:源码:

package linkedlist;

public class SingleLinkedListDemo {
    public static void main(String[] args) {
        //创建节点
        HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode heroNode2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode heroNode3 = new HeroNode(3,"吴用","智多星");
        HeroNode heroNode4 = new HeroNode(4, "林冲", "豹子头");
        //创建链表
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        //添加节点
        singleLinkedList.add(heroNode1);
        singleLinkedList.add(heroNode2);
        singleLinkedList.add(heroNode3);
        singleLinkedList.add(heroNode4);
        //显示
        singleLinkedList.list();

    }
}

//创建SingleLinkedList(单链表)
class SingleLinkedList{
    //先创建一个头节点,头节点不能动,不存放具体数据
    private HeroNode head=new HeroNode(0,"","");
    //添加节点到单向链表(添加方法)
    //当不考虑编号顺序时
    //1.找到当前链表最后节点(最后一个节点的next为null)
    //2.将最后这个节点的next指向新的节点
    public void add(HeroNode heroNode){
        //因为head节点不能动,所以我们需要一个辅助变量temp
        HeroNode temp =head;//初始值应为head
        //遍历链表,找到最后
        while (true){
            //找到最后
            if (temp.next==null){
                break;
            }
            //没有找到最后将temp后移
            temp=temp.next;
        }
        //当退出while循环时,temp到了链表最后
        //将最后这个节点的next指向新的节点
        temp.next=heroNode;
    }
    //显示链表
    public void list(){
        //判断链表是否为空
        if (head.next==null){
            System.out.println("链表为空");
            return;
        }
        //因为头节点不能动,因此我们需要一个辅助变量来遍历
        HeroNode temp =head.next;
        while (true){
            //判断是否到链表最后
            if (temp==null){
                break;
            }
            //输出节点信息
            System.out.println(temp);
            //将temp后移
            temp=temp.next;
        }
    }


}







//定义一个HeroNode,每个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 + '\'' +
                ", next=" + next +
                '}';
    }
}

在这里插入图片描述

局限性:必须按照预设顺序进行添加,如果在测试中添加顺序改变,那么无法按照编号顺序创建链表。
在这里插入图片描述

在这里插入图片描述

方法2(按照编号顺序添加)示意图:在这里插入图片描述

思路:需要按照编号顺序添加

1.首先找到新添加的节点位置,是通过辅助变量(指针),通过遍历来解决。

2.新的节点next=temp.next

3.将temp.next=新的节点

public void addByOrder(HeroNode heroNode){
    //因为head节点不能动,因此我们通过辅助变量来找到添加的位置
    //因为单链表,因为我们找的temp是位于添加位置的前一个节点,否则插入不了
    HeroNode temp =head;
    boolean flag=false; //标志添加的编号是否存在
    while (true){
        if (temp.next==null){//说明temp在链表的最后
            break;
        }
        if (temp.next.no>heroNode.no){//位置找到
            break;
        }else if(temp.next.no==heroNode.no){//说明编号存在
            flag=true;//标号存在
            break;
        }
        temp=temp.next;//后移,遍历当前链表
    }
    if (flag){//编号存在,添加失败
        System.out.printf("添加的编号%d已经存在\n",heroNode.no);
    }else {
        //插入到链表中,temp的后面
        heroNode.next=temp.next;
        temp.next=heroNode;
    }
}

运行结果:在这里插入图片描述

根据编号修改节点:

//修改节点信息,根据no编号修改
//1.根据newHeroNode的no来修改
public void update(HeroNode newHeroNode){
    if (head.next==null){
        System.out.println("链表为空");
        return;
    }
    //找到需要修改的节点,根据no编号
    //先定义辅助变量
    HeroNode temp =head.next;
    boolean flag =false;//表示是否找到该节点
    while(true){
        if (temp==null){
            break;//已经遍历完毕
        }
        if (temp.no==newHeroNode.no){
            //找到
            flag=true;
            break;
        }
        temp=temp.next;
    }
    //根据flag判断是否要找到修改的节点
    if (flag){
        temp.name=newHeroNode.name;
        temp.nickname=newHeroNode.nickname;
    }else {//没有找到
        System.out.printf("没有找到编号%d的节点\n",newHeroNode.no);
    }
}

删除操作:

​ 思路:1.我们先找到需要删除的这个节点的前一个节点temp

​ 2.temp.next=temp.next.next

​ 3.被删除的节点,并不会有其他引用指向,会被垃圾回收机制回收

//删除节点
//思路
//1.head不能动,因此我们需要一个temp辅助节点找到待删除节点的前一个节点
//2.说明我们在比较时,是temp.next.no和需要删除的节点的no比较
public void del(int no){
    HeroNode temp=head;
    boolean flag =false;//标志是否找到待删除节点
    while (true){
        if (temp.next==null){
            break;
        }if (temp.next.no==no){
            //找到嗲删除节点的前一个节点temp
            flag=true;
            break;
        }
        temp=temp.next;//temp后移,遍历
    }
    if (flag){
        //找到可以删除
        temp.next=temp.next.next;
    }else {
        System.out.printf("要删除的%d 节点不存在\n",no);
    }
}

查找单链表的倒数第k个节点(新浪面试题)

先创建得到有效节点的方法:

public static int getLength(HeroNode head){
    if (head.next==null){//空链表
        return 0;
    }
    int length=0;
    //定义一个辅助变量
    HeroNode cur=head.next;
    while (cur!=null){
        length++;
        cur=cur.next;//遍历
    }
    return length;
}

查找倒数第k个节点的方法:

public static HeroNode findLastIndexNode(HeroNode head,int index){
        //如果链表为空,返回null
        if (head.next==null){
            return null;//没有找到
        }
        //得到链表的长度(节点个数)
        int size=getLength(head);
        //遍历size-index位置,这是我们倒数第k个节点
        if (index<=0||index>size){
            return null;
        }
        //定义辅助变量,for循环定位到倒数的index
        HeroNode cur =head.next;
        for (int i=0;i<size-index;i++){
            cur=cur.next;
        }
        return cur;
}
next==null){
            return null;//没有找到
        }
        //得到链表的长度(节点个数)
        int size=getLength(head);
        //遍历size-index位置,这是我们倒数第k个节点
        if (index<=0||index>size){
            return null;
        }
        //定义辅助变量,for循环定位到倒数的index
        HeroNode cur =head.next;
        for (int i=0;i<size-index;i++){
            cur=cur.next;
        }
        return cur;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值