|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
int count = 0;
struct Book
{
char title[128];
char author[40];
struct Book* next;
};
main()
{
struct Book * pnew,*pend,*phead;
phead = NULL;
pend=pnew = (struct Book*)malloc(sizeof(struct Book));
scanf_s("%s", &pnew->title,128);
scanf_s("%s", &pnew->author,40);
while ((strcmp(pnew->title), "-1") !=0)
{
count++;
if (count == 1)
{
pnew->next = NULL;
phead= pnew;
pend = pnew;
}
else
{
pnew->next = NULL;
pend->next = pnew;
pend = pnew;
}
free(pnew);
pnew= (struct Book*)malloc(sizeof(struct Book));
printf("title:");
scanf_s("%s", &pnew->title, 128);
printf("wuthor:");
scanf_s("%s", &pnew->author, 40);
}
}
求助创建单链表表,while判定条件输入-1时,为什么不能退出循环!
本帖最后由 xieglt 于 2020-10-23 07:30 编辑
错误
1、没有包含头文件 #include <string.h>
2、while ((strcmp(pnew->title), "-1") !=0)
改成
while (strcmp(pnew->title, "-1") !=0)
3、
scanf_s("%s", &pnew->title,128);
scanf_s("%s", &pnew->author,40);
这两个输入不需要取址符,改成
scanf_s("%s", pnew->title,128);
scanf_s("%s", pnew->author,40);
|
|