2021-9-下旬 数据结构 - 线性表 - 循环链表 - java实现代码(复习用)

public class CycleLinkedListZH<E> {

    public static final  int ELEMENT_NOT_FOUND = -1;

    private int size;//定义全局变量(不在函数里的)不初始化默认初始值为0,局部变量则初始值随机
    //链表注意的无非那几个:插入删除情况分类,空指针错误

    private CycleLinkedListZH.Node<E> headNode;

    private static class Node<E> {
        E element;
        CycleLinkedListZH.Node<E> next = null;

        public Node(E element, CycleLinkedListZH.Node<E> next) {
            this.element = element;
            this.next = next;
        }
        public Node() {
        }
    }


    public void clear(){
        headNode = null;
        size = 0;
    }

    public int getSize(){
        return size;
    }

    public void isEmptyPrint(){
        if (headNode == null){
            System.out.println("Empty");
        }else{
            System.out.println("Not Empty");
        }
    }

    public E getElement(int index){
        CycleLinkedListZH.Node<E> node = indexSearchNode(index);
        return node.element;
    }


    public void containsPrint(E element){
        CycleLinkedListZH.Node<E> node = headNode;
        for(int i = 0;i < size ; i++){
            if (node.element == element){
                System.out.println("Contain");
                return;
            }
            node = node.next;
        }
        System.out.println("Not Contain");
    }


    public void add(int index , E element){
        CycleLinkedListZH.Node<E> node = new Node<>();
        node.element = element;
        if (size == 0){
            headNode = node;
            node.next = node;
        }else if (index == 0){
            CycleLinkedListZH.Node<E> lastnode = indexSearchNode(size - 1);
            lastnode.next = node;
            node.next = headNode;
            headNode = node;
        }else{
            CycleLinkedListZH.Node<E> nodePre = indexSearchNode(index-1);
            node.next = nodePre.next.next;
            nodePre.next = node;
        }
        size++;
    }

    public void remove(int index){
        if (index == 0){
            CycleLinkedListZH.Node<E> lastNode = indexSearchNode(size - 1);
            lastNode.next = lastNode.next.next;
            headNode = headNode.next;
        }else if (size == 0||size == 1){
            headNode = null;
        } else{
            CycleLinkedListZH.Node<E> nodePre = indexSearchNode(index-1);
            nodePre.next = nodePre.next.next;
        }
        size--;
    }

    public void add(E element){
        add(size,element);
    }


    private CycleLinkedListZH.Node<E> indexSearchNode (int index){
        CycleLinkedListZH.Node<E> node = headNode;
        for (int i = 0; i<index; i++){
            node = node.next;
        }
        return node;
    }

    public String toString() {
        StringBuilder string = new StringBuilder();
        string.append("size=").append(size).append("  [");
        CycleLinkedListZH.Node<E> node = headNode;
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                string.append(", ");
            }

            string.append(node.element);

            node = node.next;
        }
        string.append("]");
        return string.toString();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值