链表—约瑟夫环

来源:互联网 发布:photoshop cc mac破解 编辑:程序博客网 时间:2024/05/22 03:38
#include <stdio.h>#include <stdlib.h>typedef struct  node{    int num;    struct node* next;}NODE,*PNODE;PNODE creat_(int n){    PNODE head,p,q;    int num_=1;    head=(PNODE)malloc(sizeof(NODE));    head->next=NULL;    q=head;    head->num=num_;    n--;    while(n--)    {        p=(PNODE)malloc(sizeof(NODE));        num_=num_+1;        p->num=num_;        q->next=p;        q=p;    }    q->next=head;    return head;}void adjust(PNODE head,int n,int m){    int i=1;    int t=0;    PNODE q,p;    q=head;    if(m==1)    {        while(n--)        {            printf("%d ",q->num);            q=q->next;        }        printf("\n");    }    else    {        while(t<n*m)        {            t++;            if(i==(m-1))            {                p=q->next;                q->next=p->next;                printf("%d",p->num);                printf(" ");                i=0;            }            else            {                q=q->next;                i++;            }        }        printf("\n");    }}int main(){    int i,p;    PNODE head;    while(scanf("%d",&i)!=EOF)    {    head=creat_(i);    scanf("%d",&p);    adjust(head,i,p);    }    return 0;}

0 0