|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
可以直接看第44行的位置。不明白为什么第二种就输出不了
第一种- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- struct book //书籍信息的结构
- {
- int book_id;
- char book_name[30]; //一个书的id、一个书名、5个关键字、3个作者、一个发布地、一个发布时间
- char keyword1[30];
- char keyword2[30];
- char keyword3[30];
- char keyword4[30];
- char keyword5[30];
- char author1[30];
- char author2[30];
- char author3[30];
- char publisher[30];
- char publish_time[30];
- struct book *next;
- }*Book;
- struct book *data_book_sort(struct book *head);
- struct book *exchange(struct book *p); //交换p的右边与p右边的右边的结点
- struct book *data_book_input(); //将数据库数据导出到一个结构指针上
- void main()
- {
- struct book *head;
- head = data_book_input(); //head是头指针
- head = data_book_sort(head);
- printf("%d\n",head->book_id); //这里是测试用
- printf("%s\n",head->book_name);
- head = head->next;
- printf("%d\n",head->book_id);
- printf("%s\n",head->book_name);
- }
- struct book *data_book_sort(struct book *head)
- {
- struct book *p, *q;
- p = (struct book *)malloc(sizeof(struct book));
- q = (struct book *)malloc(sizeof(struct book));
- q->next = head;
- head = q; //建立一个空的头结点插入到链表头里
- p = q = head;
- while(q->next != NULL)
- {
- p = head;
- while(p->next->next != NULL) //不断循环,把最大的放到最后
- {
- if(p->next->book_id > p->next->next->book_id) //从小到大排序
- exchange(p);
- p = p->next;
- }
- q = q->next;
- }
- head = head->next;
- return head;
- }
- struct book *exchange(struct book *p) //交换p后面两个结点顺序
- {
- struct book *t;
- t = p->next;
- p->next = t->next;
- t->next = p->next->next;
- p->next->next = t;
- }
- struct book *data_book_input() //将数据库数据导出到一个结构指针上
- {
- struct book *p,*head,*tail;
- head = tail = NULL;
- int book_id;
- char book_name[30];
- char keyword1[30];
- char keyword2[30];
- char keyword3[30];
- char keyword4[30];
- char keyword5[30];
- char author1[30];
- char author2[30];
- char author3[30];
- char publisher[30];
- char publish_time[30];
- FILE *f = NULL;
- if((f=fopen("data_book.txt","r")) == NULL)
- {
- printf("File is Error! No1\n");
- return NULL;
- }
- while(fscanf(f, "%d %s %s %s %s %s %s %s %s %s %s %s",
- &book_id,book_name,keyword1,keyword2,keyword3,keyword4,keyword5,
- author1,author2,author3,publisher,publish_time) != EOF)
- {
- if((p = (struct book *)malloc(sizeof(struct book)))==NULL) //分配储存空间
- {
- printf("Error!");
- return NULL;
- }
- p->book_id = book_id; //导入数据
- strcpy(p->book_name , book_name);
- strcpy(p->keyword1 , keyword1);
- strcpy(p->keyword2 , keyword2);
- strcpy(p->keyword3 , keyword3);
- strcpy(p->keyword4 , keyword4);
- strcpy(p->keyword5 , keyword5);
- strcpy(p->author1 , author1);
- strcpy(p->author2 , author2);
- strcpy(p->author3 , author3);
- strcpy(p->publisher , publisher);
- strcpy(p->publish_time , publish_time);
- p->next = NULL; //顺序建立链表
- if(head == NULL)
- head = p;
- else
- tail->next = p;
- tail = p;
- } //链表头地址是head,最后的next是NULL
- fclose(f);
- return head; //返回头指针
- }
复制代码
第二种- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- struct book //书籍信息的结构
- {
- int book_id;
- char book_name[30]; //一个书的id、一个书名、5个关键字、3个作者、一个发布地、一个发布时间
- char keyword1[30];
- char keyword2[30];
- char keyword3[30];
- char keyword4[30];
- char keyword5[30];
- char author1[30];
- char author2[30];
- char author3[30];
- char publisher[30];
- char publish_time[30];
- struct book *next;
- }*Book;
- struct book *data_book_sort(struct book *head);
- struct book *exchange(struct book *p); //交换p的右边与p右边的右边的结点
- struct book *data_book_input(); //将数据库数据导出到一个结构指针上
- void main()
- {
- struct book *head;
- head = data_book_input(); //head是头指针
- head = data_book_sort(head);
- printf("%d\n",head->book_id); //这里是测试用
- printf("%s\n",head->book_name);
- head = head->next;
- printf("%d\n",head->book_id);
- printf("%s\n",head->book_name);
- }
- struct book *data_book_sort(struct book *head)
- {
- struct book *p, *q;
- p = (struct book *)malloc(sizeof(struct book));
- q = (struct book *)malloc(sizeof(struct book));
- q->next = head;
- head = q; //建立一个空的头结点插入到链表头里
- p = q = head;
- while(q->next->next != NULL)
- {
- p = head;
- while(p->next->next != NULL) //不断循环,把最大的放到最后
- {
- if(p->next->book_id > p->next->next->book_id) //从小到大排序
- exchange(p);
- p = p->next;
- }
- q = q->next;
- }
- head = head->next;
- return head;
- }
- struct book *exchange(struct book *p) //交换p后面两个结点顺序
- {
- struct book *t;
- t = p->next;
- p->next = t->next;
- t->next = p->next->next;
- p->next->next = t;
- }
- struct book *data_book_input() //将数据库数据导出到一个结构指针上
- {
- struct book *p,*head,*tail;
- head = tail = NULL;
- int book_id;
- char book_name[30];
- char keyword1[30];
- char keyword2[30];
- char keyword3[30];
- char keyword4[30];
- char keyword5[30];
- char author1[30];
- char author2[30];
- char author3[30];
- char publisher[30];
- char publish_time[30];
- FILE *f = NULL;
- if((f=fopen("data_book.txt","r")) == NULL)
- {
- printf("File is Error! No1\n");
- return NULL;
- }
- while(fscanf(f, "%d %s %s %s %s %s %s %s %s %s %s %s",
- &book_id,book_name,keyword1,keyword2,keyword3,keyword4,keyword5,
- author1,author2,author3,publisher,publish_time) != EOF)
- {
- if((p = (struct book *)malloc(sizeof(struct book)))==NULL) //分配储存空间
- {
- printf("Error!");
- return NULL;
- }
- p->book_id = book_id; //导入数据
- strcpy(p->book_name , book_name);
- strcpy(p->keyword1 , keyword1);
- strcpy(p->keyword2 , keyword2);
- strcpy(p->keyword3 , keyword3);
- strcpy(p->keyword4 , keyword4);
- strcpy(p->keyword5 , keyword5);
- strcpy(p->author1 , author1);
- strcpy(p->author2 , author2);
- strcpy(p->author3 , author3);
- strcpy(p->publisher , publisher);
- strcpy(p->publish_time , publish_time);
- p->next = NULL; //顺序建立链表
- if(head == NULL)
- head = p;
- else
- tail->next = p;
- tail = p;
- } //链表头地址是head,最后的next是NULL
- fclose(f);
- return head; //返回头指针
- }
复制代码 |
|