| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 779 人关注过本帖, 1 人收藏
标题:此程序插入函数有问题,在插入多组数据后,不能插入到正确位置,求解
只看楼主 加入收藏
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
结帖率:83.33%
收藏(1)
已结贴  问题点数:20 回复次数:6 
此程序插入函数有问题,在插入多组数据后,不能插入到正确位置,求解
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
typedef struct student
{
    int num;
    int age;
    char pc[20];
    char name[20];
    struct student *link;
}stud;

stud* creat(int n)
{
    stud *h,*p,*q;
    int i,main();
    stud *load3();//文件检测函数定义//
    load3();
    h=(stud *)malloc(sizeof(stud));
    if(h==NULL)
    {
        printf("not enough memory!");
        return(0);
    }
     printf("学号\t年龄\t姓名\t电脑型号");
    h=NULL;
    for(i=0;i<=n;i++)
    {
        
      
        if((p=(struct student *)malloc(sizeof(stud)))==NULL)
            
            
            
        {
            printf("内存不足!");
            return(0);
        }
        printf("\n");
        scanf("%d",&p->num);

        
        scanf("%d",&p->age);
        scanf("%s",&p->pc);
        scanf("%s",&p->name);
        p->link=h;
        h=p;
      
    }
   return(h);
   

     
      
 }


  stud* insert(stud *h)
  {
      stud *p,*q,*d,*i;
      
      
      p=h;
      int n;
      q=h->link;
      d=(stud *)malloc(sizeof(stud));
      if(d==NULL)
      {
          printf("内存不足!");
      }
      printf("请输入位置后的学号!\n");
      scanf("%d",&n);
      
      if(n!=p->num||n!=p->age)//如果插入节点不是头结点,则显示相关信息//
      printf("学号\t年龄\t姓名\t电脑型号\n");
      if(n==p->num||n==p->age)
      {
      printf("对不起,这里不能插入!\n");
      
      }
      
      else if
      
          (n==q->num||q->age)
      {
      
      scanf("%d",&d->num);
      scanf("%d",&d->age);
      scanf("%s",&d->pc);
      scanf("%s",&d->name);
      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);
              scanf("%s",&d->pc);
              scanf("%s",&d->name);
              d->link=p->link;
              p->link=d;
               }
             break;
               
                        
         }
      }
     return(h);
  }  
  void search(stud *h)
  {
    stud *p;
    int m;
    printf("请输入需查学号!");
    scanf("%d",&m);
     
    printf("查找中...\n");
    system("cls");
    p=h;
     
    while(p!=NULL)
    {
   
        if(m==p->num||m==p->age)

        {   printf("学号\t年龄\t姓名\t电脑型号\n");
            printf("%d\t %d\t%s\t%s\n",p->num,p->age,p->pc,p->name);
           break;
         
        }
        else
        {
         p=p->link;
        }
   
     }
     
      if(p==NULL)
     printf("无该生信息!\n");      
     
   
   
   
  }
  

  stud *delet(stud *h)
  {
      stud *q,*p,*I,*d;//指针变量定义//
      stud *creat(int n);//定义链表创建函数//
      stud *load2();//定义文件检测函数//
      FILE *fp,*fp1;//定义文件指针//
      int main();//定义主函数//
      
      p=h;
      int n;
      q=h->link;
      
   

   

   

   
      printf("请输入要删除的学号!");
      scanf("%d",&n);
   
      if(n==p->num||n==p->age)
      {if(p->link!=NULL)
        *h=*(p->link);
        else
            free(p);
      }
      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:\\KKK","w+");
    printf("保存中...\n");
    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,*q,*h;
    FILE *fp;
    printf("读取中...");
   

    fp=fopen("d:\\KKK","r");//打开一个文件//
    if(fp==NULL)//检测文件能否打开//
    {
        printf("can not open file!");
        return(0);
    }
       h=NULL;//初始化头指针//
        
   
        
   

     
      
   
       system("cls");
      
      
    while(!feof(fp))
    {  
        
        p=(stud *)malloc(sizeof(stud));
        fread(p,sizeof(stud),1,fp);
        p->link=h;
        h=p;
    }
     h=h->link;
     if(h==NULL)
         printf("无记录!\n");
     else
     {
     printf("学号\t年龄\t姓名\t电脑型号\n");
     while(h!=NULL)
     {
         
         printf(" %2d",h->num);
         printf("%8d\t",h->age);
         printf("%s\t",h->pc);
         printf("%s",h->name);
          h=h->link;
          printf("\n");

    }
    fclose(fp);
     }
   
}
stud *load2()//文件检测函数,检测文件是否为空////
{
     stud *p,*q,*h;
    FILE *fp;
    int main();
    printf("读取中...");
   

    fp=fopen("d:\\KKK","r");//打开一个文件//
    if(fp==NULL)//检测文件能否打开//
    {
        printf("can not open file!");
        return(0);
    }
       h=NULL;//初始化头指针//
        
   
        
   

     
      
   
       system("cls");
      
      
    while(!feof(fp))
    {  
        
        p=(stud *)malloc(sizeof(stud));
        fread(p,sizeof(stud),1,fp);
        p->link=h;
        h=p;
    }
     h=h->link;
     if(h==NULL)
     {
     printf("无数据,请输入内容!\t");
     printf("按任意键返回主菜单\n");
     getchar();
     if(getchar()!='\n')
     main();
     }
     fclose(fp);
}
stud *load3()//文件检测函数//
{
    stud *p,*q,*h;//结构体指针声明//
    FILE *fp;//文件指针声明//
    int main(),n;//主函数定义//
   
    printf("读取中...");
   

    fp=fopen("d:\\KKK","r");//打开一个文件//
    if(fp==NULL)//检测文件能否打开//
    {
        printf("can not open file!");
        return(0);
    }
       h=NULL;//初始化头指针//
        
   
        
   

     
      
   
       system("cls");
      
      
    while(!feof(fp))
    {  
        
        p=(stud *)malloc(sizeof(stud));
        fread(p,sizeof(stud),1,fp);
        p->link=h;
        h=p;
    }
     h=h->link;
     if(h!=NULL)
     {
     printf("已有数据,您写入将会覆盖,继续?  y n\n");
     }
     getchar();
     if(getchar()=='y')
     
     system("cls");
     return(0);
     fclose(fp);
}
stud *cbc()
{
    stud *p,*q,*h;
    FILE *fp;

   

    fp=fopen("d:\\KKK","r");//打开一个文件//
    if(fp==NULL)//检测文件能否打开//
    {
        printf("can not open file!");
        return(0);
    }
       h=NULL;//初始化头指针//
        
   
        
   

     
      
   
      
      
      
    while(!feof(fp))
    {  
        
        p=(stud *)malloc(sizeof(stud));
        fread(p,sizeof(stud),1,fp);
        p->link=h;
        h=p;
    }
     h=h->link;
    p=h;//头指针赋给p//
    fp=fopen("d:\\KKK","w+");
   
    if(fp==NULL)
    {
        printf("memeory error!");
    }
   
    while(p!=NULL)
    {
        
        fwrite(p,sizeof(stud),1,fp);
        p=p->link;
   
  
    }
     
    fclose(fp);
     
   
}
stud *DEL()
{
    stud *p,*q,*h;
    FILE *fp;
    stud *load2();//定义文件检测函数//
    static n=0;
    printf("读取中...");
   

    fp=fopen("d:\\KKK","r");//打开一个文件//
    if(fp==NULL)//检测文件能否打开//
    {
        printf("can not open file!");
        return(0);
    }
       h=NULL;
        
   
       system("cls");
      
      
    while(!feof(fp))
    {  
        
        p=(stud *)malloc(sizeof(stud));
        fread(p,sizeof(stud),1,fp);
        p->link=h;
        h=p;
    }
      h=h->link;
      load2();
      delet(h);
      printf("保存修改? y n\n");
      getchar();
      if(getchar()=='y')
      save(h);
     fclose(fp);
   
}        
stud *INF()
{
    stud *p,*q,*h;//结构指针定义//
    stud *load2();//检测函数定义//
    FILE *fp;//文件指针定义//
    load2();
    printf("读取中...");
   

    fp=fopen("d:\\KKK","r");//打开一个文件//
    if(fp==NULL)//检测文件能否打开//
    {
        printf("can not open file!");
        return(0);
    }
       h=NULL;
        
    while(!feof(fp))
    {  
        
        p=(stud *)malloc(sizeof(stud));
        fread(p,sizeof(stud),1,fp);
        p->link=h;
        h=p;
    }   
        
   

     
      
   
       system("cls");
      
      
    while(!feof(fp))
    {  
        
        p=(stud *)malloc(sizeof(stud));
        fread(p,sizeof(stud),1,fp);
        p->link=h;
        h=p;
    }
     h=h->link;   
      insert(h);//接到返回指针,执行插入任务//
      printf("保存修改? y n\n");
      getchar();
      if(getchar()=='y')
     save(h);//接到插入函数返回指针,执行保存任务//
    fclose(fp);
   
   
}   

  
stud *SER()
{
    stud *p,*q,*h;
    FILE *fp;
    printf("读取中...");
   

    fp=fopen("d:\\KKK","r");//打开一个文件//
    if(fp==NULL)//检测文件能否打开//
    {
        printf("can not open file!");
        return(0);
    }
       h=NULL;
        
   
        
   

     
      
   
       system("cls");
      
      
    while(!feof(fp))
    {  
        
        p=(stud *)malloc(sizeof(stud));
        fread(p,sizeof(stud),1,fp);
        p->link=h;
        h=p;
    }
      h=h->link;   
      search(h);//获取返回指针,执行查找任务//
      fclose(fp);
   
   
}
int main()
{
    printf("载入中...\n");
    system("cls");
    int c,b,a,e,g,I,Q;
    static n=3;
    struct student *h;
    printf("\t********************欢迎使用学生宿舍管理系统***********************\n");
    printf("   \t\t0 添加记录\n");
    printf("   \t\t1 删除记录\n");
    printf("   \t\t2 显示所有记录\n");
    printf("   \t\t3 插入新记录\n");
    printf("   \t\t4 查找记录\n");
    printf("   \t\t5 退出\n");

    printf("请输入选项!\n");
  
     do
     {
         scanf("%d",&c);
         if(c!=0&&c!=1&&c!=2&&c!=3&&c!=4&&c!=5)
         printf("无相关选项,请重新选择!\n");
         
                  
         
   
         
     }while(c!=0&&c!=1&&c!=2&&c!=3&&c!=4&&c!=5);//如果选项不在所列范围,则循环提示重新输入//

    switch(c)
    {//
        
     
     case 0:load3();
           h=creat(2);
     printf("5 保存\n6 返回\n");
     scanf("%d",&g);
     if(g!=5&&g!=6)
     do//判断是否有对应选项,否则提示重新输入//
     {
         
         printf("没有对应选项,请重新选择!");
         scanf("%d",&g);
     }while(g!=5&&g!=6);
     switch(g)
     {
     case 5:save(h);main();break;
     case 6: main();break;
     };break;
     case 1:DEL();
         n++;//记录调用DEL()函数的次数//
         if(n>3)//如果存储次数与3相除余数不为0,说明链表顺序相发,执行cbc()函数,该函数功能是再读取所存链表,然后再写入磁盘以使链表顺序恢复正常//
             cbc();
     printf("8 退出\n");
     scanf("%d",&I);
     do//判断是否有对应选项,否则提示重新输入//
     {
         if(I!=8)
         printf("没有对应选项,请重新选择!");
        
     }while(I!=8);
     switch(I)//作出选择,若为7则保存修改,为8则返回主菜单//
     {
     

     case 8:main();break;
     };break;
     case 2:load();
     printf("9 返回上一层\n");
     scanf("%d",&g);
     switch(g)
     {
     case 9:main();break;//选择9,调用主函数,返回主菜单//
     };break;
     case 3:INF();
     n++;
     if(n>3)
     cbc();
     printf("11 退出\n");
     scanf("%d",&a);//输入选择值//
     switch(a)
     {
     
     case 11:main();break;//选择11则直接回到主菜单//
     };break;
     case 4:SER();
         
     printf("12 返回上一层\n");
     scanf("%d",&Q);//输入选择的数//
     switch(Q)
     {
      case 12:main();break;//如果所选数为12,调用主函数,返回主菜单//
     };break;
     case 5:printf("您确定退出吗? y n\n");
           
           getchar();
         if(getchar()=='y')
         {
             if(getchar()!='\n')
             return(0);break;//退出,按任意键//
         }
         else
         {
             system("cls");
             main();break;
         }
              
}//
}   
还有一个问题,为什么我创建时按y得按两遍才可以呢?
搜索更多相关主题的帖子: 电脑型号 检测 include memory return 
2011-11-03 22:41
heroinearth
Rank: 10Rank: 10Rank: 10
来 自:云南曲靖
等 级:青峰侠
帖 子:430
专家分:1506
注 册:2011-10-24
收藏
得分:10 
stud 是个结构变量,可以这样定义吗?stud *h,*p,*q;请高手指点
2011-11-03 23:04
laznrbfe
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:482
专家分:1599
注 册:2011-5-22
收藏
得分:10 
插入的函数,我觉得
if(n!=p->num||n!=p->age)//如果插入节点不是头结点,则显示相关信息//
        printf("学号\t年龄\t姓名\t电脑型号\n");
    if(n==p->num||n==p->age)
    {
        printf("对不起,这里不能插入!\n");
        
    }
有问题
2011-11-03 23:22
laznrbfe
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:482
专家分:1599
注 册:2011-5-22
收藏
得分:0 
回复 2楼 heroinearth
可以这样定义为指针的。
2011-11-03 23:23
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
收藏
得分:0 
回复 3楼 laznrbfe
有什么问题?我改了下好像不是这里的问题
2011-11-04 07:55
laznrbfe
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:482
专家分:1599
注 册:2011-5-22
收藏
得分:0 
回复 5楼 zyx1989
if语句
2011-11-06 19:13
lowkey_c
Rank: 2
等 级:论坛游民
帖 子:19
专家分:28
注 册:2011-10-14
收藏
得分:0 
指针   头都肿了。。
2011-11-06 22:15
快速回复:此程序插入函数有问题,在插入多组数据后,不能插入到正确位置,求解
数据加载中...
 
   



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

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