【Java数据结构】单向 不带头 非循环 链表实现

模拟实现LinkedList:下一篇文章

LinkedList底层是双向、不带头结点、非循环的链表

复制代码
/**
 * LinkedList的模拟实现
 *单向 不带头 非循环链表实现
 */
class SingleLinkedList {
    class ListNode {
        public int val;
        public ListNode next;

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

    public ListNode head;//永远指向头结点

    //创建链表
    public void createList() {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
    }
    //显示
    public void display() {
        while (head != null) {
            System.out.print(head.val + " ");
            head = head.next;//head往后移
        }
    }
    //得到单链表的长度
    public int size() {
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    //清空
    public void clear() {
        this.head = null;
    }
    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }
    //尾插法
    public void addLast(int data) {
        ListNode cur = head;
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
        }
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;//这时cur就是尾巴节点
    }
    //在任意位置插入,第一个数据节点为0的下标
    public void addIndex(int index, int data) {
        ListNode node = new ListNode(data);
        int len = size();
        //0.判断index位置是否合法
        if (index < 0 || index > len) {
            return;//也可以抛异常
        }
        //1.先找到index-1的位置  下面有个findIndex方法
        ListNode cur = findIndex(index);
        //2.插入数据
        node.next = cur.next;
        cur.next = node;
    }

    private ListNode findIndex(int index) {
        ListNode cur = head;
        while (index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;//index-1位置的节点
    }

    //查找是否包含关键字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) {
        if(head==null){
            return;
        }
        if(head.val==key){
            head=head.next;
            return;
        }
        ListNode prev = searchPrev(key);
        if (prev== null) {
            System.out.println("没有这个数据");
            return;
        }
        ListNode del=prev.next;
        prev.next=del.next;
    }
    private ListNode searchPrev(int key){
        ListNode prev=head;
        while(prev.next!=null){
            if(prev.next.val==key){
                return prev;
            }else{
                prev=prev.next;
            }
        }
        return null;
    }

    //删除所有值为key的节点
    public void removeAllkey(int key) {
        if(head==null){
            return;
        }
        ListNode cur=head.next;
        ListNode prev=head;
        while(cur!=null){
            if(cur.val==key){
                prev.next=cur.next;
                cur=cur.next;
            }else{
                prev=cur;
                cur=cur.next;
            }
        }
        if(head.val==key){
            head=head.next;
        }
    }
}
public class Test {
    public static void main(String[] args) {
        SingleLinkedList singleLinkedList=new SingleLinkedList();
        singleLinkedList.createList();
        singleLinkedList.display();
    }
}

此处只调用了createList()和display()。需要其他方法的自己可以在main中调用哦

相关推荐
带刺的坐椅6 分钟前
qwen3 惊喜发布,用 ollama + solon ai (java) 尝个鲜
java·spring·solon·solon-ai·qwen3
Vacant Seat34 分钟前
贪心算法-跳跃游戏II
算法·游戏·贪心算法
.生产的驴40 分钟前
SpringBoot 接口国际化i18n 多语言返回 中英文切换 全球化 语言切换
java·开发语言·spring boot·后端·前端框架
夜松云43 分钟前
从对数变换到深度框架:逻辑回归与交叉熵的数学原理及PyTorch实战
pytorch·算法·逻辑回归·梯度下降·交叉熵·对数变换·sigmoid函数
Howard_Stark43 分钟前
Spring的BeanFactory和FactoryBean的区别
java·后端·spring
饮长安千年月44 分钟前
学生管理系统审计
java·网络安全·代码审计
八股文领域大手子1 小时前
深入浅出限流算法(三):追求极致精确的滑动日志
开发语言·数据结构·算法·leetcode·mybatis·哈希算法
-曾牛1 小时前
Spring Boot中@RequestParam、@RequestBody、@PathVariable的区别与使用
java·spring boot·后端·intellij-idea·注解·spring boot 注解·混淆用法
新时代苦力工1 小时前
处理对象集合,输出Map<String, Map<String, List<MyObject>>>格式数据,无序组合键处理方法
java·数据结构·list
啊阿狸不会拉杆1 小时前
人工智能数学基础(一):人工智能与数学
人工智能·python·算法