让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
问题描述:给出的代码中存在哪些问题?如何改正?请结合代码和注释进行回答。
问题分析:
- 缺少头文件声明 代码中缺少头文件的声明,应该加上以下头文件:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
- 结构体书籍信息和借书人信息不匹配 代码中定义的结构体中,书籍信息和借书人信息混淆在了一起,应该单独定义。
- 代码缺少主函数内容 代码中只有定义的函数,缺少主函数的内容。
- 定义链表节点时不应该使用指向下一个节点的指针,而应该使用指向数据的指针 定义链表节点时,应该使用指向数据的指针,而不是指向下一个节点的指针。因为链表的节点是用来存储数据的,而不是存储指针。
- 代码中有内存泄漏问题 代码中定义了新的节点,但是没有释放内存,而是一直累加,会出现内存泄漏问题。
- 链表删除节点的代码存在问题 链表删除节点的代码存在问题,应该先找到要删除的节点,然后再进行删除。 问题解决: 以下是修改后的代码,注释中展示了修改的地方。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
// 定义书籍信息
struct Book {
char bookNum[20];
char bookName[20];
char authorName[20];
char pressName[20];
char Class[20];
int totalNum;
int nowNum;
float bookPrice;
int heat;
};
// 定义借书人信息
struct Resident {
char booknum[20];
char residentname[20];
char housenumber[20];
char cellphone[20];
};
// 链表节点,指向数据
struct Node {
Book book;
struct Node* pNext;
};
// 链表节点,指向数据
struct ResidentNode {
Resident resident;
struct ResidentNode* pNext;
};
// 添加新的书籍信息
Node* AddBook(Node** ppStart, Book x) {
Node* New = (Node*)malloc(sizeof(Node));
New->pNext = NULL;
New->book = x;
if (*ppStart == NULL) {
*ppStart = New;
}
else {
Node* tail = *ppStart;
while (tail->pNext != NULL) {
tail = tail->pNext;
}
tail->pNext = New;
}
return New;
}
// 创建新的借书人信息
ResidentNode* CreateResidentNode(Resident x) {
ResidentNode* New = (ResidentNode*)malloc(sizeof(ResidentNode));
New->pNext = NULL;
New->resident = x;
return New;
}
// 添加新的借书人信息
void AddResidentNode(ResidentNode** ppStart, Resident x) {
ResidentNode* New = CreateResidentNode(x);
if (*ppStart == NULL) {
*ppStart = New;
}
else {
ResidentNode* tail = *ppStart;
while (tail->pNext != NULL) {
tail = tail->pNext;
}
tail->pNext = New;
}
}
// 删除借书人信息
void DeleteResidentNode(ResidentNode** ppStart, char* booknum) {
ResidentNode* pos = *ppStart, *pre = NULL;
while (pos != NULL && strcmp(pos->resident.booknum, booknum) != 0) {
pre = pos;
pos = pos->pNext;
}
if (pos == NULL) {
printf("未查到相关信息\n");
}
else if (pre == NULL) {
*ppStart = (*ppStart)->pNext;
free(pos);
}
else {
pre->pNext = pos->pNext;
free(pos);
}
}
// 通过编号查找借书人信息
ResidentNode* SearchResidentNode(ResidentNode** ppStart, char* booknum) {
ResidentNode* pos = *ppStart;
while (pos != NULL && strcmp(pos->resident.booknum, booknum) != 0) {
pos = pos->pNext;
}
return pos;
}
void BookBorrowing() {
Resident x;
printf("请输入借出的图书编号: ");
scanf("%s", x.booknum);
printf("\n");
printf("请输入借书人的姓名: ");
scanf("%s", x.residentname);
printf("\n");
printf("请输入借书人的门牌号: ");
scanf("%s", x.housenumber);
printf("\n");
printf("请输入借书人的电话号码: ");
scanf("%s", x.cellphone);
printf("\n");
}
int main() {
Node* pStart = NULL;
ResidentNode* pHead = NULL;
Book book = { "001", "三体", "刘慈欣", "重庆出版社", "科幻", 20, 20, 50.0, 10 };
AddBook(&pStart, book);
Resident resident = { "001", "张三", "123", "12345678901" };
AddResidentNode(&pHead, resident);
BookBorrowing();
DeleteResidentNode(&pHead, "001");
ResidentNode* pos = SearchResidentNode(&pHead, "001");
if (pos != NULL) {
printf("姓名:%s\n", pos->resident.residentname);
printf("门牌号:%s\n", pos->resident.housenumber);
printf("电话号码:%s\n", pos->resident.cellphone);
}
return 0;
}