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

带头结点的循环单链表

程序员文章站 2024-03-21 19:46:28
...
/**
 * @author yhd
 * @email 
 * @description 带头结点的循环单链表
 * @since 2021/4/2 9:08
 */
public class CircularLinkedList {
    static class Node {
        public int data;
        public Node next;

        public Node() {
        }

        public Node(int data, Node next) {
            this.data = data;
            this.next = next;
        }

        public Node(int data) {
            this.data = data;
        }
    }

    Node head;

    {
        head = new Node();
        head.next = head;

    }


    /**
     * @return
     * @author yhd
     * @email 
     * @description 头插
     * @params
     * @since 2021/4/2 9:15
     */
    public void insertElement(int data) {
        head.next = new Node(data, head.next);
    }

    /**
     * @return
     * @author yhd
     * @email 
     * @description 尾插
     * @params
     * @since 2021/4/2 9:23
     */
    public void insertElement2(int data) {

        //如果只有头结点
        if (head.next == head) {
            Node node = new Node(data);
            head.next = node;
            node.next = head;
        } else {
            //定义指针
            Node p = head;
            //跳出循环的条件:指针指向head的上一个节点
            while (p.next != head) {
                p = p.next;
            }
            //node入链表
            Node node = new Node(data);
            p.next = node;
            node.next = head;
        }

    }

    /**
     * @return
     * @author yhd
     * @email 
     * @description 删除指定位置的元素
     * @params
     * @since 2021/4/2 9:27
     */
    public void remove(int i) {
        Node p = head.next;
        Node q = head;
        int count = 1;
        //头尾不相等的时候
        while (p != head) {
            if (count == i) {
                q.next = q.next.next;
            }
            count++;
            p = p.next;
            q = q.next;
        }

    }

    /**
     * @return
     * @author yhd
     * @email 
     * @description 获取当前链表的元素个数
     * @params
     * @since 2021/4/2 10:41
     */
    public int getTotal() {
        int total = 0;
        Node p = head;
        while (p.next != head) {
            total++;
            p = p.next;
        }
        return total;
    }

    /**
     * @return
     * @author yhd
     * @email 
     * @description 遍历
     * @params
     * @since 2021/4/2 10:44
     */
    public void print() {
        Node p = head;
        while (p.next != head) {
            System.out.print(p.next.data + "-->");
            p = p.next;
        }
    }

    /**
     * @return
     * @author yhd
     * @email 
     * @description 根据元素查找
     * @params
     * @since 2021/4/2 10:44
     */
    public int getLocation(Node node) {
        int total = 1;
        Node p = head.next;
        while (p != head) {
            if (p.data == node.data) {
                return total;
            }
            total++;
            p = p.next;
        }
        return 0;
    }

    /**
     * @return
     * @author yhd
     * @email 
     * @description 根据位置查找
     * @params
     * @since 2021/4/2 10:45
     */
    public Node getNode(int location) {

        Node p = head.next;
        int count = 1;
        while (p.next != head) {
            if (count == location) {
                return p;
            }
            count++;
            p = p.next;
        }
        return null;
    }
}