| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 611 人关注过本帖
标题:求大神帮忙看下 代码怎么不能将文件中的内容导入到链表中
只看楼主 加入收藏
yzjj
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-5-19
收藏
 问题点数:0 回复次数:1 
求大神帮忙看下 代码怎么不能将文件中的内容导入到链表中
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct book{
 int num;
 char name[20];
 double price;
 char writer[20];
 struct book*next;};
struct book*sortdata(struct book*head);
 struct book*insert(struct book*head,struct book*b);
struct book*Delete(struct book*head,int num);
struct book*search(struct book*head,int num);
void print(struct book*head);
 int main(void)

 {
  FILE *fp;
  struct book*head,*p,*tail,*ptr;
 int num,choice;
  char name[20],writer[20];
  double price;
  int size=sizeof(struct book);
  head=tail=NULL;
  if((fp=fopen("c:\\book.txt","r"))==NULL)
  { printf("File open error!\n");
    exit(0);
  }
  while(num!=0)
   fscanf(fp,"%d%s%lf%s",p->num,p->name,p->price,p->writer);
  while(num!=0){
          p=(struct book*)malloc(size);
          p->num=num;
          strcpy(p->name,name);
          p->price=price;
          strcpy(p->writer,writer);
          p->next=NULL;
          if(head==NULL)
              head=p;
          else
              tail->next=p;
          tail=p;
          scanf("%d%s%lf%s",&num,name,&price,writer);}
do{
      printf("1:排序 2:插入 3:删除 4:查找 5:输出 0:Exit\n");
      scanf("%d",&choice);
      switch(choice){
      case 1:
          head=sortdata(head);
          break;
      case 2:
          printf("输入一个图书信息 num,name,price,writer:\n");
          scanf("%d%s%lf%s",&num,name,&price,writer);
              p=(struct book*)malloc(size);
          p->num=num;
          strcpy(p->name,name);
          p->price=price;
          strcpy(p->writer,writer);
          head=insert(head,p);
              break;
      case 3:
          printf("输入要删除图书书号 num:\n");
          scanf("%d",&num);
          head=Delete(head,num);
          break;
      case 4:
 printf("输入要查找图书书号 num:\n");
scanf("%d",&num);
          ptr=search(head,num);
          printf("%d %s %lf %s\n",ptr->num,ptr->name,ptr->price,ptr->writer);
          break;
      case 5:
          print(head);
              break;
      case 0:
          break;
      }
  }while(choice!=0);
      return 0;
 }

struct book*sortdata(struct book*head)
{struct book*ptr1,*ptr2,*ptr3;
ptr1=head;
ptr2=head->next;
while(ptr2!=NULL){
    ptr3=ptr2;
    if(ptr1->num>ptr3->num)
    {    ptr1->num=ptr3->num;
 strcpy(ptr1->name,ptr3->name);
          ptr1->price=ptr3->price;
          strcpy(ptr1->writer,ptr3->writer);
          ptr3=ptr3->next;}
    ptr1=ptr2;
    ptr2=ptr2->next;
}
return head;
}
struct book*insert(struct book*head,struct book*b)
 {
  struct book*ptr,*ptr1,*ptr2;
  ptr2=head;
  ptr=b;
  if(head==NULL){
   head=ptr;
   head->next=NULL;
  }
  else{
   while((ptr->num>ptr2->num)&&(ptr2->next!=NULL)){
    ptr1=ptr2;
    ptr2=ptr2->next;
   }
   if(ptr->num<=ptr2->num){
    if(head==ptr2)  head=ptr;
    else ptr1->next=ptr;
    ptr->next=ptr2;
   }
   else{
    ptr2->next=ptr;
    ptr->next=NULL;
   }
  }
  return head;
 }
struct book*Delete(struct book*head,int num)
{
    struct book*ptr1,*ptr2;
    while(head!=NULL&&head->num==num){
        ptr2=head;
        head=head->next;
        free(ptr2);
    }
    if(head==NULL)
        return NULL;
    ptr1=head;
    ptr2=head->next;
    while(ptr2!=NULL){
        if(ptr2->num==num){
            ptr1->next=ptr2->next;
            free(ptr2);
        }
        else
            ptr1=ptr2;
        ptr2=ptr1->next;
    }
    return head;
}

struct book*search(struct book*head,int num)
{struct book*p,*ptr;
if(head==NULL)
printf("图书馆中没有存入图书\n");

for(ptr=head;ptr!=NULL;ptr=ptr->next)
if(ptr->num==num)
p=ptr;
return p;
}

void print(struct book*head)
{
        struct book*ptr;
    if(head==NULL){
        printf("No Recods\n");
        return;
    }
    printf("全部图书信息:\n");
    printf("编号\t书名\t作者\t价格\n");
    for(ptr=head;ptr!=NULL;ptr=ptr->next)
printf("%d %s %lf %s\n",ptr->num,ptr->name,ptr->price,ptr->writer);
        
}


[ 本帖最后由 yzjj 于 2015-5-19 22:50 编辑 ]
搜索更多相关主题的帖子: insert choice double search include 
2015-05-19 22:40
yzjj
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-5-19
收藏
得分:0 
求大神
2015-05-19 22:41
快速回复:求大神帮忙看下 代码怎么不能将文件中的内容导入到链表中
数据加载中...
 
   



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

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