算法学习Day-2 使用链表解决约瑟夫环

class Node{
    public Object data;
    public Node next;

    public Node getNext() {
        return next;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public void setNext(Node next) {
        this.next = next;
    }
    Node(){
        super();
    }
    public Node(Object data, Node next){
        super();
        this.data=data;
        this.next=next;
    }
}

public class YueSeFu{
    public static void main(String[] args) {
        Node node1=new Node("1",null);
        Node node2=new Node("2",null);
        Node node3=new Node("3",null);
        Node node4=new Node("4",null);
        Node node5=new Node("5",null);
        Node node6=new Node("6",null);
        Node node7=new Node("7",null);
        Node header = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        node6.next = node7;
        node7.next = node1;

        teat(header);
    }


    private static void teat(Node header) {
        Node n = header;
        Node lastOne ;//表示上一个节点
        int  count  = 1 ;
        System.out.print("依次出列的顺序为:");
        while(n != n.next){//最后环中只剩下一个人,那肯定是它的下一个就是它本身  即  n=n.next

            lastOne = n;
            n = n.next;
            count ++ ;

            if(count == 3){//若数到3,出列
                lastOne.next = n.next;//上一个的节点的下一个就是当前的下一个   即lastOne.next = n.next;
                count = 0;//count清 0
                System.out.print(n.data   + "   ");//打印出列的元素
            }
        }
        System.out.println();
        System.out.println("最后剩下的是:" + n.data);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
约瑟夫环问题是一个经典的数学问题,具体描述如下:有n个人(编号为1~n),围成一个圆圈,从第一个人开始报数,报到m的人出圈,然后从他的下一个人开始重新报数,重复这个过程,直到剩下最后一个人。 下面是使用循环链表解决约瑟夫环问题的C语言实现: ``` #include <stdio.h> #include <stdlib.h> typedef struct node { int num; struct node *next; } Node; void create_circle(Node **head, int n) { Node *tail = NULL, *p = NULL; for (int i = 1; i <= n; i++) { p = (Node *)malloc(sizeof(Node)); p->num = i; if (*head == NULL) { *head = p; tail = p; } else { tail->next = p; tail = p; } } tail->next = *head; // 将尾节点指向头节点,形成循环链表 } void josephus_circle(Node **head, int m) { Node *p = *head, *q = NULL; int count = 0; while (p->next != p) { count++; if (count == m) { q->next = p->next; printf("%d出圈\n", p->num); free(p); p = q->next; count = 0; } else { q = p; p = p->next; } } printf("剩下%d号\n", p->num); *head = p; } int main() { int n, m; Node *head = NULL; printf("请输入总人数n和报数m:"); scanf("%d%d", &n, &m); create_circle(&head, n); josephus_circle(&head, m); return 0; } ``` 首先,使用 `create_circle` 函数创建一个有 n 个节点的循环链表,其中头节点指针保存在 `head` 变量中。 然后,使用 `josephus_circle` 函数来模拟约瑟夫环问题。使用两个指针 `p` 和 `q` 遍历循环链表, `count` 记录报数的次数。当 `count` 等于 `m` 时,删除节点 `p`,并输出其编号。否则,指针 `p` 和 `q` 向后移动。当循环链表中只剩下一个节点时,输出其编号并退出循环。 最后,将头节点指针 `head` 指向最后剩下的节点,返回其编号。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值