java--带头循环单链表

首先创建一个接口

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();
}

然后实现接口:

public class HeadSingleListImpl implements ICLinked {

    class Node{
        private int data;
        private  Node next;
        //头结点
        public Node(){
            this.data = -1;
        }
        //数据节点
        public Node(int data){
            this.data = data;
        }
    }
    private Node head;
    public HeadSingleListImpl(){
        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 node = new Node(data);
      Node cur = this.head;
     while(cur.next != this.head){
         cur = cur.next;
     }
     node.next = cur.next;
     cur.next = node;
    /* cur.next = node;
     node.next = this.head;*/
    }

    //index-1的位置
    private  Node searchIndex(int index){
        checkIndex(index);
        Node cur = this.head;
        for (int i = 0; i <index ; i++) {
            cur = cur.next;
        }
        return cur;
    }
    private void checkIndex(int index){
        if(index<0 || index>getLength()){
            throw new UnsupportedOperationException("index不合法");
        }
    }
    @Override
    public boolean addindex(int index, int data) {
        Node pre = searchIndex(index);
        Node node  = new Node(data);
        node.next = pre.next;
        pre.next = node;
        return true;
    }

    @Override
    public boolean contains(int key) {
        Node cur = this.head.next;
        while (cur != this.head) {
            if (cur.data == key) {
                return true;
            }
            cur = cur.next;
        }
            return false;
    }
    //如果找不到返回空
    private  Node searchPre(int key){
        Node pre = this.head;
        while(pre.next != this.head){
            if(pre.next.data == key){
                return pre;
            }
            pre = pre.next;
        }
        return null;
    }

    @Override
    public int remove(int key) {
        Node pre = searchPre(key);
        if(pre == null){
            throw new UnsupportedOperationException("没有key这个关键字");
        }
        Node del = pre.next;
        int oldData = del.data;
        pre.next= del.next;
        return oldData;
    }

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

        }
    }
    @Override
    public int getLength() {
        int count = 0;
        //cur指向第一个数据节点
       Node cur = this.head.next;
       while(cur != this.head){
           count++;
           cur = cur.next;
       }
        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() {
        //防止内存泄漏
        if(this.head.next != this.head){
            Node cur = this.head.next;
            this.head.next = cur.next;
        }
        //头结点为空
        this.head = null;

    }
}

测试类:

public class TestMain {
    public static void main(String[] args) {
        HeadSingleListImpl headSingleList = new HeadSingleListImpl();
        headSingleList.addFirst(10);
        headSingleList.addFirst(20);
        headSingleList.addFirst(30);
        headSingleList.addFirst(40);
        headSingleList.display();

        headSingleList.addLast(10);
        headSingleList.addLast(20);
        headSingleList.addLast(30);
        headSingleList.addLast(40);
        headSingleList.display();// 40 30 20 10 10 20 30 40
/*      headSingleList.addindex(0,77);
        headSingleList.display();//77 40 30 20 10 10 20 30 40
        headSingleList.addindex(0,66);
        headSingleList.display();//66 77 40 30 20 10 10 20 30 40
        headSingleList.addindex(8,77);
        headSingleList.display();//66 77 40 30 20 10 10 20 77 30 40
        headSingleList.addindex(3,87);
        headSingleList.display();//66 77 40 87 30 20 10 10 20 77 30 40*/
/*        System.out.println(headSingleList.contains(20));
        System.out.println(headSingleList.contains(999));
        System.out.println(headSingleList.remove(66));
        headSingleList.display();
        System.out.println(headSingleList.remove(40));
        headSingleList.display();*/

        /*headSingleList.removeAllKey(20);
        headSingleList.display();*/

        headSingleList.clear();
        headSingleList.display();

    }
}    

以下就是运行后的结果在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值