心情不错,写个带头循环单链表

本文介绍了一种带头结点的循环单链表的数据结构实现,包括头插法、尾插法、任意位置插入等核心操作,并实现了查找、删除等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package Interface;
//带头循环单链表
public interface ICLinked {
    //头插法
    void addFirst(int data);
    //尾插法
    void addLast(int data);
    //任意位置插入,第一个数据节点为0号下标
    boolean addindex(int index,int data);
    //查找是否包含关键字key是否在单链表当中
    boolean contains(int key);
    //删除第一次出现关键字为key的节点
    int remove(int key);
    //删除所有值为key的节点
    void removeAllKey(int key);
    //得到单链表的长度
    int getLength();
    void display();
    void clear();
}

package DS;
//带头循环单链表
import Interface.ICLinked;

public class MyHeadSingleListImpl implements ICLinked {
    class Node {
        private int data;
        private Node next;
        public Node(int data) {
            this.data = data;
            this.next = null;
        }

        public Node() {
            this.data = -1;
            this.next = null;
        }
    }

    private Node head;
    private MyHeadSingleListImpl() {
        this.head = new Node();
        this.head.next = this.head;
    }

    @Override
    public void addFirst(int data) {
        Node node = new Node(data);
        //以下两句顺序很重要
        node.next = this.head.next;
        this.head.next = node;
    }

    @Override
    public void addLast(int data) {
        Node cur = this.head;
        while (cur.next != this.head) {
            cur = cur.next;
        }
        Node node = new Node(data);
         node.next = this.head;
        cur.next = node;
    }

    @Override
    public boolean addindex(int index, int data) {
        if (index < 0 || index > getLength()) {
            return false;
        }
        if (index == 0) {
            addFirst(data);
            return true;
        } else if (index == getLength()) {
            addLast(data);
            return true;
        }
        //找下标,跳出循环时cur就是index下标
        Node cur = this.head.next;
        int count = 1;
        while (count != index) {
            count++;
            cur = cur.next;
        }
        Node node = new Node(data);
        node.next = cur.next;
        cur.next = node;
        return true;
    }

    @Override

    public boolean contains(int key) {
        Node cur = this.head;
        while (cur.next != this.head) {
            if (cur.data == key) {
                return true;
            }
            cur = cur.next;
        }
        if (cur.data == key) {
            return true;
        }
        return false;
    }

    public Node SearchPrev(int key) {
        Node cur = this.head;
        //找前驱
        while (cur.next != this.head) {
            if (cur.next.data == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    @Override
    public int remove(int key) {
        if(this.head == null ){
            throw new UnsupportedOperationException("单链表为空");
        }
        Node prev = SearchPrev(key);
        if (prev == null) {
            throw new UnsupportedOperationException("没有前驱!");
        }
        if (this.head.data == key) {
            this.head = this.head.next;
            return key;
        }
        Node delete = prev.next;
        prev.next = delete.next;
        return delete.data;
    }

    @Override
    public void removeAllKey(int key) {
        if (this.head == null || this.head.next == this.head) {
            return;
        }
        if (this.head.data == key) {
            this.head = this.head.next;
        }
        Node prev = this.head;
        Node cur = this.head.next;
        while (cur != this.head) {
            if (cur.data == key) {
                prev.next = cur.next;
                cur = prev.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
    }

    @Override
    public int getLength() {
        Node cur = this.head;
        int length = 0;
        while (cur.next != this.head) {
            length++;
            cur = cur.next;
        }
        return length;
    }

    @Override
    public void display() {
        Node cur = this.head.next;
        while (cur != this.head) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {

    }

    public static void main(String[] args) {
        MyHeadSingleListImpl m = new MyHeadSingleListImpl();
        m.addLast(13);
        m.addFirst(11);
        m.addFirst(12);
        m.addFirst(114);
        m.addFirst(15);
        m.display();
        m.addindex(1,15);
        m.display();
        m.remove(11);
        m.display();
        m.removeAllKey(15);
        m.display();
        System.out.println(m.getLength()+" "+m.contains(12));
    }
}

在这里插入图片描述

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值