| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 530 人关注过本帖
标题:单向链表理解问题
只看楼主 加入收藏
pokerLee
Rank: 2
等 级:论坛游民
帖 子:41
专家分:29
注 册:2012-11-4
结帖率:93.33%
收藏
已结贴  问题点数:20 回复次数:3 
单向链表理解问题
#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
heroinearth
Rank: 10Rank: 10Rank: 10
来 自:云南曲靖
等 级:青峰侠
帖 子:430
专家分:1506
注 册:2011-10-24
收藏
得分:0 
for循也用while()代替,
printf("\nDo you want to enter details of a horse(Y or N)?");
while((ch=getchar())!='N' || ch != 'n')
{
    ............
}
2013-10-28 10:29
rchunjiang
Rank: 4
来 自:大连
等 级:业余侠客
帖 子:42
专家分:262
注 册:2013-10-25
收藏
得分:20 
current=(*current).next;
就是将current指针指向当前元素的下一个元素,比如链表中现在有三个元素
horse1
horse2
horse3
指针current目前指向的是horse1,表明现在处理的是horse1,下面两条语句
previous=current;
current=(*current).next;
就是让previous指向horse1,current指向horse2,表明要显示horse2,以此类推。其实就是current指针沿着链表一个一个往下移。

[ 本帖最后由 rchunjiang 于 2013-10-28 11:13 编辑 ]
2013-10-28 11:11
快速回复:单向链表理解问题
数据加载中...
 
   



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

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