单链表
怎么程序只能运行个开头?#include<stdio.h>
#include<stdlib.h>
struct Book//单链表头插法
{
char title;
char author;
struct Book *next;
};
void getInput(struct Book *book)
{
printf("请输入书名:");
scanf("%s",book->title);
printf("请输入作者:");
scanf("%s",book->author);
}
void addBook(struct Book **library)
{
struct Book *book,*temp;
book=(struct Book *)malloc (sizeof(struct Book));
if(book=NULL)
{
printf("内存分配失败!\n");
exit(1);
}
getInput(book);
if(*library !=NULL)
{
temp=*library;
*library=book;
book->next=temp;
}
else
{
*library=book;
book->next=NULL;
}
}
void printLibrary(struct Book *library)
{
struct Book *book;
int count =1;
book=library;
while(book!=NULL)
{
printf("Book%d:",count);
printf("书名:%s",book->title);
printf("作者:%s",book->author);
book=book->next;
count++;
}
}
void releaseLibrary(struct Book *library)
{
while(library !=NULL)
{
free(library);
library=library->next;
}
}
int main(void)
{
struct Book *library=NULL;
int ch;
while(1)
{
printf("请问是否需要录入书籍信息(Y/N):");
do
{
ch=getchar();
}while(ch!='Y'&&ch!='N');
if(ch=='Y')
{
addBook(&library);
}
else
{
break;
}
}
printf("请问是否需要打印图书信息(Y/N):");
do
{
ch=getchar();
}while(ch!='Y'&&ch!='N');
if(ch=='Y')
{
printLibrary(library);
}
releaseLibrary(library);
return 0;
} if(book==NULL)
{
printf("内存分配失败!\n"); ba21 发表于 2021-5-3 23:01
if(book==NULL)
{
printf("内存分配失败!\n");
if里面的条件错了这个if就不执行了,可是这个if里面的程序能对整个程序有影响么? 认真学好C语言 发表于 2021-5-4 11:34
if里面的条件错了这个if就不执行了,可是这个if里面的程序能对整个程序有影响么?
请问你能给NULL对象 赋值吗? ba21 发表于 2021-5-4 23:10
请问你能给NULL对象 赋值吗?
懂了,谢谢
页:
[1]