标题:求解!!!链表,究竟是哪里出错
只看楼主
hd小狼崽
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-12-7
结帖率:100%
已结贴  问题点数:20 回复次数:8 
求解!!!链表,究竟是哪里出错
#include<stdio.h>
#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);
}

出现的错误提示:--------------------Configuration: 6.c - Win32 Debug--------------------
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/6.c.exe : fatal error LNK1120: 1 unresolved externals
搜索更多相关主题的帖子: head next include 
2012-12-07 16:15
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6809
专家分:42393
注 册:2010-12-16
得分:5 
没有main函数

写个main函数就行了

void main()
{
 // ……
}

我行我乐
我的博客:
http://blog.yuccn. net
2012-12-07 18:26
wu_jian_long
Rank: 2
等 级:论坛游民
帖 子:4
专家分:15
注 册:2012-12-7
得分:15 
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)

struct student *creat(void);
void TraverseLink(struct student * );

struct student
{
    long num;
    float score;
    struct student *next;
};



int main(void)
{
   struct student * Link;
    Link = creat();
    TraverseLink(Link);

    return 0;
}



struct student *creat(void)
{

    int n;
    struct student *head;
    struct student *p1,*p2;
    n=0;
    p1=p2=(struct student *)malloc(LEN);
    printf("请输入num和score的值,中间用逗号隔开,当输入num中为0时结束\n");
    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);
        printf("请输入num和score的值,中间用逗号隔开,当输入num中为0时结束\n");
        scanf("%ld,%f",p1->num,p1->score);
    }
    p2->next=NULL;
    return(head);
}

void TraverseLink(struct student * pLink)
{
      struct student * pN=pLink;
      while( NULL != pN->next)
      {
           printf("%ld,%f\n",pN->num,pN->score);
            pN = pN->next;
  
      }   

}
2012-12-07 23:18
wu_jian_long
Rank: 2
等 级:论坛游民
帖 子:4
专家分:15
注 册:2012-12-7
得分:0 
这个程序暂时有点问题。明天在调试一下。今天有点晚了。明天还要工作。
2012-12-07 23:20
hd小狼崽
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-12-7
得分:0 
回复 2楼 yuccn
大神啊!!!!一眼就看出问题所在。佩服。。。
2012-12-08 13:48
hd小狼崽
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-12-7
得分:0 
回复 3楼 wu_jian_long
不错不错,你写的代码让我发现我的代码的不少问题
2012-12-08 13:50
hd小狼崽
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-12-7
得分:0 
第一次感觉到论坛的收益匪浅啊,,,,决定以后要多逛论坛了!!!!!
2012-12-08 13:52
小强。小强
Rank: 2
来 自:山西大同
等 级:论坛游民
帖 子:42
专家分:56
注 册:2012-11-15
得分:0 
程序代码:
#include<stdio.h>
#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);
    }


 int main()

 {
     printf("please enter num,score;\n");
      struct student * p;
      p=creat();
      printf("num is%d, score is %f\n",p->num ,p->score );
      return 0;

 }
除主函数外主函数,还有要告诉人家输入啥东东(也可以不写)
  错了见谅啊!
2012-12-08 15:52
wu_jian_long
Rank: 2
等 级:论坛游民
帖 子:4
专家分:15
注 册:2012-12-7
得分:0 
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)

struct student *creat(void);
void TraverseLink(struct student * );

struct student
{
    long num;
    float score;
    struct student *next;
};



int main(void)
{
   struct student * Link;
    Link = creat();
    TraverseLink(Link);

    return 0;
}



struct student *creat(void)
{

    int n=0;
    struct student *head;
    struct student *p1,*p2;
     head=NULL;
    p1=(struct student *)malloc(LEN);
    printf("请输入num和score的值,中间用逗号隔开,当输入num中为0时结束\n");
    scanf("%ld,%f",&p1->num,&p1->score);
    while(p1->num)
    {
        n++;
        if(n==1)  head=p1;
        else p2->next=p1;
        p2=p1;
        p1=(struct student *)malloc(LEN);
        printf("请输入num和score的值,中间用逗号隔开,当输入num中为0时结束\n");
        scanf("%ld,%f",&p1->num,&p1->score);
    }
    p2->next=NULL;
    return(head);
}

void TraverseLink(struct student * pLink)
{
      struct student * pN=pLink;
      while( NULL != pN->next)
      {
           printf("%ld,%f\n",pN->num,pN->score);
           pN = pN->next;
      }   

}
test.rar (570.58 KB)



使用的编译器是vc2010

[ 本帖最后由 wu_jian_long 于 2012-12-8 21:23 编辑 ]
2012-12-08 21:20



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-390043-1-1.html




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

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