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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM