数据结构——链表(java)

链表

1. 基本介绍

1.1 定义

链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的。

如图所示:
在这里插入图片描述

1.2 链表分类

单向、双向;带头、不带头;循环、非循环
重点:单向不带头非循环、双向不带头非循环(集合类底层)

如图:单项带头非循环链表结构
在这里插入图片描述
如图:单向带头循环链表结构
在这里插入图片描述
如图:双向不带头循环链表结构
在这里插入图片描述

3.不带头非循环单链表CURD

代码展示:

package demo3;

/**
 * @author zq
 * 不带头非循环单链表相关操作
 */
public class MySingleList {

    class Node {
        public int val;//存储数据
        public Node next;//存储下一个节点地址

        public Node(int val) {
            //不知道下一个节点位置,只需要val
            this.val = val;
        }

    }
    public Node head;//代表当前头节点的引用

    //遍历链表
        //创建链表
        public void createLink(){
            Node node1 = new Node(12);
            Node node2 = new Node(45);
            Node node3 = new Node(23);
            Node node4 = new Node(90);

            node1.next = node2;
            node2.next = node3;
            node3.next = node4;
            head = node1;
        }
        //遍历链表,利用head遍历链表
        public void display() {
            while (head != null){
                System.out.println(head.val);
                head = head.next;
            }
        }
        //查找是否包含关键字key是否在单链表当中
        public boolean contains(int key){
            Node cur = head;
            while (cur != null){
                if (cur.val == key){
                    return true;
                }
                cur = cur.next;
            }
            return false;
        }
        //头插法
        public void addFirst(int data){
            Node node = new Node(data);
            node.next = head;
            head = node;
        }

        //尾插法,需判断head是否为空
        public void addLast(int data){
            Node node = new Node(data);
            if (head == null) {
                head = node;
                return;
            }
                Node cur = head;
                while (cur.next != null) {
                    cur = cur.next;
                }
                cur.next = node;
            }




        //任意位置插入,第一个数据节点为0号下标
        public void addIndex(int index,int data){
            checkindex(index);
            if (index == 0){
                addFirst(data);
                return;
            }
            if (index == size()){
                addLast(data);
                return;
            }

            Node cur = findIndexSubOne(index);
            Node node = new Node(data);
            node.next = cur.next;
            cur.next = node;



        }
        /*
        找到index-1处的节点
         */
        private Node findIndexSubOne(int index){
            Node cur = head;
            int count = 0;
            while (count != index -1 ){
                cur = cur.next;
                count++;
            }
            return cur;

        }
        private void checkindex(int index){
            if (index<0||index>size()){
                throw new IndexOutOfException("index位置不合法");
            }
        }

        //删除第一次出现关键字为key的节点
        public void remove(int key){
            if (head.val == key){
                head = head.next;
                return;
            }
          Node cur = searchPrev(key);
            if (cur == null){
                return;
            }
          cur.next = cur.next.next;
        }

        //删除所有值为key的节点
        public void removeAllKey(int key){

            if (head == null){
                return;
            }
            //如果前俩个节点值均为key时。
            // 或者用if放到最后,直接删掉漏掉的节点
            while (head.val==key){
                head = head.next;
            }
            Node prev = head;
            Node cur = head.next;
            while (cur != null){
                if (cur.val == key){
                    prev.next = cur.next;
                    cur = cur.next;
                }else{
                    prev = cur;
                    cur = cur.next;
                }
            }

        }
        //找到关键字key的前一个节点
    private Node searchPrev(int key){
            if (head== null){
                return null;//链表为空时
            }
            Node cur = head;
            while (cur.next!= null){
                if (cur.next.val == key){
                    return cur;
                }
                cur = cur.next;
            }
            return null;//没有需要删除的节点
    }

        //得到单链表的长度
        public int size(){
            int count = 0;
            Node cur = head;
            while (cur != null){
                count++;
                cur = cur.next;
            }
            return count;
        }

        /*
        保证列表中每一个节点都被回收
         */
        public void clear() {
            ListNode cur = head;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        last = null;
        }
}


4.不带头非循环双向链表CURD

代码展示:

package demo4;

import demo3.MySingleList;

import java.util.List;

