标题:为什么会出现这样啊,帮我调试下,谢谢
只看楼主
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
结帖率:83.33%
已结贴  问题点数:20 回复次数:1 
为什么会出现这样啊,帮我调试下,谢谢
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
typedef struct student
{
    int num;
    int age;
    struct student *link;
}stud;

stud* creat(int n)
{
    stud *h,*p;
    int i;
    h=(stud *)malloc(sizeof(stud));
    if(h==NULL)
    {
        printf("not enough memory!");
        return(0);
    }
    h=NULL;
    for(i=0;i<=n;i++)
    {
        
        
        if((p=(struct student *)malloc(sizeof(stud)))==NULL)
            
            
            
        {
            printf("内存不足!");
            return(0);
        }
        printf("请输入数字!");
        scanf("%d",&p->num);
        scanf("%d",&p->age);
        p->link=h;
        h=p;
        
    }
    return(h);
}
  stud* insert(stud *h)
  {
      stud *p,*q,*d;
      p=h;
      int n;
      q=h->link;
      d=(stud *)malloc(sizeof(stud));
      if(d==NULL)
      {
          printf("内存不足!");
      }
      printf("请输入插入位置的前一个数!");
      scanf("%d",&n);
      

      if(n==p->num||n==p->age)
      {
      printf("请输入要插入的数 !");
      scanf("%d",&d->num);
      scanf("%d",&d->age);
      d=h;
      p=d->link;
      }
      else if
      
          (n==q->num||q->age)
      {
       printf("请输入要插入的数 !");
      scanf("%d",&d->num);
      scanf("%d",&d->age);
      d->link=p->link;
      p->link=d;
      }
      else if(n!=p->num&&n!=q->num)
      {
         while(q->num!=NULL&&p->num!=NULL)
         {
              
              
              p=p->link;
              q=q->link;
              if(n==q->num)
             {
              printf("请输入要插入的数字!");
              scanf("%d",&d->num);
              scanf("%d",&d->age);
              d->link=p->link;
              p->link=d;
               }
             break;
               
                        
         }
      }
     return(h);
  }  
  void search(stud *h)
  {
    stud *p;
    int m;
    printf("请输入需查找数据!");
    scanf("%d",&m);
    p=h;
    while(p!=NULL)
    {
   
        if(m==p->num||m==p->age)
        {printf("%d %d\n",p->num,p->age);
        
         break;
        }
        else
         p=p->link;
        
    }
         if(p==NULL)
            printf("无此数!");
   
   
   
  }
  stud *delet(stud *h)
  {
      stud *q,*p;
      p=h;
      int n;
      q=h->link;
      printf("请输入要删除的数字!");
      scanf("%d",&n);
      if(n==p->num||n==p->age)
      h=p->link;
      else if(n==q->num||n==q->age)
          p->link=q->link;
      else if(n!=p->num&&n!=q->num)
         while(q->link!=NULL&&p->link!=NULL)
         {
              
              
              p=p->link;
              q=q->link;        
             if(n==q->num||n==q->age)
              p->link=q->link;
         
         }
         
         
      return(h);
  }
void print(stud *h)
{
     stud *p;
     p=h;
     while(p!=NULL)
     {
         printf("%3d %d\n",p->num,p->age);
         p=p->link;
     }
}
void save(stud *h) //保存函数//
{
    stud *p;
    FILE *fp;
     p=h;//头指针赋给p//
    fp=fopen("d:\\wj123","a");
    if(fp==NULL)
    {
        printf("memeory error!");
    }
   
    while(p!=NULL)
    {
        fwrite(p,sizeof(stud),1,fp);
        p=p->link;
   
    fclose(fp);
    }
    printf("save successful !\n");
}
stud *load()
{
    stud *p,*h;
    FILE *fp;
    fp=fopen("d:\\j123","rb");//打开一个文件//
    if(fp==NULL)//检测文件能否打开//
    {
        printf("can not open file!");
        return(0);
    }
    p=(stud *)malloc(sizeof(stud));//申请空间。。。//

    if(!p)
    {
        printf("内存溢出!");
        return(h);
    }
    h=NULL;
    while(!feof(fp))
    {
        if(1!=fread(p,sizeof(stud),1,fp))//若没读到数据,跳出循环//
            break;
        else p->link=(stud *)malloc(sizeof(stud));//为下一个结点申请空间//
        if(p->link==NULL)
        {
            printf("内存溢出!");
            return(h);
        }
         p->link=h;//建立链表//
         h=p;
    }
    fclose(fp);
    printf("loading successul !\n");
    return(h);
}
void main()
{
    struct student *h;
   
   
    load();
    print(h);
    h=creat(1);
   
      save(h);
   
   
   
   
    insert(h);
    print(h);
     save(h);
   
    print(h);
    search(h);
   
      
}



1XH04ZNNJUUSJ2IEK0PK9209HICBS568[1].jpg (1.98 KB)
搜索更多相关主题的帖子: include memory return 
2011-10-15 15:26
ileelsai
Rank: 5Rank: 5
等 级:职业侠客
帖 子:87
专家分:303
注 册:2011-9-25
得分:20 
stud *h,*p;
    int i;
    h=(stud *)malloc(sizeof(stud));
    if(h==NULL)
    {
        printf("not enough memory!");
        return(0);
    }
    h=NULL;
有什么意义。
如果你是想建头结点。那么h->link=NULL,后面p->link=h->link;h->link=p;
建议你多看看别人现成的代码
2011-10-15 18:31



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




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

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