| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 503 人关注过本帖
标题:有个问题请教高手:
只看楼主 加入收藏
zhangyafei4353
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2008-11-28
收藏
 问题点数:0 回复次数:1 
有个问题请教高手:
谁有空,帮我看一下:
    运行:  case '1':
            h=creat(i);break;
h=creat(i);调用返回出错;谢了
出错图看附加压缩文件。。。。
、、、、、、、、、、代码如下
#include "hh.h"
#include "creat.h"
#include "find.h"
#include "display.h"
#include "del.h"
#include "order.h"

int display(int i,Infor *head);
Infor *order(int i,Infor *head) ;
Infor *search(int x,Infor*head);
Infor *del(int i,Infor *head);
Infor *creat(int i);

char choice;
Infor *h;
static int i=0;

int main()
{
    do
    {
        system("cls");
        printf("\n※※※※※※※※※※※※※※※※※   Welcome!  ※※※※※※※※※※※※※※※※\n\n\n");
        printf("\t\t1.  Add the information.\n"
               "\t\t2.  Display the information.\n"
               "\t\t3.  Search the althlete's/horse's information.\n"
               "\t\t4.  The order to playact.\n"
               "\t\t5.  Delete the information.\n"
               "\t\t6.  Exit.\n\n\n");
        fflush(stdin);
        printf("\t●Please input your choice:    ");
        scanf("%c",&choice);
        switch(choice)
        {
            case '1':
            h=creat(i);break;
            case '2':
            display(i,h);break;
            case '3':
            h=search_main(i,h);break;
            case '4':
            h=del(i,h);break;
            case '5':
            h=del(i,h);break;
            case '6':
            printf("\n\n※※※※※※※※※※※※※※※※※  Goodbye!  ※※※※※※※※※※※※※※※※※\n"); break;
        }
        getchar(); //暂停一次

        while(h)
        {
            free(h);
            h=h->next;
        }//将内存释放
    }while(choice!='6');/*菜单结束选择条件*/
    return 0;
}
creat.h文件:
#ifndef CREAT_H_INCLUDED
#define CREAT_H_INCLUDED

#include "hh.h"

Infor *creat(int i)
{
    char a;
    int len_1,len_2,d,e;
    Infor*pr,*r,*head;//建立头head,尾r,pr为移动节点;
    head=(Infor *)malloc(sizeof(Infor));
    head->next=NULL;
    r=head;

    do
    {
        pr=(Infor *)malloc(sizeof(Infor));//申请新节点内存
        if(pr==NULL)
        {
            printf("\n\n\t◆No memory!");
            return(NULL);
        }

        printf("\n\t★There has been %d massages!\n\n",i);
        printf("\t●Please input the %dth athletes' information here.\n",i+1);
        do
        {
            fflush(stdin);
            printf("\n\tThe serial number includes 6 numbers.\n"
                   "\tAnd if inputing error, it will get back!!!\t\n");
            printf("\t●Input the serial number of athlete :  ");
            scanf("%d",&(pr->id));
        }while(pr->id>999999||pr->id<100000) ;//限制为六位数

        fflush(stdin);
        printf("\n\t●Please input the name:  ");
        gets(pr->name);
        len_1=strlen(pr->name);
        for(d=0;d<len_1;d++)
        {
            if(pr->name[d]<65||pr->name[d]>122||(pr->name[d]>90&&pr->name[d]<97))//限制为字母输入
            {
                printf("\n\t◆Input error! Number is not allowded in the name!\n\tPlease input again.");
                printf("\n\t●Please input the name:  ");
                gets(pr->name);
                len_1=strlen(pr->name);
                d=0;
            }
        }

        do
        {
            printf("\n\tAlthelete's age is between 14-90"
                    "\n\tIf imput error,it will get back!!!");
            fflush(stdin);
            printf("\n\t●Age:  ");
            scanf("%d",&(pr->age));
        }while(pr->age<14||pr->age>90);  //控制年龄

        fflush(stdin);
        printf("\n\t●Country:  ");
        gets(pr->country);
        len_2=strlen(pr->country);
        for(e=0;e<len_2;e++)
        {
            if(pr->country[e]<65||pr->country[e]>122||(pr->country[e]>90&&pr->country[e]<97))
            {
                printf("\n\t◆Input error! Number is not allowded in the country!\n\tPlease input again.");
                printf("\n\t●Please input the country:  ");
                gets(pr->country);
                len_2=strlen(pr->country);
            }
        }

        do
        {
            fflush(stdin);
            printf("\n\t●The serial number of horse includes 6 numbers."
                   "\n\tAnd if inputing error, it will get back!!!)\t\n");
            printf("\n\t●The horse's serial number:  ");
            scanf("%d",&(pr->horse_number));
        }while((pr->horse_number)>999999||pr->horse_number<100000);

        do
        {
            fflush(stdin);
            printf("\n\n\tThe horse's age is between 2-40"
                    "\n\tIf imput error,it will get back!!!");
            printf("\n\t●The horse's age:  ");
            scanf("%d",&(pr->horse_age));
        }while(pr->horse_age<2||pr->horse_age>40);
        pr->next=NULL;
        r->next=pr;
        r=r->next;//r指向下一个节点位置
        i++;//计数器

        printf("\n\t●Do you want to input input the information of another one?(Y\\N) ");
        fflush(stdin);
        a=getchar();
    }while(a=='y'||a=='Y'&&i<N);//限制数据条数在100条以内,可以通过改变宏来调节

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

#endif // CREAT_H_INCLUDED

未命名.jpg (23.76 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: del display include 压缩文件 search 
2008-12-03 19:11
debroa723
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:23
帖 子:862
专家分:1954
注 册:2008-10-12
收藏
得分:0 
while(h)
{
    free(h);
    h=h->next;
}//将内存释放
这里有错误,h被释放后,h->将是非法操作.正确释放应该是倒过来,从最后一个开始释放(可以用递归的方法来实现)
或者先记录下一个,再释放前一个.
Infor* tempP = NULL;
while(h)
{
    tempP=h->next;
    free(h);
    h=tempP;
}

2:我调试的h=creat(i)返回是正常的,不过有个问题,你是从第二个结点开始记录数据的,第一个结点没有记录数据,可能是做为头指针吧.
2008-12-03 22:25
快速回复:有个问题请教高手:
数据加载中...
 
   



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

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