这次是更改过来的了,打印的问题仍在
#include<stdio.h>
#include<stdlib.h>
struct Book
{
char title[128];
struct Book *next;
};
void addBook(struct Book **lbr);
void getInput(struct Book *book);
void printlbr(struct Book *lbr);
void release(struct Book **lbr);
void addBook(struct Book **lbr)
{
struct Book *book,*temp;
book = (struct Book *)malloc(sizeof(struct Book));
if(book == NULL)
{
printf("内存分配失败\n");
exit(1);
}
getInput(book);
if(*lbr != NULL)
{
temp = *lbr;
*lbr = book;
book = temp;
}
else
{
*lbr = book;
book->next = NULL;
}
}
void getInput(struct Book *book)
{
printf("请输入书名:");
scanf("%s",book->title);
}
void printlbr(struct Book *lbr)
{
struct Book *book;
int count = 1;
book = lbr;
while(book != NULL)
{
printf("Book%d:\n",count);
printf("书名: %s\n",book->title);
book = book->next;
count++;
}
}
void release(struct Book **lbr)
{
struct Book *temp;
while(lbr != NULL)
{
temp = *lbr;
*lbr = (*lbr)->next;
free(temp);
}
}
int main(void)
{
struct Book *lbr;
int ch;
while(1)
{
printf("请问是否需要录入书籍信息(Y/N):");
do
{
ch = getchar();
}while(ch != 'Y' && ch != 'N');
if(ch == 'Y')
{
addBook(&lbr);
}
else
{
break;
}
}
printf("请问是否需要打印书籍信息(Y/N):");
do
{
ch = getchar();
}while(ch != 'Y' && ch != 'N');
if(ch == 'Y')
{
printlbr(lbr);
}
release(&lbr);
return 0;
}
没有正确地设置book->next指针。你应该让book->next指向temp,还有getInput函数放在addBook函数之前,不然会报错
运行结果
帮你修改好了,拿去用
#include <stdio.h>
#include <stdlib.h>
struct Book {
char title[128];
struct Book *next;
};
// 输入书籍信息
void getInput(struct Book *book) {
printf("请输入书名:");
scanf("%s", book->title);
}
// 添加书籍
void addBook(struct Book **lbr) {
struct Book *book, *temp;
book = (struct Book *)malloc(sizeof(struct Book));
if (book == NULL) {
printf("内存分配失败\n");
exit(1);
}
getInput(book);
if (*lbr != NULL) {
temp = *lbr;
*lbr = book;
book->next = temp;
} else {
*lbr = book;
book->next = NULL;
}
}
// 打印书籍链表
void printlbr(struct Book *lbr) {
struct Book *book;
int count = 1;
book = lbr;
while (book != NULL) {
printf("Book%d:\n", count);
printf("书名: %s\n", book->title);
book = book->next;
count++;
}
}
// 释放内存
void release(struct Book **lbr) {
struct Book *temp;
while (*lbr != NULL) {
temp = *lbr;
*lbr = (*lbr)->next;
free(temp);
}
}
int main(void) {
struct Book *lbr;
int ch;
while (1) {
printf("请问是否需要录入书籍信息(Y/N):");
do {
ch = getchar();
} while (ch != 'Y' && ch != 'N');
if (ch == 'Y') {
addBook(&lbr);
} else {
break;
}
}
printf("请问是否需要打印书籍信息(Y/N):");
do {
ch = getchar();
} while (ch != 'Y' && ch != 'N');
if (ch == 'Y') {
printlbr(lbr);
}
release(&lbr);
return 0;
}
if(*lbr != NULL)
{
temp = *lbr;
*lbr = book;
book = temp;
}
->
if(*lbr != NULL)
{
temp = *lbr;
*lbr = book;
book->next = temp;
}
修改如下,改动处见注释,供参考:
#include<stdio.h>
#include<stdlib.h>
struct Book{
char title[128];
struct Book* next;
};
void addBook(struct Book** lbr);
void getInput(struct Book* book);
void printlbr(struct Book* lbr);
void release(struct Book** lbr);
void addBook(struct Book** lbr)
{
struct Book* book, * temp;
book = (struct Book*)malloc(sizeof(struct Book));
if (book == NULL)
{
printf("内存分配失败\n");
exit(1);
}
book->next = NULL; // 修改
getInput(book);
if ((*lbr) != NULL)
{
temp = *lbr;
*lbr = book;
book->next = temp; // book = temp; 修改
}
else
{
*lbr = book;
//book->next = NULL; 修改
}
}
void getInput(struct Book* book)
{
printf("请输入书名:");
scanf("%s", book->title);
}
void printlbr(struct Book* lbr)
{
struct Book* book;
int count = 1;
book = lbr;
while (book != NULL)
{
printf("Book%d:\n", count);
printf("书名: %s\n", book->title);
book = book->next;
count++;
}
}
void release(struct Book** lbr)
{
struct Book* temp;
while ((*lbr) != NULL) //while (lbr != NULL) 修改
{
temp = *lbr;
*lbr = (*lbr)->next;
free(temp);
}
}
int main(void)
{
struct Book* lbr = NULL; //struct Book* lbr; 修改
int ch;
while (1)
{
printf("请问是否需要录入书籍信息(Y/N):");
do
{
ch = getchar();
} while (ch != 'Y' && ch != 'N');
if (ch == 'Y')
{
addBook(&lbr);
}
else
{
break;
}
}
printf("请问是否需要打印书籍信息(Y/N):");
do
{
ch = getchar();
} while (ch != 'Y' && ch != 'N');
if (ch == 'Y')
{
printlbr(lbr);
}
release(&lbr);
return 0;
}
有几个问题
1、main函数里面的 struct Book *lbr; 没有初始化
struct Book *lbr = NULL;
2、根据releas函数,目的是要在链表尾部插入,而addBook函数做法错误,因为lbr总是存头部。
可以每次插入时遍历,或者用static变量保存链表尾部
void addBook(struct Book **lbr)
{
struct Book *book,*temp;
static struct Book *tail = NULL;
book = (struct Book *)malloc(sizeof(struct Book));
if(book == NULL)
{
printf("内存分配失败\n");
exit(1);
}
getInput(book);
if(*lbr != NULL)
{
tail->next = book;
book->next = NULL;
tail = book;
}
else
{
*lbr = tail = book;
book->next = NULL;
}
}
3、release 函数有误
while(lbr != NULL)
要改成
while(*lbr != NULL)
【相关推荐】
包含标准输入输出头文件(standard input output.head),包含printf函数