正在学习链表,无法正常输入,找不到错误在哪里,求解。
#include
#include
struct Book {
char title[36];
char name[24];
int ID;
};//数据
struct Date {
struct Book date;
struct Date *next;
};//结构体
struct Date creat_Nobe(struct Date *head) {
head=(struct Date *)malloc(sizeof(struct Date));
head->next=NULL;
return *head;
}//创建链表
struct Date link_Nobe(struct Date *head,struct Date *p1) { //p1为要插入的数
struct Date *p2;
p1=(struct Date *)malloc(sizeof(struct Date));
p2=(struct Date *)malloc(sizeof(struct Date));
if(head->next==NULL) {
head->next=p2;
p2->next=NULL;
} else {
p1->next=p2;
head->next=p1;
p2=p1;
}
}//插入链表
//========================================
struct Date scan_Nobe(struct Date *head) {
struct Date *p1;
char choice='y';
while(choice=='Y'||choice=='y') {
p1=(struct Date *)malloc(sizeof(struct Date));
printf("\n书名:");
scanf("%s",p1->date.title);
getchar();
printf("\n=====测试书名:%s=====",(*p1).date.title);
printf("\n作者:");
scanf("%s",p1->date.name);
getchar();
printf("\n=====测试作者:%s=====",(*p1).date.name);
printf("\n书ID:");
scanf("%s",p1->date.ID); //这里无法正常输入
getchar();
printf("\n=====测试书ID:%d=====",(*p1).date.ID);
link_Nobe(head,p1);//链接
printf("\n是否继续输入(Y/N)");
scanf("%c",&choice);
}
}//输入数据
//==========================================
struct Date put_Nobe(struct Date *head) {
struct Date *p1=head->next;
if(p1==NULL) {
printf("没有数据");
exit (1);
} else {
while(p1->next==NULL) {
printf("\n书名:%s",(*p1).date.title);
printf("\n作者:%s",(*p1).date.name);
printf("\n书ID:%d",(*p1).date.ID);
p1=p1->next;
}
}
}//输出数据
int main(void) {
struct Date *head=NULL;
creat_Nobe(head);
scan_Nobe(head);
put_Nobe(head);
return 0;
}
改动处见注释,供参考:
#include<stdio.h>
#include<stdlib.h>
struct Book {
char title[36];
char name[24];
int ID;
};//数据
struct Date {
struct Book date;
struct Date *next;
};//结构体
struct Date* creat_Nobe(struct Date *head) { //修改
//struct Date creat_Nobe(struct Date *head)
head=(struct Date *)malloc(sizeof(struct Date));
head->next=NULL;
return head; //return* head; 修改
}//创建链表
void link_Nobe(struct Date *head,struct Date *p1) { //p1为要插入的数
//struct Date link_Nobe(struct Date *head,struct Date *p1) 修改
struct Date *p2 = head;
while(p2->next){
p2 = p2->next;
}
p2->next = p1;
//p1=(struct Date *)malloc(sizeof(struct Date)); 修改
//p2=(struct Date *)malloc(sizeof(struct Date)); 修改
//if(head->next==NULL) {
// head->next=p2;
// p2->next=NULL;
//} else {
// p1->next=p2;
// head->next=p1;
// p2=p1;
//}
}//插入链表
//========================================
void scan_Nobe(struct Date *head) { //修改
//struct Date scan_Nobe(struct Date *head)
struct Date *p1;
char choice='y';
while(choice=='Y'||choice=='y') {
p1=(struct Date *)malloc(sizeof(struct Date));
printf("\n书名:");
scanf("%s",p1->date.title);
getchar();
printf("\n=====测试书名:%s=====",(*p1).date.title);
printf("\n作者:");
scanf("%s",p1->date.name);
getchar();
printf("\n=====测试作者:%s=====",(*p1).date.name);
printf("\n书ID:");
scanf("%d",&p1->date.ID); //书号是 int型数据,用%d
//scanf("%s",p1->date.ID); //这里无法正常输入 修改
getchar();
printf("\n=====测试书ID:%d=====",(*p1).date.ID);
link_Nobe(head,p1);//链接
printf("\n是否继续输入(Y/N)");
scanf(" %c",&choice); // 修改
}
}//输入数据
//==========================================
void put_Nobe(struct Date *head) { //修改
//struct Date put_Nobe(struct Date *head)
struct Date *p1=head->next;
if(p1==NULL) {
printf("没有数据");
exit (1);
} else {
while(p1 != NULL) { //while(p1->next==NULL) 修改
printf("\n书名:%s",(*p1).date.title);
printf("\n作者:%s",(*p1).date.name);
printf("\n书ID:%d",(*p1).date.ID);
p1=p1->next;
}
}
}//输出数据
int main(void) {
struct Date *head=NULL;
head = creat_Nobe(head); //修改
scan_Nobe(head);
put_Nobe(head);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
struct Book {
char title[36];
char name[24];
int ID;
};//数据
struct Date {
struct Book date;
struct Date *next;
};//结构体
struct Date* creat_Nobe(struct Date *head) {
head=(struct Date *)malloc(sizeof(struct Date));
head->next=NULL;
return head;
}//创建链表
struct Date* link_Nobe(struct Date *head,struct Date *p1) { //p1为要插入的数
//printf("23\n");
struct Date *p2 = NULL;
//p1=(struct Date *)malloc(sizeof(struct Date));
if(head->next==NULL) {
//printf("133");
head->next=p1;
} else {
p2 = head;
while(p2->next != NULL){
p2 = p2->next;
//printf("书名:%s\n",(*p2).date.title);
}
p2->next = p1;
}
return head;
}//插入链表
//========================================
struct Date* scan_Nobe(struct Date *head) {
struct Date *p1;
char choice='y';
while(choice=='Y'||choice=='y') {
p1=(struct Date *)malloc(sizeof(struct Date));
printf("\n书名:");
scanf("%s",p1->date.title);
//getchar();
printf("\n=====测试书名:%s=====",(*p1).date.title);
printf("\n作者:");
scanf("%s",p1->date.name);
//getchar();
printf("\n=====测试作者:%s=====",(*p1).date.name);
printf("\n书ID:");
scanf("%d",&p1->date.ID); //这里无法正常输入
//getchar();
p1->next = NULL;
printf("\n=====测试书ID:%d=====",(*p1).date.ID);
head = link_Nobe(head,p1);//链接
printf("\n是否继续输入(Y/N)");
getchar();
scanf("%c",&choice);
}
return head;
}//输入数据
//==========================================
struct Date put_Nobe(struct Date *head) {
struct Date *p1=head->next;
if(p1==NULL) {
printf("没有数据");
exit (1);
} else {
while(p1->next!=NULL) {
printf("\n书名:%s",(*p1).date.title);
printf("\n作者:%s",(*p1).date.name);
printf("\n书ID:%d",(*p1).date.ID);
p1=p1->next;
}
}
}//输出数据
int main(void) {
struct Date *head=NULL;
head = creat_Nobe(head);
head = scan_Nobe(head);
put_Nobe(head);
return 0;
}