注册 登录
编程论坛 数据结构与算法

求解!!!链表,究竟是哪里出错

hd小狼崽 发布于 2012-12-07 16:15, 584 次点击
#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
8 回复
#2
yuccn2012-12-07 18:26
没有main函数

写个main函数就行了

void main()
{
 // ……
}
#3
wu_jian_long2012-12-07 23:18
#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;
  
      }   

}
#4
wu_jian_long2012-12-07 23:20
这个程序暂时有点问题。明天在调试一下。今天有点晚了。明天还要工作。
#5
hd小狼崽2012-12-08 13:48
回复 2楼 yuccn
大神啊!!!!一眼就看出问题所在。佩服。。。
#6
hd小狼崽2012-12-08 13:50
回复 3楼 wu_jian_long
不错不错,你写的代码让我发现我的代码的不少问题
#7
hd小狼崽2012-12-08 13:52
第一次感觉到论坛的收益匪浅啊,,,,决定以后要多逛论坛了!!!!!
#8
小强。小强2012-12-08 15:52
程序代码:
#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;

 }
除主函数外主函数,还有要告诉人家输入啥东东(也可以不写)
  错了见谅啊!
#9
wu_jian_long2012-12-08 21:20
#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;
      }   

}
只有本站会员才能查看附件,请 登录



使用的编译器是vc2010

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