就是循环链表的应用 。
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct point{
int data;
int num;
struct point *next;
}LNode,*LinkList;
int n,m;
LinkList create(){
int i;
LinkList head,tail,news;
head = NULL;
printf("please input the n and m:");
scanf("%d%d",&n,&m);
for(i = 1;i<=n;i++){
news = (LinkList ) malloc (sizeof(LNode));
news->num = i;
if(head == NULL) {
head = news;tail = head;
}
else {
tail ->next = news;tail = news;
}
}
tail ->next = head;
return head;
}
void print(LinkList head){
printf("%d",head->num);
LinkList p = head->next;
while(p!=head){
printf("->%d",p->num);
p = p->next;
}
}
void search(LinkList head){
int count ,num;
LinkList pre,p;
num = 0;
count = 1;
p = head;
while(num < n){
do{
count++;pre=p;p=p->next;
}while(count<m);
pre->next = p->next;
printf("%3d",p->num);
free(p);
p = pre->next;
count = 1;
num ++;
}
}
int main(){
LinkList head;
head = create();
print(head);
search(head);
return 0;
}