#include <stdio.h> #include <stdlib.h> typedef struct node { int name; int number; struct node *next; }person; //初始化循环列表 person * initLink(int n) { person *head = (person *)malloc(sizeof(person)); head->number = 1; head->next = NULL; person *cycle = head; for(int i=2;i<=n;i++) { person *body = (person *)malloc(sizeof(person)); body->number = i; body->next = NULL; cycle->next = body; cycle = cycle->next; } cycle->next = head;//收尾相连 return head; } //打印循环列表数据 void printf_list(person *s) { person *p = s->next; while(p->number !=1) { p = p->next; } printf("-> [name:%d,number:%d]\r\n",p->name,p->number); p = p->next; while(p!= s) { printf("-> [name:%d,number:%d]\r\n",p->name,p->number); p = p->next; } } //间隔k个,总数m void write_list(person *s ,int k,int m) { person *p = s; for(int i=1;i<=m;i++) { for(int j=0;j<k;j++) { while(p->name != 0) { p = p->next; } p=p->next; } while(p->name != 0) { p = p->next; } p->name = i; } } int main(int argc ,char* argv) { //初始化 10 个元素的列表 person *head = initLink(10); //间隔1张,总数为10 write_list(head,1,10); //输出顺序 printf_list(head); return 0; }
原文:https://www.cnblogs.com/doitjust/p/13337061.html