| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 336 人关注过本帖
标题:链表函数出错
只看楼主 加入收藏
gamelunppq
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-6-15
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
链表函数出错
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct worker)
struct worker
{
    long num,age;
    char name[8];
    char sex[2];
    struct worker *next;

};
int n;
struct worker *creat(void)  /*建立链表函数*/
{
    struct worker *head;
    struct worker *p1,*p2;
    n=0;
    p1=p2=(struct worker *)malloc(LEN);
    scanf("%ld %s %ld %s",&p1->num,&p1->name,&p1->age,&p1->sex);
    head=NULL;
    while(p1->num!=0)
    {
        n=n+1;
        if(n==1)
            head=p1;
        else
        {
            p2->next=p1;
            p2=p1;
            p1=(struct worker *)malloc(LEN);
            scanf("%ld %s %ld %s",&p1->num,&p1->name,&p1->age,&p1->sex);

    }
    p2->next=NULL;
    return(head);           

}
void print(struct worker *head) /*建立输出函数*/
{
    struct worker *p;
    p=head;
    printf("\nThese %d records are:\n",n);
    if(head!=NULL)        
        do{
            printf("%ld %s %ld %s",p->num,p->name,p->age,p->sex);
            p=p->next;
        }
        while(p!=NULL);
   
}
struct worker *del(struct worker *head,long num)   /*删除结点函数*/
{
    struct worker *p1,*p2;
    if(head=NULL)
    {
        printf("\nList is NULL\n");
        goto end;
    }
    p1=head;
    while(num!=p1->num&&p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if(num==p1->num)
    {
        if(p1==head)
            head=p1->next;
        else
            p2->next=p1->next;
        printf("\ndelete list item:%ld\n",num);
        n=n-1;
    }
    else
        printf("%ld not been found!\n",num);
end:
    return(head);
}
struct worker *insert(struct worker *head,struct worker *stud)/*插入结点函数*/
{
    struct worker *p0,*p1,*p2;
    p1=head;
    p0=stud;
    if(head==NULL)
    {
        head=p0;
        p0->next=NULL;
    }
    else
    {
        while((p0->num)>(p1->num)&&p1->next!=NULL)
        {
            p2=p1;
            p1=p1->next;
        }
        if((p0->num)<=p1->num)
        {
            if(p1==head)
                head=p0;
            else
            {
                p2->next=p0;
                p0->next=p1;
            }
        }
        else
        {
            p1->next=p0;
            p0->next=NULL;
        }

    }
    n=n+1;
    return(head);

}
void main()
{
    struct worker *head,*wor;
    long del_num;
    printf("Input records:\n");
    head=creat();
    print(head);
    printf("\ninput the deleted number:\n");
    scanf("%ld",&del_num);
    while(del_num!=0)                        /*删除函数*/
    {
        head=del(head,del_num);
        print(head);
        printf("\ninput the deleted number:\n");
        scanf("%ld",&del_num);
    }
    printf("\ninput the inserted record:\n ");
    wor=(struct worker *)malloc(LEN);
    scanf("%ld %s %ld %s",&wor->num,&wor->name,&wor->age,&wor->sex);
    while(wor->num!=0)
    {
        head=insert(head,wor);
        print(head);
        printf("\ninput the inserted record:\n ");
        scanf("%ld %s %ld %s",&wor->num,&wor->name,&wor->age,&wor->sex);
    }


}
错误是local function definitions are illegal
搜索更多相关主题的帖子: include 
2013-06-15 16:36
lzj12530
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:264
专家分:841
注 册:2013-3-28
收藏
得分:20 
把错误信息写全呢

C++菜鸟
2013-06-15 16:48
gamelunppq
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-6-15
收藏
得分:0 
D:\c语言file\大实验2\大实验2.cpp(40) : error C2601: 'print' : local function definitions are illegal
D:\c语言file\大实验2\大实验2.cpp(53) : error C2601: 'del' : local function definitions are illegal
D:\c语言file\大实验2\大实验2.cpp(81) : error C2601: 'insert' : local function definitions are illegal
D:\c语言file\大实验2\大实验2.cpp(119) : error C2601: 'main' : local function definitions are illegal
D:\c语言file\大实验2\大实验2.cpp(147) : fatal error C1004: unexpected end of file found
执行 cl.exe 时出错.
2013-06-15 16:50
lzj12530
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:264
专家分:841
注 册:2013-3-28
收藏
得分:0 
函数定义不合法

C++菜鸟
2013-06-15 21:30
lzj12530
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:264
专家分:841
注 册:2013-3-28
收藏
得分:0 
把你功能函数前面的struct 去掉

C++菜鸟
2013-06-15 21:31
快速回复:链表函数出错
数据加载中...
 
   



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

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