轻松学会单链表的创建

学习目标:轻松学会单链表的创建

学习时间:2020.10.20 星期二 12点-8点

学习内容:本次构造单向、不带头、非循环的单链表

顺序表:物理上是连续的,逻辑上也是连续的。
单链表:物理上不一定是连续的,单逻辑上是连续的。
在这里插入图片描述

接下来先展示一下一天的成果,初次接触,如有不足,轻踩

class Node {
    public int data;
    public Node next;//存储对象引用

    public Node(int data) {
        this.data = data;
        //这里没有初始化next的引用是,不知道next当前指向那个节点
        }
}

public class MySingleList {
    public Node head;//作用是,定位头节点的引用
    //头插法
    public void addFirst(int data){
        Node node = new Node(data);
            node.next = this.head;
            this.head = node;
    }
    //尾插法
    public void addLast(int data){
        Node node = new Node(data);
        if (this.head == null) {
            this.head = node;
            return;
        }
        Node cur = this.head;
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }

    //找到要插入位置的前一个位置的下标
    public Node searchPrev(int index) {
        int count = 0;
        Node cur = this.head;
        while (count < index-1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) {
        Node node = new Node(data);
        if (index < 0 || index > size()) {
            System.out.println("index不合法");
            return;

        }
        if (index == 0) {
            addFirst(data);
            return;
        }

        Node cur = searchPrev(index);
        node.next = cur.next;
        cur.next = node;
    }



    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        Node cur = this.head;
        while (cur != null){
            if (cur.data == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //找到关键字key的前驱
    public Node searchPrevNode(int key){
        Node cur = this.head;
        while (cur.next != null){
            if (cur.next.data == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if (this.head.data == key){
            this.head = head.next;
            return;
        }
        Node pre = searchPrevNode(key);
        if (pre == null){
            return;
        }
        Node del = pre.next;
        pre.next = del.next;

    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {
        for (int count = this.size(); count >= 0 ; count++) {
            remove(key);
        }
    }


    //得到单链表的长度
    public int size() {
        Node cur = this.head;
        int count = 0;
        while (cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }
    //打印单链表
    public void display() {
        Node cur = this.head;
        while (cur != null){
            System.out.print(cur.data + "  ");
            cur = cur.next;
        }
        System.out.println();
    }
    //清空单链表
    public void clear() {
        this.head = null;
    }
}

不懂得来看下面的个人学习思路
1、首先来说一下头插法
头插法就是在链表的第一个位置插入一个对象(node),我让node的next直接等于这个列表的head,再定义这个列表的head等于node就可以了。看图理解:

在这里插入图片描述
2、尾插法
首先要判断一下这个列表是否为空,如果是空列表,直接插入就行了,
如果不为空,定义cur这个对象指向这个列表的head,然后用while循环让他走到这个列表的最后位置,让cur.next = node就完成了
3、在任意位置插入:
为了便于理解,我们先像顺序表一样自己定义一下数据节点的下标,
我们首先要判断插入位置是否合法,
在0号位置插入的直接调用头插法,
然后在其他位置插入,首先要找到要插入位置的前一个位置的下标,这时创建一个函数(searchPrev)取得这个下标,然后让看图,先让要插入的node的next = 要插入位置的前驱,此时前面的链条和后面的链条就断开了,再让前面的链条把插入后的后面的链条连起来。
4、得到单链表的长度
定义一个新对象cur等于这个链表的首号位置head,然后用while循环开始计数,当cur走到末尾位置,循环停止,返回count值就是链表长度。
5、查找是否包含关键字
和取链表长度类似,只不过在里面加个if判断语句就行了。
后面的因为今天学习太累就暂时不介绍了,觉得有帮助不介意点个赞!!!有人评论发下面的个人理解吗??

学习产出:

提示:这里统计学习计划的总量
例如:
1、 技术笔记 2 遍
2、CSDN 技术博客 3 篇
3、 学习的 vlog 视频 1 个

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值