java实现的带头单向循环链表

 

package com.bit;

public interface CLinkList {
    //头插法
    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 com.bit;

public class CHeadSingleListImpl implements CLinkList{
    //内部类
    class Node{
        private Node next;
        private int data;
        //node的data域没有值,提供一个不带有参数的构造函数
        public Node(){
            //用-1去标识data域中的数据
            this.data = -1;
            this.next = null;
        }
        //实体结点应该有data值
        public Node(int data){
            this.data = data;
        }
    }
    private Node head;
    public CHeadSingleListImpl(){
        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;
    }

    private void checkIndex(int index){
        if(index<0||index>getLength()){
            throw new UnsupportedOperationException("下标不合法");
        }
    }
    @Override
    public boolean addIndex(int index, int data) {
        checkIndex(index);
        Node cur = this.head;
        for (int i = 0;i < index;i++){
            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.next;
        while(cur!=head){
            if(cur.data == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    private Node searchPrev(int key){
        int oldData = 0;
        Node prev = this.head;
        while(prev.next != this.head){
            if(prev.next.data == key){
                return prev;
            }
            prev = prev.next;
        }
        return null;
    }
    @Override
    public int remove(int key) {
        Node prev = searchPrev(key);
        if(prev == null) {
            //return -1;
            throw new UnsupportedOperationException("key不存在前驱");
        }
        int oldData = 0;
        Node delNode = prev.next;
        oldData = delNode.data;
        prev.next = delNode.next;
        return oldData;
    }

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

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

    @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() {
        while(this.head.next!= this.head){
            Node cur = this.head.next;
            this.head.next = cur.next;
            cur.next = null;
        }
        this.head = null;
    }

    public static void main(String[] args) {

    }
}
package com.bit;


public class TestDemo {
    public static void main(String[] args) throws InterruptedException {
        CHeadSingleListImpl cHeadSingleList = new CHeadSingleListImpl();
        cHeadSingleList.addFirst(18);
        cHeadSingleList.addFirst(18);
        cHeadSingleList.addFirst(99);
        cHeadSingleList.display();
        cHeadSingleList.addIndex(0,18);
        cHeadSingleList.addLast(100);
        cHeadSingleList.display();
        System.out.println(cHeadSingleList.contains(178));
        cHeadSingleList.removeAllKey(18);
        cHeadSingleList.display();
        cHeadSingleList.clear();
        Thread.sleep(1000);
        System.out.println("睡醒了");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值