当先锋百科网

首页 1 2 3 4 5 6 7
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int n,m;

typedef int ElemType;
typedef struct LNode{
    ElemType data;
    struct LNode *next;
}LinkList;

void DispList(LinkList *L,int m)
{
    LinkList *p = L;
    LinkList *lp = L;
    /*
    while(p != L)
    {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("\n");
    */
    int k=0,i=1;
    while(true){
        if(i==m){
            cout<<p->data<<" ";
            i=1;
            k++;
            lp->next=p->next;
            p=p->next;
            if(k==n) break;
        }
        else{
            lp=p;
            p=p->next;
            i++;
        }

    }
}

void ListInsert(LinkList *&L,int n){
    LinkList *s,*r;
    L=(LinkList *)malloc(sizeof(LinkList));
    r=L;
    L->data=1;
    for(int i=2;i<=n;i++){
        s=(LinkList *)malloc(sizeof(LinkList));
        s->data=i;
        r->next=s;
        r=s;
    }
    r->next=L;
}

int main(){
    cin>>n>>m;
    LinkList *L;
    ListInsert(L,n);
    DispList(L,m);

    return 0;
}