/**
 * @author zq
 * LinkedList的实现
 * 双向链表
 */
public class MyLinkedList {
    static class ListNode{
        ListNode prev;
        ListNode next;
        int val;

        public ListNode(int val) {
            //不知道下一个节点位置,只需要val
            this.val = val;
        }

    }
    public MyLinkedList.ListNode head;//代表当前头节点的引用
    public MyLinkedList.ListNode last;//代表最后节点的引用


    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            last = node;
        }else{
            node.next = head;
            node.prev = null;
            head.prev = node;
            head = node;
        }

    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
            last = node;
        }else{
            last.next = node;
            node.prev = last;
            node.next = null;
            last = node;
        }

    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        ListNode node = new ListNode(data);
        checkindex(index);
        if (index==0){
            addFirst(data);
            return;
        }
        if (index==size()){
            addLast(data);
            return;
        }
        ListNode cur = findIndex(index);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;



    }
    //判断插入位置是否合法
    private void checkindex(int index){
        if (index<0||index>size()){
            throw new IndexOutOfException("index位置不合法");
        }
    }
    /*
    找到index处的节点
         */
    private ListNode findIndex(int index){
        ListNode cur = head;
        while (index != 0){
            cur = cur.next;
            index--;
        }
        return cur;

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

        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                //删除的是头节点
                if(cur == head) {
                    head=head.next;
                    //如果只有一个节点时处理方式
                    //否则会有空指针异常
                    if (head !=null) {
                        head.prev = null;
                    }
                }else{
                    //中间,尾巴都能用此行代码。
                    cur.prev.next = cur.next;
                    if (cur.next != null){
                        //不是尾巴节点
                        cur.next.prev = cur.prev;
                    }else {
                        last = cur.prev;
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                //删除的是头节点
                if(cur == head) {
                    head=head.next;
                    //如果只有一个节点时处理方式
                    //否则会有空指针异常
                    if (head !=null) {
                        head.prev = null;
                    }
                }else{
                    //中间,尾巴都能用此行代码。
                    cur.prev.next = cur.next;
                    if (cur.next != null){
                        //不是尾巴节点
                        cur.next.prev = cur.prev;
                    }else {
                        last = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }


    }
    //得到单链表的长度
    public int size(){
        int len = 0;
        ListNode cur = head;
        while (cur != null){
            cur = cur.next;
            len++;
        }

        return len;
    }
    public void display(){
        ListNode cur = head;
       while (cur !=null){
           System.out.print(cur.val + " ");
           cur = cur.next;
       }
    }
    public void clear(){
        head = null;
    }
}

  • 6
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据结构和算法在Java中是密切相关的。数据结构是一门研究组织数据方式的学科,而算法则是处理数据的方法。在Java中,学习好数据结构可以帮助我们编写更漂亮、更高效的代码。数据结构是算法的基础,想要学好算法就需要将数据结构学到位。\[1\] 在Java中,我们可以使用各种数据结构和算法来解决问题。常见的数据结构包括数组、链表、栈、队列、树、图等。而算法则是对这些数据结构进行操作和处理的方法。通过选择合适的数据结构和算法,我们可以提高程序的效率和性能。 在Java中,我们可以使用Java集合框架中提供的数据结构,如ArrayList、LinkedList、HashMap等。这些数据结构已经被封装好,我们可以直接使用它们来解决问题。同时,我们也可以根据具体的需求,自己实现一些特定的数据结构和算法。 如果想深入了解Java中的数据结构和算法,可以参考一些相关的资料和链接。比如,可以了解Java中的哈希算法及HashMap的底层实现原理,以及常用的数据结构集合框架对比总结。\[2\] \[3\] 总之,数据结构和算法在Java中是相互依赖的。学习好数据结构可以帮助我们更好地理解和应用算法,而学习好算法则可以提高我们解决问题的效率和质量。 #### 引用[.reference_title] - *1* [数据结构与算法【Java版】:第一课](https://blog.csdn.net/sum_12/article/details/115621379)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Java——数据结构与算法](https://blog.csdn.net/m0_64359609/article/details/124174183)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [数据结构与算法——Java版](https://blog.csdn.net/su2231595742/article/details/120618818)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值