| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1271 人关注过本帖
标题:大神,帮我看看我的代码问题出在哪。有n个人围成一圈,从第1个人开始报数1、 ...
只看楼主 加入收藏
涧边幽草
Rank: 2
等 级:论坛游民
帖 子:37
专家分:15
注 册:2015-11-4
结帖率:88.89%
收藏
已结贴  问题点数:20 回复次数:1 
大神,帮我看看我的代码问题出在哪。有n个人围成一圈,从第1个人开始报数1、2、3,每报到3的人退出圈子。编程使用链表找出最后留下的人。
大神,帮我看看我的代码问题出在哪。
有n个人围成一圈,从第1个人开始报数1、2、3,每报到3的人退出圈子。编程使用链表找出最后留下的人。
我输入一个数,但总没有东西输出。
#include "stdio.h"
#include "stdlib.h"
struct person {
    int num;
    struct person *next;
};
struct person *creat(int n)//P>=1
{
     struct person *head=NULL,*p1=NULL,*p2=NULL;
     int i;
     for(i=1;i<=n;i++)
     {  p1=(struct person *)malloc(LEN);
        p1->num=i;
        p1->next=NULL;
        if(i==1) head=p1;
        else p2->next=p1;
        p2=p1;
      }
      return(head);
}
struct person *del(struct person *head)
{
    struct person *p1=head,*p2=head,*rm,*s=head;
    int k=1;
    while(p1->next!=p1)
    {
        while(s->next!=NULL)//让链表首尾相连
        {
            s=s->next;
        }
        s->next=head;
        if(k==3)
        {
            rm=p1;
            p1=p1->next;
            p2->next=p1;
            free(rm);
            rm=NULL;
            k=0;
        } else {
            p2=p1;
            p1=p1->next;
        }
        k++;
    }
    p1->next=NULL;
    return(p1);
}
int main()
{
    struct person *pt,*p;
    int n;
    scanf("%d",&n);
    pt=creat(n);
    p=del(pt);
    printf("%d",p->num);
    return 0;
}
搜索更多相关主题的帖子: include person 
2015-12-04 23:56
ntprc0x
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:23
专家分:130
注 册:2015-11-14
收藏
得分:20 



while(p1->next!=p1)
    {
        while(s->next!=NULL)//让链表首尾相连
        {                                          
            s=s->next;
        }
        s->next=head;

     .....
    }
这个while第一次能找到NULL 链表首尾相连后就没有NULL了 死循环了 所以没有东西输出
把它移到第一个while的外面 或者你在创建链表的时候就让它首尾相连 不是更省事
2015-12-06 02:32
快速回复:大神,帮我看看我的代码问题出在哪。有n个人围成一圈,从第1个人开始报 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014678 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved