约瑟夫问题循环链表的实现

#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#include<string>
struct node
{
    long d;
    node * next;
} ;
long n,m;
node * head,* p,* r;
int main()
{
    long i,j;
    cin>>n>>m;
    head=(node*)malloc(sizeof(node));

    head->d=1;
    head->next=NULL;
    r=head;
    for(i=2;i<=n;i++)
    {
        p=(node*)malloc(sizeof(node));

        p->d=i;
        p->next=NULL;
        r->next=p;
        r=p;
    }
    r->next=head;
    r=head;
    for (i=1;i<=n;i++)
    {
    for(j=1;j<=m-2;j++)    r=r->next;
     
        r->next=r->next->next;
        r=r->next;
    }
cout<<r->d<<endl;    
return 0;
}

猜你喜欢

转载自blog.csdn.net/u010583225/article/details/81277393