3、循环链表(java实现)

1、节点类

public class Node<T> {
    public T data;
    public Node next;
}

2、实现类

public class CircularLink<T> {

    private static Node head = null;

    /**
     * 初始化
     */
    public void initCircularLink() {
        head = new Node();
        head.data = null;
        head.next = head;
    }

    /**
     * 插入节点
     *
     * @param element :节点元素值
     */
    public void insertCircularLink(T element) {
        Node node = new Node(); //初始化节点
        node.data = element;
        if (head.next == head) {
            head.next = node;
            node.next = head;
        } else {
            Node temp = head;
            while (temp.next != head) {
                temp = temp.next;
            }
            temp.next = node;
            node.next = head;

        }
    }

    /**
     * 值删除
     *
     * @param element : 要删除的值
     * @return :删除则为true 否则为假
     */
    public boolean deleteCircularLink(T element) {

        Node temp = head;
        if (temp.next == head) {
            System.out.println("链表已空,没有可删除的值");
            return false;
        }
        while (temp.next != head) {
            if (temp.next.data == element) {
                temp.next = temp.next.next;
                return true;
            } else {
                temp = temp.next;
            }
        }
        return false;
    }

    /**
     * 下标删除
     *
     * @param i : 要删除的值下标
     * @return :删除则为true 否则为假
     */
    public boolean deleteIndexCircularLink(int i) {


        Node temp = head;
        int index = -1;
        if (temp.next == head) {
            System.out.println("链表已空,没有可删除的值");
            return false;
        }
        if (i < 0 || i >= sizeCircularLink()) {
            System.out.println("越界下标");
            return false;
        }
        while (temp.next != head) {
            index++;
            if (index == i) {
                temp.next = temp.next.next;
            }
            temp = temp.next;
        }
        return false;

    }

    /**
     * 给定值求下标
     *
     * @param element :要找的元素
     */
    public void findCircularLink(T element) {
        Node temp = head;
        int index = -1;
        if (temp.next == head) {
            System.out.println("链表已空,无法查找");

        }
        while (temp.next != head) {
            temp = temp.next;
            index++;
            if (temp.data == element) {
                temp = temp.next;
                System.out.println("下标为: " + index);
                return;
            }
        }
        System.out.println("你要找的值不在这里");
    }

    /**
     * 下标求值
     * @param index
     */
    public void findDataCircularLink(int index) {
        Node temp = head;
        int size= 0;           //为增加下标用的
        if (temp.next == head) {
            System.out.println("链表已空,没有可删除的值");
            return;
        }
        if (index >= sizeCircularLink() || index < 0){
            System.out.println("你的下标越界");
            return;
        }

        while (temp.next !=head){
            temp = temp.next;

            if (size == index){
                System.out.println("你要找的值为: "+temp.data);
                return;
            }
            size++;
        }
        System.out.println("没有你的值");
    }

    /**
     * 打印
     */
    public void printCircularLink() {
        Node temp = head;
        System.out.print("打印循环链表: ");
        while (temp.next != head) {
            temp = temp.next;
            System.out.print(temp.data + " ");

        }
        System.out.println();
    }


    /**
     * 求长度
     *
     * @return : 返回的长度
     */
    public static int sizeCircularLink() {
        Node temp = head;
        int size = 0;
        while (temp.next != head) {
            temp = temp.next;
            size++;
        }
        return size;
    }

    public static void main(String[] args) {
        CircularLink<Integer> circularLink = new CircularLink();
        circularLink.initCircularLink();
        circularLink.insertCircularLink(1);
        circularLink.insertCircularLink(2);
        circularLink.insertCircularLink(3);
        circularLink.insertCircularLink(4);
        circularLink.insertCircularLink(5);

        circularLink.printCircularLink();
        System.out.println("长度: " + circularLink.sizeCircularLink());
        System.out.println();

        System.out.println("值删除值");
        circularLink.deleteCircularLink(1);
        circularLink.printCircularLink();
        System.out.println("长度: " + circularLink.sizeCircularLink());
        System.out.println();

        System.out.println("下标删除值");
        circularLink.deleteIndexCircularLink(1);
        circularLink.printCircularLink();
        System.out.println("长度: " + circularLink.sizeCircularLink());
        System.out.println();

        System.out.println("值查找下标");
        circularLink.findCircularLink(5);
        circularLink.printCircularLink();
        System.out.println("长度: " + circularLink.sizeCircularLink());
        System.out.println();

        System.out.println("下标查找值");
        circularLink.findDataCircularLink(3);
        circularLink.printCircularLink();
        System.out.println("长度: " + circularLink.sizeCircularLink());
        System.out.println();

    }
}

3、测试结果

打印循环链表: 1 2 3 4 5 
长度: 5

值删除值
打印循环链表: 2 3 4 5 
长度: 4

下标删除值
打印循环链表: 2 4 5 
长度: 3

值查找下标
下标为: 2
打印循环链表: 2 4 5 
长度: 3

下标查找值
你的下标越界
打印循环链表: 2 4 5 
长度: 3

猜你喜欢

转载自www.cnblogs.com/karrya/p/11030211.html