ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

单向环形链表

2022-03-27 08:32:43  阅读:182  来源: 互联网

标签:Node helper int 单向 环形 next 链表 public first


joseph问题

单向环形链表

节点类

class Node {
    public int id;
    public Node next;

    @Override
    public String toString() {
        return "Node{" +
                "id=" + id +
                '}';
    }

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

链表类

class CircleSingleLinkedList {
    private Node first = null;

    // 报数出圈
    public void countNode(int startId, int countNum, int nums) {
        if (isEmpty() || startId < 1 || startId > nums) {
            System.out.println("参数不合法");
            return;
        }
        // 定义辅助指针指向first前面
        Node helper = first;
        while (true) {
            if (helper.next == first) {
                break;
            }
            helper = helper.next;
        }
        // 此时辅助指针指向first前一个节点
        // 报数前,先将两个指针移动到startId
        for (int i = 0; i < startId - 1; i++) {
            first = first.next;
            helper = helper.next;
        }
        // 循环报数
        while (true) {
            if (first == helper) {
                break;
            }
            // 将辅助指针移动countNum - 1次
            for (int i = 0; i < (countNum - 1); i++) {
                first = first.next;
                helper = helper.next;
            }
            // 此时first指向的节点就是要出圈的节点
            System.out.println(first + "出圈");
            // 出圈
            first = first.next;
            helper.next = first;
        }
        System.out.println("最后留下的是" + first);
    }


    // 创建nums个节点
    public void addNodes(int nums) {
        if (nums < 1) {
            System.out.println("nums不合法");
            return;
        }
        // 辅助指针
        Node cur = null;
        for (int i = 1; i <= nums; i++) {
            // 创建
            Node node = new Node(i);
            // 插入
            // 如果是第一个
            if (i == 1) {
                first = node;
                // 构成环状
                first.next = first;
                // cur移动
                cur = first;
            } else {
                cur.next = node;
                node.next = first;
                // cur移动
                cur = node;
            }
        }
    }

    // 遍历
    public void show() {
        if (isEmpty()) {
            System.out.println("没有任何小孩");
            return;
        }
        Node cur = first;
        while (true) {
            System.out.println(cur.id);
            if (cur.next == first) {
                break;
            }
            // cur右移
            cur = cur.next;
        }
    }

    private boolean isEmpty() {
        return first == null;
    }
}

测试类

public class Joseph {
    public static void main(String[] args) {
        CircleSingleLinkedList list = new CircleSingleLinkedList();
        list.addNodes(5);
        list.countNode(1,2,5);
    }
}

标签:Node,helper,int,单向,环形,next,链表,public,first
来源: https://www.cnblogs.com/coderDreams/p/16061849.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有