首页 > 解决方案 > 多个数据的链表中的无限循环

问题描述

我刚刚开始学习链表 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 中执行,还是在创建列表时在循环之后执行?谢谢!

标签: c

解决方案


您链接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() 被认为是一种不好的做法


推荐阅读