| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 736 人关注过本帖
标题:我这个链表中的init()这个函数错在哪里?
只看楼主 加入收藏
shq711
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-4-19
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:4 
我这个链表中的init()这个函数错在哪里?
#include <stdio.h>
struct student
{
   char name[15];
   char sex[4];
   char QQ[15];
   struct student *next;
};
struct student *head=NULL;
void creat()
{
   struct student *p1,*p2;
   int count=0;
   p1=(struct student *)malloc(sizeof(struct student));
   memset(p1,0,sizeof(struct student));//格式化内存
   scanf("%s%s%s",p1->name,p1->sex,p1->QQ);
    p1->next=NULL;
   while(strcmp(p1->name,"0")!=0)
   {
        count++;
     if(count==1)head=p1;
     else p2->next=p1;
     p2=p1;   
     p1=(struct student *)malloc(sizeof(struct student));
     scanf("%s %s %s",p1->name,p1->sex,p1->QQ);
     p1->next=NULL;
   }
   free(p1);
}
void print()
{
   struct student *p;
   if(head==NULL) printf("lianbiaokong\n");
   else
   {
    p=head;
    while(p!=NULL)
    {
       printf("%s %s %s\n",p->name,p->sex,p->QQ);
       p=p->next;
    }
   }
}
void last()
{
    struct student *p2,*p;
    p=(struct student *)malloc(sizeof(struct student));
    scanf("%s %s %s",p->name,p->sex,p->QQ);
    p->next=NULL;
    if(head!=NULL)
    {
        p2=head;
        while(p2!=NULL&&p2->next!=NULL)
        {
        p2=p2->next;
        }   
        p2->next=p;
    }
    else
    head=p;   
}

void cover()
{
  printf("1创建\n");
  printf("2输出\n");
  printf("3查询\n");
  printf("4修改\n");
  printf("5删除结点\n");
  printf("6插入结点\n");
  printf("7尾部插入结点\n");
  printf("8退出程序\n");        
}
void search()
{
  struct student *p;
  char name[15];
  if(head==NULL) printf("list kong\n");
  else
    {
    printf("输入要查询的人的姓名:\n");
    scanf("%s",name);
   p=head;
   while(p->next!=NULL&&strcmp(name,p->name)!=0)
   p=p->next;
   if( strcmp(name,p->name)==0)
   {
     printf("你要查询的人的信息为:\n");
     printf("%s %s %s\n",p->name,p->sex,p->QQ);
   }
   else
   printf("查无此人\n");
   }   
}
void modify()
{
    struct student *p;
  char name[15];
  if(head==NULL) printf("list kong\n");
  else
    {
    printf("输入要修改的人的姓名:\n");
    scanf("%s",name);
   p=head;
   while(p->next!=NULL&&strcmp(name,p->name)!=0)
   p=p->next;
   if( strcmp(name,p->name)==0)
   {
     printf("你要修改的人的信息为:\n");
     printf("%s %s %s\n",p->name,p->sex,p->QQ);
     printf("重写输入此人的信息:\n");
     scanf("%s %s %s",p->name,p->sex,p->QQ);   
   }
   else
   printf("查无此人\n");
   }     
}
void del()
{
    struct student *p1,*p2;
    char name[15];
    if(head==NULL) printf("list kong\n");
  else
    {
    p1=head;
    printf("输入要删除的人的姓名:\n");
    scanf("%s",name);
   while(p1->next!=NULL&&strcmp(name,p1->name)!=0)
   {
   p2=p1;
   p1=p1->next;
   }
   if( strcmp(name,p1->name)==0)
   {
       if(p1==head)
       head=p1->next;
       else
       p2->next=p1->next;  
   }
   else
   printf("查无此人\n");
}
free(p1);
}
void insert()
{
    struct student *p,*p1,*p2;
    char name[15];
    p=(struct student *)malloc(sizeof(struct student));
   memset(p,0,sizeof(struct student));//格式化内存
   scanf("%s%s%s",p->name,p->sex,p->QQ);
    p->next=NULL;
    if(head==NULL) printf("list kong\n");
  else
    {
    printf("输入要修改的人的姓名:\n");
    scanf("%s",name);
   p1=head;
   while(p1->next!=NULL&&strcmp(name,p1->name)!=0)
   {
   p2=p1;
   p1=p1->next;
   }
   if( strcmp(name,p1->name)==0)
   {
       if(p1==head)
       {
       head=p;
       p->next=p1;
       }
       else
       {
       p2->next=p;
       p->next=p1;
       }
   }
   else
   printf("查无此人\n");
    }
    free(p1);
}
void save()
{
    struct student *p;
    FILE *fp;
    fp=fopen("a.txt","wb");
    if(fp!=NULL)
    {
        p=head;
        while(p!=NULL)
        {
            fwrite(p,sizeof(struct student),1,fp);
            p=p->next;
        }
        fclose(fp);
    }
    else
    printf("文件没有打开");
}
void init()
{
    struct student *p1,*p2;
    int count=0;
    FILE *fp;
    fopen("a.txt","rb");
    if(fp!=NULL)
    {
    p1=(struct student *)malloc(sizeof(struct student));
    memset(p1,0,sizeof(struct student));//格式化内存
    while( fread(p1,sizeof(struct student),1,fp)==1)
    {
    count++;
    if(count==1)
    head=p1;
    else
    p2->next=p1;
    p2=p1;
    p1=(struct student *)malloc(sizeof(struct student));
    memset(p1,0,sizeof(struct student));//格式化内存
    }
    p1->next=NULL;   
    free(p1);
    fclose(fp);
}
    else
    printf("文件没有成功打开");   
}
main()
{
 int choice;
 init();   
 printf("******学生信息管理系统******\n");
 while(1)
 {
   cover();
   scanf("%d",&choice);
   switch(choice)
   {
    case 1:creat();save();break;
    case 2:print();break;
    case 3:search();break;
    case 4:modify();save();break;
    case 5:del();save();break;
    case 6:insert();save();break;
    case 7:last();save();break;
    case 8:exit(0);break;
    default :printf("输入不合法\n");exit(0);
   }
 }
}

搜索更多相关主题的帖子: 链表 init 函数 
2010-05-30 17:49
djhwang
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-3-18
收藏
得分:0 
void init()
{
    struct student *p1,*p2;
    int count=0;
    FILE *fp;
    fopen("a.txt","rb");
    if(fp!=NULL)
fopen函数值没有赋给fp。我也刚学不知道对不对~还有没有其它的错误~
2010-05-30 18:41
colinxt
Rank: 2
等 级:论坛游民
帖 子:34
专家分:10
注 册:2010-4-5
收藏
得分:0 
回复 2楼 djhwang
没错
2010-05-30 18:49
djhwang
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-3-18
收藏
得分:0 
if(count==1)
    head=p1;
    else
    p2->next=p1;
    p2=p1;
    p1=(struct student *)malloc(sizeof(struct student));
    memset(p1,0,sizeof(struct student));//格式化内存
fread(p1,sizeof(struct student),1,fp);
    }
p2->next=NULL;   
    free(p1);
    fclose(fp);
}
    else
    printf("文件没有成功打开"); 这样改不知道对不对~欢迎加我QQ一起探讨QQ290001662   
2010-05-30 19:14
yunfeismile
该用户已被删除
收藏
得分:20 
提示: 作者被禁止或删除 内容自动屏蔽
2010-05-30 22:10
快速回复:我这个链表中的init()这个函数错在哪里?
数据加载中...
 
   



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

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