| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 395 人关注过本帖
标题:帮忙看下链表,这个为什么运行不了?郁闷……
只看楼主 加入收藏
C语言学徒
Rank: 2
来 自:NamKing
等 级:论坛游民
帖 子:51
专家分:22
注 册:2009-2-27
结帖率:90%
收藏
已结贴  问题点数:10 回复次数:2 
帮忙看下链表,这个为什么运行不了?郁闷……

如题,是书上的例子,运行不动!




#include <malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{long num;
 float score;
struct student *next;
};
int n;


struct student *creat(void)  
{struct student *head;
 struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}


void print(struct student *head)
{struct student *p;
 printf("\nNow,These %d records are:\n",n);
 p=head;
if(head!=NULL)
do
{printf("%ld %5.1f",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
main()
{struct student *head,stu;
printf("\ninput records:");
head=creat();
print(head);
getch();
}
搜索更多相关主题的帖子: 运行 链表 
2009-10-04 20:22
广陵绝唱
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:29
帖 子:3607
专家分:1709
注 册:2008-2-15
收藏
得分:7 
帮你改了下,现在可以正常运行:
程序代码:
#include <stdio.h>
#include<stdlib.h>
//#define NULL 0
#define LEN sizeof(struct student)
struct student {
    long num;
    float score;
    struct student *next;
};
int n;
struct student *creat(void)
{
    struct student *head;
    struct student *p1, *p2;
    n = 0;
    p1 = p2 = (struct student *)malloc(LEN);
    scanf("%ld %f", &p1->num, &p1->score);
    head = NULL;
    while (p1->num != 0) {
        n = n + 1;
        if (n == 1)
            head = p1;
        else
            p2->next = p1;
        p2 = p1;
        p1 = (struct student *)malloc(LEN);
        scanf("%ld %f", &p1->num, &p1->score);
    }
    //p2->next = NULL;
    p1=NULL;
    return (head);
}
void print(struct student *head)
{
    struct student *p;
    printf("\nNow,These %d records are:\n", n);
    p = head;
    //if (head != NULL)
    /*do*/while(p) {
        printf("%ld %5.1f\n", p->num, p->score);
        p = p->next;
    } //while (p != NULL);
}
main()
{
    struct student *head, stu;
    printf("\ninput records:");
    head = creat();
    print(head);
    //getch();
}

2009-10-04 23:11
C语言学徒
Rank: 2
来 自:NamKing
等 级:论坛游民
帖 子:51
专家分:22
注 册:2009-2-27
收藏
得分:0 
还是不能运行啊?难道是我的软件的问题,郁闷&
2009-10-05 19:13
快速回复:帮忙看下链表,这个为什么运行不了?郁闷……
数据加载中...
 
   



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

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