#include"stdio.h"
#include"stdlib.h"
#include"malloc.h"
typedef struct Node
{
int data;
struct Node * pNext;
}NODE,* PNODE;
PNODE create_list(void)
{
int i;
int len;
printf("请输入总人数:");
scanf("%d",&len);
PNODE pHead=(PNODE)malloc(sizeof(NODE));
if(pHead==NULL)
{
printf("动态分配失败!");
exit(-1);
}
pHead->pNext=pHead;
PNODE pTial=pHead;
for(i=1;i<=len;i++)
{
PNODE pNew=(PNODE)malloc(sizeof(NODE));
if(pNew==NULL)
{
printf("动态分配失败!");
exit(-1);
}
pNew->data=i;
pNew->pNext=pTial->pNext;
pTial->pNext=pNew;
pTial=pNew;
}
return pHead;
}
void treavese_list(PNODE pHead)
{
PNODE p;
p=pHead->pNext;
while(p!=pHead)
{
printf("%d ",p->data);
p=p->pNext;
}
printf("\n");
return;
}
void search_list(int n,int s,int m)
{
int i,j,k;
PNODE q,r,Head;
if(n<s)
{
printf("查找失败!\n");
return;
}
Head=create_list();
treavese_list(Head);
printf("\n");
printf("\n");
q=Head;
for(k=1;k<s;k++)
{
q=q->pNext;
}
for(i=1;i<n;i++)
{
for(j=1;j<m;j++)
{
q=q->pNext;
if(q==Head)
{
q=q->pNext;
}
}
if(q->pNext==Head)
{
q=Head;
}
r=q->pNext;
printf("%d\n",r->data);
q->pNext=r->pNext;
free(r);
}
printf("%d\n",Head->pNext->data);
}
int main()
{
int n,s,m;
PNODE r;
printf("Intput n, s and m:");
scanf("%d%d%d",&n,&s,&m);
search_list(n,s,m);
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。