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

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

摘要生成于 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));
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值