数据结构与算法之链表

1.  链表的概念及结构

       链表是一种 物理存储结构上非连续 存储结构,数据元素的 逻辑顺序 是通过链表中的 引用链接 次序实现的 。
实际中链表的结构非常多样,以下情况组合起来就有 8 种链表结构:
1. 单向或者双向
2. 带头或者不带头
3. 循环或者非循环
那么组合起来就有8种结构, 虽然有这么多的链表的结构,但是我们重点掌握两种: 无头单向非循环链表 ,无头双向链表

2. 链表的实现

// 1、无头单向非循环链表实现
public class SingleLinkedList {
//头插法
public void addFirst(int data);
//尾插法
public void addLast(int data);
//任意位置插入,第一个数据节点为0号下标
public boolean addIndex(int index,int data);
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key);
//删除第一次出现关键字为key的节点
public void remove(int key);
//删除所有值为key的节点
public void removeAllKey(int key);
//得到单链表的长度
public int size();
//打印整个链表
public void display();
//清空整个链表
public void clear();
}

1. 创建一个链表

static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }

public void createList(){
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(34);
        ListNode listNode4 = new ListNode(45);
        ListNode listNode5 = new ListNode(56);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;

    }

 2. 打印链表的所有元素:

    public void display(){
        if (this.head == null) {
            return ;
        }
        ListNode ret = this.head;
        while (ret != null){
            System.out.println(ret.val+" ");
            ret = ret.next;
        }
    }

 

3. 输出链表的长度

public int size(){
        int usedsize = 0;
        ListNode ret = this.head;
        while (ret != null){
            usedsize ++;
            ret = ret.next;
        }
        return usedsize;
    }

 

 4. 查找是否包含关键字key在单链表中

 public boolean contains(int key){
        ListNode ret = this.head;
        while (ret != null){
            if (ret.val == key) {
                return true;
            }
            ret = ret.next;
        }
        return false;
    }

 5. 头插法插数据元素

 public void addFirst(int data){
        ListNode node = new ListNode(data);//给一个节点
        if (this.head == null) {//空链表
            this.head = node;
        }else {
            node.next = this.head;
            head = node;
        }
    }

 6.尾插法插入数据元素

 public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode ret = this.head;
        if (this.head == null){
            this.head = node;
        }
        while (ret.next != null){
            ret = ret.next;
        }
        ret .next = node;
    }

 7. 任意位置插入节点

    //任意位置插入
    public void addIndex(int index,int data) {
        if (index < 0 || index > size()) {
            System.out.println("index 不合法!");
            throw new IndexWrongFullExcept("index 位置不合法!");
        }
        if (index == 0) {//相当于头插法
            addFirst(data);
            return;
        }
        if (index == size()) {//相当于尾插法
            addLast(data);
            return;
        }
        //插入节点
        ListNode ret = findIndexSubOne(index);
        ListNode node = new ListNode(data);
        node.next = ret.next;
        ret.next = node;
    }
    //先走index-1步,找到ret
    private ListNode findIndexSubOne(int index){
        ListNode ret = this.head;
        while (index - 1 != 0){
            ret = ret.next;
            index --;
        }
        return ret;
    }

 8. 删除第一次出现的key的节点

     public void remove(int key) {
        if (this.head == null) {
            return;
        }
        if (this.head.val == key) {
            this.head = head.next;
            return;
        }
        ListNode ret = findPrevOfKey(key);
        ListNode del = ret.next;
        if (ret == null) {
            System.out.println("没有找到要删除的数字!");
            return;
        } else {
            ret.next = del.next;
        }
    }
    //找到要删除的节点的前一个节点
    private ListNode findPrevOfKey(int key){
        ListNode ret = this.head;
        while (ret.next != null){
            if (ret.next.val == key) {
                return ret;
            }
            ret = ret.next;
        }
        return null;
    }

9. 删除所有值为key的节点

 //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode ret = this.head.next;
        ListNode prev = this.head;
        while (ret != null){
            if (ret.val == key){
                prev.next = ret.next;
                ret = ret.next;
            }
            else {
                prev = ret;
                ret = ret.next;
            }
        }
        if (this.head.val == key){
            this.head = head.next;
        }
    }

 

10. 清空整个链表

这个很简单 我们让我们的头节点直接变成空的,是不是就清空了

  //清空整个链表
    public void clear(){
        this.head = null;

    }

 3. 自己实现一个链表的全部代码

public class MySingleList {
    //内部类表示一个节点
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;//头节点

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);//给一个节点
        if (this.head == null) {//空链表
            this.head = node;
        } else {
            node.next = this.head;
            head = node;
        }
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode ret = this.head;
        if (this.head == null) {
            this.head = node;
        }
        while (ret.next != null) {
            ret = ret.next;
        }
        ret.next = node;
    }

    //任意位置插入
    public void addIndex(int index, int data) {
        if (index < 0 || index > size()) {
            System.out.println("index 不合法!");
            throw new IndexWrongFullExcept("index 位置不合法!");
        }
        if (index == 0) {//相当于头插法
            addFirst(data);
            return;
        }
        if (index == size()) {//相当于尾插法
            addLast(data);
            return;
        }
        //插入节点
        ListNode ret = findIndexSubOne(index);
        ListNode node = new ListNode(data);
        node.next = ret.next;
        ret.next = node;
    }

    //先走index-1步,找到ret
    private ListNode findIndexSubOne(int index) {
        ListNode ret = this.head;
        while (index - 1 != 0) {
            ret = ret.next;
            index--;
        }
        return ret;
    }


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

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if (this.head == null) {
            return;
        }
        if (this.head.val == key) {
            this.head = head.next;
            return;
        }
        ListNode ret = findPrevOfKey(key);
        ListNode del = ret.next;
        if (ret == null) {
            System.out.println("没有找到要删除的数字!");
            return;
        } else {
            ret.next = del.next;
        }
    }
    //找到要删除的节点的前一个节点
    private ListNode findPrevOfKey(int key){
        ListNode ret = this.head;
        while (ret.next != null){
            if (ret.next.val == key) {
                return ret;
            }
            ret = ret.next;
        }
        return null;
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode ret = this.head.next;
        ListNode prev = this.head;
        while (ret != null){
            if (ret.val == key){
                prev.next = ret.next;
                ret = ret.next;
            }
            else {
                prev = ret;
                ret = ret.next;
            }
        }
        if (this.head.val == key){
            this.head = head.next;
        }
    }

    //得到单链表的长度
    public int size(){
        int usedsize = 0;
        ListNode ret = this.head;
        while (ret != null){
            usedsize ++;
            ret = ret.next;
        }
        return usedsize;
    }
    //打印列表当中的元素
    public void display(){
        if (this.head == null) {
            return ;
        }
        ListNode ret = this.head;
        while (ret != null){
            System.out.print(ret.val+" ");
            ret = ret.next;
        }
    }
    //清空整个链表
    public void clear(){
        this.head = null;

    }
    //创建链表
    public void createList(){
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(12);
        ListNode listNode3 = new ListNode(34);
        ListNode listNode4 = new ListNode(12);
        ListNode listNode5 = new ListNode(12);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;

    }
}

那么本次链表的分享就到此结束了,下次再见

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java小白~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值