c - 多个数据的链表中的无限循环
问题描述
我刚刚开始学习链表 C,我想从键盘读取有关用户指定数量的电影的信息,并按读取顺序打印它们。为什么我得到一个无限循环?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int year;
char title[30];
float budget;
struct node *nextptr;
}*stnode;
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int year, i;
char title[30];
float budget;
stnode = (struct node *)malloc(sizeof(struct node));
if(stnode == NULL)
{
printf("Memory can not be allocated.");
}
else
{
printf("Enter year for movie 1 : ");
scanf("%d", &year);
stnode->year = year;
stnode->nextptr = NULL;
tmp = stnode;
printf("Enter title for movie 1 : ");
scanf("%s", title);
strcpy(stnode->title,title);
stnode->nextptr = NULL;
tmp = stnode;
printf("Enter budget for movie 1 : ");
scanf("%f", &budget);
stnode->budget = budget;
stnode->nextptr = NULL;
tmp = stnode;
for(i = 2 ; i<= n; i++)
{
fnNode = (struct node *)malloc(n*sizeof(struct node));
if(fnNode == NULL)
{
printf("Memory can not be allocated.");
break;
}
else
{
printf("Enter year for movie %d : ", i);
scanf("%d", &year);
fnNode->year = year;
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
printf("Enter title for movie %d : ", i);
scanf("%s", title);
strcpy(fnNode->title, title);
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
printf("Enter budget for movie %d : ", i);
scanf("%f", &budget);
fnNode->budget = budget;
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
}
}
}
}
void displayList()
{
struct node *tmp;
if(stnode == NULL)
{
printf(" List is empty.");
}
else
{
tmp = stnode;
while(tmp != NULL)
{
printf("Year: %d | Title: %s | Budget: %f\n", tmp->year, tmp->title, tmp->budget);
tmp = tmp->nextptr;
}
}
}
int main()
{
int n;
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n);
printf("\n List of movies you entered : \n");
displayList();
return 0;
}
另外,当我想释放内存时,我应该在 main 中执行,还是在创建列表时在循环之后执行?谢谢!
解决方案
您链接fnNode
到,fnNode
因为:
printf("Enter year for movie %d : ", i);
scanf("%d", &year);
fnNode->year = year;
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
/* tmp is the same as fnNode here */
printf("Enter title for movie %d : ", i);
scanf("%s", title);
strcpy(fnNode->title, title);
fnNode->nextptr = NULL;
/* fnNode is connected to fnNode here */
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
你应该:
- 将所有
tmp
数据放入节点后,每个节点只修改一次。 n
只为一个节点分配一个元素,而不是元素。
它会是这样的:
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int year, i;
char title[30];
float budget;
stnode = malloc(sizeof(struct node));
if(stnode == NULL)
{
printf("Memory can not be allocated.");
}
else
{
printf("Enter year for movie 1 : ");
scanf("%d", &year);
stnode->year = year;
/* don't update tmp here */
printf("Enter title for movie 1 : ");
scanf("%s", title);
strcpy(stnode->title,title);
/* don't update tmp here */
printf("Enter budget for movie 1 : ");
scanf("%f", &budget);
stnode->budget = budget;
/* update tmp after putting all data */
stnode->nextptr = NULL;
tmp = stnode;
for(i = 2 ; i<= n; i++)
{
/* allocate for one node, not n nodes */
fnNode = malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf("Memory can not be allocated.");
break;
}
else
{
printf("Enter year for movie %d : ", i);
scanf("%d", &year);
fnNode->year = year;
fnNode->nextptr = NULL;
/* don't update tmp here */
printf("Enter title for movie %d : ", i);
scanf("%s", title);
strcpy(fnNode->title, title);
/* don't update tmp here */
printf("Enter budget for movie %d : ", i);
scanf("%f", &budget);
fnNode->budget = budget;
/* update tmp after putting all data */
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
}
}
}
}void createNodeList(int n)
{
struct node *fnNode, *tmp;
int year, i;
char title[30];
float budget;
stnode = malloc(sizeof(struct node));
if(stnode == NULL)
{
printf("Memory can not be allocated.");
}
else
{
printf("Enter year for movie 1 : ");
scanf("%d", &year);
stnode->year = year;
/* don't update tmp here */
printf("Enter title for movie 1 : ");
scanf("%s", title);
strcpy(stnode->title,title);
/* don't update tmp here */
printf("Enter budget for movie 1 : ");
scanf("%f", &budget);
stnode->budget = budget;
/* update tmp after putting all data */
stnode->nextptr = NULL;
tmp = stnode;
for(i = 2 ; i<= n; i++)
{
/* allocate for one node, not n nodes */
fnNode = malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf("Memory can not be allocated.");
break;
}
else
{
printf("Enter year for movie %d : ", i);
scanf("%d", &year);
fnNode->year = year;
fnNode->nextptr = NULL;
/* don't update tmp here */
printf("Enter title for movie %d : ", i);
scanf("%s", title);
strcpy(fnNode->title, title);
/* don't update tmp here */
printf("Enter budget for movie %d : ", i);
scanf("%f", &budget);
fnNode->budget = budget;
/* update tmp after putting all data */
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
}
}
}
}
另请注意,铸造结果malloc()
被认为是一种不好的做法。
推荐阅读
- google-cloud-platform - Google Cloud VPN - 从 GCP 到本地定价的出口说明
- php - 在登录表单上声明后,PHP 无法获取存储用户 ID 的会话
- php - 使用php在mysql中插入复选框数据
- javascript - JSON.parse() 从 PythonShell 字符串返回字符串
- rust - 我什么时候应该使用 bindgen crate 而不是 libloading?
- javascript - 使用 forEach 使用 push 方法创建另一个数组,但循环结束后数组为空
- content-management-system - 从浏览器编辑 Plone 中的 buildout.cfg
- python - 我需要帮助在将流行时间用作 Django REST API 以在 ionic 中使用
- azure - Azure Devops:即使在设置 NPM Authenticate 之后,也无法使用 NPM 私有注册表构建映像
- amazon-web-services - 如何在 dynamodb-local docker 实例中使用保存的 DynamoDBLocal.jar