欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-03-15 20:33:50
...

 

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("睡醒了");
    }
}