| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 311 人关注过本帖
标题:动态链表,帮忙看下哪错了?
只看楼主 加入收藏
klfy6666
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2010-4-23
结帖率:0
收藏
已结贴  问题点数:5 回复次数:3 
动态链表,帮忙看下哪错了?
#include<stdio.h>
#include<stdlib.h>
struct student
{
    int num;
    float score;
    struct student *next;
};
struct student *head,*p1,*p2;
void main()   
    {
   
    p1=(struct student *)malloc(sizeof(struct student));
    scanf("%d,%f",&p1->num,&p1->score);
    head=p1;
    p2=head;
    while(p1->num!=0)
    {        
        p1=(struct student *)malloc(sizeof(struct student));
        scanf("%d,%f",&p1->num,&p1->score);
        p2->next=p1;
        p2=p1;
        
    }
        p2->next=NULL;   
    while(p1!=NULL)
    {
    printf("%d,%f\n",p1->num,p1->score);
    p1=p1->next;
    }
}
2010-12-27 20:13
a343637412
Rank: 7Rank: 7Rank: 7
来 自:そ ら
等 级:黑侠
帖 子:357
专家分:620
注 册:2010-9-26
收藏
得分:2 
                                路过

占位置.....


给个测试用的例子吧....


/*
--------------------Configuration: aln - Win32 Debug--------------------
Compiling...
aln.cpp

aln.obj - 0 error(s), 0 warning(s)
*/

怎么帮你?
2010-12-27 20:19
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:2 
#include<stdio.h>
#include<stdlib.h>
struct student
{
    int num;
    float score;
    struct student *next;
};
struct student *head,*p1,*p2;
void main()   
    {
   
    p1=(struct student *)malloc(sizeof(struct student));
    scanf("%d,%f",&p1->num,&p1->score);
    head=p1;
    p2=head;
    while(p1->num!=0)
    {        
        p1=(struct student *)malloc(sizeof(struct student));
        scanf("%d,%f",&p1->num,&p1->score);
        p2->next=p1;
        p2=p1;
        
    }
        p2->next=NULL;  
    p1=head;   //将p指向第一个节点
    while(p1->next!=NULL)  //注意了,还有这个不能输出最后中一个的信息,好好想想吧
    {
    printf("%d,%f\n",p1->num,p1->score);
    p1=p1->next;
    }
    printf("%d,%f\n",p1->num,p1->score);  //输出最后一个,num为0的学生,不要的话可以去掉
}

[ 本帖最后由 qq1023569223 于 2010-12-27 21:27 编辑 ]

   唯实惟新 至诚致志
2010-12-27 21:25
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:2 
程序代码:
#include<stdio.h>
#include<stdlib.h>
struct student
{
    int num;
    float score;
    struct student *next;
};
//struct student *head,*p1,*p2;  //你可以把全局变量去掉
void main()   
    {
    struct student *head,*p1,*p2;
    head=(struct student *)malloc(sizeof(struct student));
    head->next=NULL;////没开辟一段内存要把节点的指针域进行初始化 赋值为空  防止野指针的出现
    p1=(struct student *)malloc(sizeof(struct student));
    p1->next=NULL;////没开辟一段内存要把节点的指针域进行初始化 赋值为空  防止野指针的出现
    while(scanf("%d,%f",&p1->num,&p1->score))//倒叙插入法  头结点的数据域要为空
    {
        if(0==p1->num)
            break;
        p1->next=head->next;
        head->next=p1;
        p1=(struct student *)malloc(sizeof(struct student));
        p1->next=NULL;////没开辟一段内存要把节点的指针域进行初始化 赋值为空  防止野指针的出现
    }
    p2=head->next;
    while(p2) 
    {
    printf("%d,%f\n",p2->num,p2->score);
    p2=p2->next;
    }
//     printf("%d,%f\n",p2->num,p2->score);  //输出最后一个,num为0的学生,不要的话可以去掉  那就不要了
}
这是标准的动态创建

                                         
===========深入<----------------->浅出============
2010-12-27 22:02
快速回复:动态链表,帮忙看下哪错了?
数据加载中...
 
   



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

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