贴上原版代码
/* films2.c -- 使用结构链表 */
#include <stdio.h>
#include <stdlib.h> /* 提供malloc()原型 */
#include <string.h> /* 提供strcpy()原型 */
#define TSIZE 45 /* 储存片名的数组大小 */
struct film {
char title[TSIZE];
int rating;
struct film* next; /* 指向链表中的下一个结构 */
};
char* s_gets(char* st, int n);
int main(void)
{
struct film* head = NULL;
struct film* prev ;
struct film *current;
char input[TSIZE];
/* 收集并储存信息 */
puts("Enter first movie title:");
while (s_gets(input, TSIZE) != NULL && input[0] != '\0')
{
current = (struct film*)malloc(sizeof(struct film));
if (head == NULL) /* 第1个结构 */
head = current;
else /* 后续的结构 */
prev->next = current;
current->next = NULL;
strcpy(current->title, input);
puts("Enter your rating <0-10>:");
scanf("%d", ¤t->rating);
while (getchar() != '\n')
continue;
puts("Enter next movie title (empty line to stop):");
prev = current;
}
/* 显示电影列表 */
if (head == NULL)
printf("No data entered. ");
else
printf("Here is the movie list:\n");
current = head;
while (current != NULL)
{
printf("Movie: %s Rating: %d\n",
current->title, current->rating);
current = current->next;
}
/* 完成任务,释放已分配的内存 */
current = head;
while (current != NULL)
{
current = head;
head = current->next;
free(current);
}
printf("Bye!\n");
return 0;
}
char* s_gets(char* st, int n)
{
char* ret_val;
char* find;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
find = strchr(st, '\n'); // 查找换行符
if (find) // 如果地址不是 NULL,
*find = '\0'; // 在此处放置一个空字符
else
while (getchar() != '\n')
continue; // 处理剩余输入行
}
return ret_val;
}
vscode或在linux下的gcc编译都会报错,如下
解决方法:
在vscode中可以看到如下的报错,很明显是释放内存时错了
current = head;
while (current != NULL)
{
current = head;
head = current->next;
free(current);
}
这几条语句咋一看好像是对的,将current指向头,将它的next放入在这里可以看做中间变量head中,然后再释放current的内存空间,但是忽略了一个问题,释放了当前current空间后就没有了,而循环到while下的第一条语句current=head;时没有空间属于非法调用了,所以最简单的方法就是在最下面加一条current=head;就可以了,但是没必要赋值两次那么将第一条删除即可,如下所示。
current = head;
while (current != NULL)
{
head = current->next;
free(current);
current = head;
}
当然将head换成prev也可以解决。
我改完之后看是不是我的文档有问题去百度搜索的时候,看到的很多人的版本是这样的
current = head;
while (current != NULL)
{
free(current);
current = current->next;
}
这都没关系,按上面的修改即可。
代码如下:
#include <stdio.h>
#include <stdlib.h> /* 提供malloc()原型 */
#include <string.h> /* 提供strcpy()原型 */
#define TSIZE 45 /* 储存片名的数组大小 */
struct film {
char title[TSIZE];
int rating;
struct film* next; /* 指向链表中的下一个结构 */
};
char* s_gets(char* st, int n);
int main(void)
{
struct film* head = NULL;
struct film* prev ;
struct film *current;
char input[TSIZE];
/* 收集并储存信息 */
puts("Enter first movie title:");
while (s_gets(input, TSIZE) != NULL && input[0] != '\0')
{
current = (struct film*)malloc(sizeof(struct film));
if (head == NULL) /* 第1个结构 */
head = current;
else /* 后续的结构 */
prev->next = current;
current->next = NULL;
strcpy(current->title, input);
puts("Enter your rating <0-10>:");
scanf("%d", ¤t->rating);
while (getchar() != '\n')
continue;
puts("Enter next movie title (empty line to stop):");
prev = current;
}
/* 显示电影列表 */
if (head == NULL)
printf("No data entered. ");
else
printf("Here is the movie list:\n");
current = head;
while (current != NULL)
{
printf("Movie: %s Rating: %d\n",
current->title, current->rating);
current = current->next;
}
/* 完成任务,释放已分配的内存 */
current = head;
while (current != NULL)
{
// current = head;
head = current->next;
free(current);
current = head;
}
printf("Bye!\n");
return 0;
}
char* s_gets(char* st, int n)
{
char* ret_val;
char* find;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
find = strchr(st, '\n'); // 查找换行符
if (find) // 如果地址不是 NULL,
*find = '\0'; // 在此处放置一个空字符
else
while (getchar() != '\n')
continue; // 处理剩余输入行
}
return ret_val;
}