| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 530 人关注过本帖
标题:单向链表理解问题
取消只看楼主 加入收藏
pokerLee
Rank: 2
等 级:论坛游民
帖 子:41
专家分:29
注 册:2012-11-4
结帖率:93.33%
收藏
已结贴  问题点数:20 回复次数:1 
单向链表理解问题
#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;
        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;
    }
    current=first;
    while(current!=NULL)
    {
        printf("\n\n%s is %d years old,%d hands high,",
               (*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;
}

最后显示循环的while()循环中,(*current).next赋给current如何理解?
搜索更多相关主题的帖子: previous current details include father 
2013-10-26 11:52
pokerLee
Rank: 2
等 级:论坛游民
帖 子:41
专家分:29
注 册:2012-11-4
收藏
得分:0 
可以理解为列表的形成由for()循环生成~~然后while()循环指定么~~求指点~~
2013-10-26 21:21
快速回复:单向链表理解问题
数据加载中...
 
   



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

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