C语言程序自动跳出问题 急!!!!!求解
求高手!! #include<stdio.h> #include<math.h> #include<string.h> #include<stdlib.h> struct books_list { char author[20]; /*作者名*/ char bookname[20]; /*书名*/ char publisher[20]; /*出版单位*/ char pbtime[15]; /*出版时间*/ char loginnum[10]; /*登陆号*/ float price; /*价格*/ char classfy[10]; /*分类号*/ struct books_list * next; /*链表的指针域*/ }; struct books_list * Create_Books_Doc(); /*新建链表*/ void InsertDoc(struct books_list * head); /*插入*/ void DeleteDoc(struct books_list * head );/*删除*/ void Print_Book_Doc(struct books_list * head);/*浏览*/ void search_book(struct books_list * head); /*查询*/ void info_change(struct books_list * head);/*修改*/ void save(struct books_list * head);/*保存数据至文件*/ /*新建链表头节点*/ struct books_list * Create_Books_Doc() { struct books_list * head; head=(struct books_list *)malloc(sizeof(struct books_list)); /*分配头节点空间*/ head->next=NULL; /*头节点指针域初始化,定为空*/ return head; } /*保存数据至文件*/ void save(struct books_list * head) { struct books_list *p; FILE *fp; p=head; fp=fopen("data.txt","w+"); /*以写方式新建并打开 data.txt文件*/ fprintf(fp,"...............................................................................\n"); /*向文件输出表格*/ fprintf(fp," CAS RN book name author dan wei data sortnum price \n"); getch(); /*指针从头节点开始移动,遍历至尾结点,依次输出图书信息*/ while(p->next!= NULL) { p=p->next; fprintf(fp,"%-6.6s %-10.10s %-10.10s %-10.10s %-12.12s %-6.6s %.2f \n",p->loginnum,p->bookname,p->author,p->publisher,p->pbtime,p->classfy,p->price); } fclose(fp); printf(" save data.txt 文件\n"); } /*插入*/ void InsertDoc(struct books_list *head) { /*定义结构体指针变量 s指向开辟的新结点首地址 p为中间变量*/ struct books_list *s, *p; char flag='Y'; /*定义flag,方便用户选择重复输入*/ p=head; /*遍历到尾结点,p指向尾结点*/ while(p->next!= NULL) { p=p->next; } /*开辟新空间,存入数据,添加进链表*/ while(flag=='Y'||flag=='y') { s=(struct books_list *)malloc(sizeof(struct books_list)); printf("\n Please input books landed number:"); fflush(stdin); scanf("%s",s->loginnum); printf("\n Please enter the book name:"); fflush(stdin); scanf("%s",s->bookname); printf("\n Please input book author's name:"); fflush(stdin); scanf("%s",s->author); printf("\n Please input book publishing house:"); fflush(stdin); scanf("%s",s->publisher); printf("\n Please enter publishing time:"); fflush(stdin); scanf("%s",s->pbtime); printf("\n Please input book classification number:"); fflush(stdin); scanf("%s",s->classfy); printf("\n Please input book prices:"); fflush(stdin); scanf("%f",&s->price); printf("\n"); getch(); p->next=s; /*将新增加的节点添加进链表*/ p=s; /*p指向尾节点,向后移*/ s->next=NULL; getch(); printf("......Add success!"); getch(); printf("\n Continue to add?(Y/N):");
大概你的原文太长。我们只看到前一部分。 问题在哪里?
答:答案:首结点成为尾结点 q=s;s=s->next;p=s; /* s指针后移,指向第2个节点,q指针指向首节点 */ while(p->next) p=p->...详情>>