| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 325 人关注过本帖
标题:【求助】请教一个简单的问题
只看楼主 加入收藏
司徒瑾贤
Rank: 2
等 级:论坛游民
帖 子:25
专家分:44
注 册:2010-3-18
结帖率:71.43%
收藏
已结贴  问题点数:20 回复次数:2 
【求助】请教一个简单的问题
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>

int main(void)
{
    struct horse
    {
        int age;
        int height;
        char name[20];
        char father[20];
        char mother[20];

        struct horse *next;
    };
   
    struct horse *first=NULL;
    struct horse *current=NULL;
    struct horse *previous=NULL;

    char test='\0';
    for(;;)
    {
        printf("Do you want to enter details of  a%s horse (Y or N)?",first!=NULL?"nother":" " );
        scanf("%c",&test);
        if(tolower(test)=='n') break;
        current=(struct horse *)malloc(sizeof(struct horse));
        if(first==NULL)
            first=current;
        if(previous!=NULL)
            previous->next=current;

        printf("\nEnter the name of the horse:");
        scanf("%s",current->name);

        printf("\nHow old is %s ? ",current->name );
        scanf("%d",&current->age );

        printf("\nHow high is %s (in hand)?",current->name);
        scanf("%d",&current->height);

        printf("\nWho is %s's father?",current->name);
        scanf("%s",current->father);

        printf("\n Who is %s's mother ? ",current->name);
        scanf("%s",current->mother);

        current->next=NULL;
        previous=current;
    }
  current=first;
  while(current!=NULL)
  {
    printf("\n\n%s is %d years old ,%d hands height.",current->name,current->age,current->height);
    printf("and has %s and %s as parents.",current->father,current->mother);

    previous=current;
    current=current->next;
    free(previous);
  }

    return 0;
}

为什么在输入第二只 horse 的 detail 时会出问题?导致链表输入无法结束。

[ 本帖最后由 司徒瑾贤 于 2010-9-14 18:03 编辑 ]
2010-09-14 13:12
清风拂晓
Rank: 8Rank: 8
来 自:火星
等 级:蝙蝠侠
威 望:1
帖 子:356
专家分:889
注 册:2010-8-13
收藏
得分:20 
第一只输入的时候没问题?

清风拂暮(木)
2010-09-14 14:15
司徒瑾贤
Rank: 2
等 级:论坛游民
帖 子:25
专家分:44
注 册:2010-3-18
收藏
得分:0 
回复 2楼 清风拂晓
已解决  是这样的:
程序代码:
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>

int main(void)
{
    struct horse
    {
        int age;
        int height;
        char name[20];
        char father[20];
        char mother[20];

        struct horse *next;
    };
   
    struct horse *first=NULL;
    struct horse *current=NULL;
    struct horse *previous=NULL;

    char test='\0';
    for(;;)
    {
        printf("\nDo you want to enter details of  a%s horse (Y or N)?",first!=NULL?"nother":" " );
        scanf("%c",&test);
        if(tolower(test)=='n') break;
                if(tolower(test)=='y')
               { 
        current=(struct horse *)malloc(sizeof(struct horse));
        if(first==NULL)
            first=current;
        if(previous!=NULL)
            previous->next=current;

        printf("\nEnter the name of the horse:");
        scanf("%s",current->name);

        printf("\nHow old is %s ? ",current->name );
        scanf("%d",&current->age );

        printf("\nHow high is %s (in hand)?",current->name);
        scanf("%d",&current->height);

        printf("\nWho is %s's father?",current->name);
        scanf("%s",current->father);

        printf("\nWho is %s's mother?",current->name);
        scanf("%s",current->mother);

        current->next=NULL;
        previous=current;
                fflush(stdin);
               }
    }
  current=first;
  while(current!=NULL)
  {
    printf("\n\n%s is %d years old ,%d hands height.",current->name,current->age,current->height);
    printf("and has %s and %s as parents.\n",current->father,current->mother);

    previous=current;
    current=current->next;
    free(previous);
  }
        system("pause");
        return 0;
}
这样的
2010-09-14 18:06
快速回复:【求助】请教一个简单的问题
数据加载中...
 
   



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

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