//主函数 int main() { cLinkList *head, *p, *s, *temp; int n, k; int i = 1; printf("Please enter the total number n:\n"); scanf("%d", &n); printf("Please enter the key value:\n"); scanf("%d", &k); k %= n; head = (cLinkList *)malloc(sizeof(cLinkList)); p = head; p->next = p;//这里要赋值为p,不能赋值为head,要保持head的位置不变 p->data = i; for(i = 2; i <= n; i++) { s = (cLinkList *)malloc(sizeof(cLinkList)); s->data = i; s->next = p->next; p->next = s; p = s; }
p = head; int total = n; while(n--) { for(i = 1; i < k - 1; i++) { p = p->next; } printf("%d->", p->next->data); temp = p->next;//temp为要删除的元素 p->next = temp->next;//链表中跳过temp free(temp);//释放temp p = p->next;//p向前移动继续寻找 } printf("Done!\n"); return 0; }