求解约瑟夫环用循环单链表的代码问题

下面是我的代码,但是运行不出结果,不知道问题在哪,有无大神指教????
# include<malloc.h>
# include<stdio.h>
struct node
{
int id;
int pw;
node *next;
};
int main()
{
int m,n,i,j,h;
node *p,*q,*f;
printf("Enter the number n:\n");
scanf("%d",&n);
printf("Enter the m:\n");
scanf("%d",&m);
for(j=1;j<=n;j++){
if(j==1)
{
f=p=(node*)malloc(sizeof(node));
if(p==0){
return 0;
}
}
else
{
q=(node*)malloc(sizeof(node));
if(q==0){
return 0;
p->next=q;
p=q;
}
}
printf("Enter the h:\n");
scanf("%d",&h);
p->id=j;
p->pw=h;
}
p->next=f;
p=f;
printf("删除顺序为:\n");
for(i=1;i<=n;i++){
for(j=1;j<m;j++,p=p->next);
m=p->pw;
printf("p->id=%d\n",p->id);
p->id=p->next->id;
p->pw=p->next->pw;
p->next=q;
p->next=q->next;
free(q);
}
return 0;
}

# include<malloc.h>
# include<stdio.h>
struct node
{
int id;
int pw;
struct node *next;
};
int main()
{
int m,n,i,j,h;
struct node *p,*q,*f;
printf("Enter the number n:\n");
scanf("%d",&n);
printf("Enter the m:\n");
scanf("%d",&m);
for(j=1;j<=n;j++){
if(j==1)
{
f=p=(struct node*)malloc(sizeof(struct node));
if(p==0){
return 0;
}
}
else
{
q=(struct node*)malloc(sizeof(struct node));
if(q==0) //这里有个括号不对
return 0;
p->next=q;
p=q;
//这里有个括号不对
}

printf("Enter the h:\n");
scanf("%d",&h);
p->id=j;
p->pw=h;

}

p->next=f;
p=f;

printf("删除顺序为:\n");

for(i=1;i<=n;i++){
for(j=1;j<m;j++,p=p->next);
m=p->pw; // 这个我认为你是在改变下一次的m
printf("p->id=%d\n",p->next->id); // 我们遍历到我们要删的上一个节点,所以要删的节点是p->next,要删的id是p->next->id;

// p->id=p->next->id; // 这个不知道要做什么
// p->pw=p->next->pw; // 这个不知道要做什么
// p->next=q; // 这一句肯定是错的,你可能写错了

q=p->next; // 这个我添加的语句
p->next=q->next;

free(q);
}
getchar();
getchar();
return 0;
}
温馨提示:答案为网友推荐,仅供参考
相似回答