| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 489 人关注过本帖
标题:链表的问题
只看楼主 加入收藏
HSU
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2010-1-3
结帖率:83.33%
收藏
已结贴  问题点数:10 回复次数:2 
链表的问题
# include <stdio.h>
# include <malloc.h>
# define LEN sizeof(struct train)
# define NULL 0

struct train  
{
    int num;
    struct train *next;
}
void print (struct train *head);
void count (struct train *head);
int main()
{
    int n=0;
    struct train *head,*p1,*p2;
    p1=p2=(struct train *) malloc(LEN);
    head=NULL;
    scanf("%d",&p1->num);
    while (p1->num !=0)
    {
        n++;
        if (n==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=(struct train *) malloc(LEN);
        scanf("%d",&p1->num);
    }
    p2->next=NULL;
    print(head);
    count(head);
    return 0;
}

void print (struct train *head)
{
    struct train *tmp;
    tmp=head;
    if (head !=NULL)
        do
        {printf("%d ",tmp->num);
         tmp=tmp->next;
        }while (tmp !=NULL);
        printf("\n");
}

void count (struct train *head)
{
    int count=0;
    struct train *tmp;
    tmp=head;
    if (head !=NULL)
        do
        {count++;
        }while (tmp->next !=NULL);
    printf("%d\n",count);
}


编译过程提示错误:
error C2628: “train”后面接“void”是非法的(是否忘记了“;”?)
error C2556: “void print(train *)”: 重载函数与“train print(train *)”只是在返回类型上不同
error C2371: “print”: 重定义;不同的基类型
这是什么错误?
搜索更多相关主题的帖子: 链表 
2010-03-27 17:27
ldg628
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:3
帖 子:526
专家分:3036
注 册:2009-6-23
收藏
得分:10 
struct train  
{
    int num;
    struct train *next;
}; //记得要加一个分号
2010-03-27 17:35
HSU
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2010-1-3
收藏
得分:0 
的确是少了“;”
后来发现第二个函数也有问题,正确的应该是
# include <stdio.h>
# include <malloc.h>
# define LEN sizeof(struct train)
# define NULL 0

struct train  
{
    int num;
    struct train *next;
};

void print (struct train *head);
void count (struct train *head);

int main()
{
    int n=0;
    struct train *head,*p1,*p2;
    p1=p2=(struct train *) malloc(LEN);
    head=NULL;
    scanf("%d",&p1->num);
    while (p1->num !=0)
    {
        n++;
        if (n==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=(struct train *) malloc(LEN);
        scanf("%d",&p1->num);
    }
    p2->next=NULL;
    print(head);
    count(head);
    return 0;
}

void print (struct train *head)
{
    struct train *tmp;
    tmp=head;
    if (head !=NULL)
        do
        {printf("%d ",tmp->num);
         tmp=tmp->next;
        }while (tmp !=NULL);
        printf("\n");
}

void count (struct train *head)
{
    int count=0;
    struct train *tmp;
    tmp=head;
    if (head !=NULL)
      do
      {count++;
      tmp=tmp->next;
      }while (tmp!=NULL);
    printf("%d\n",count);
}
2010-03-27 17:57
快速回复:链表的问题
数据加载中...
 
   



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

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