【问题标题】:How to use malloc to insert a node in a linked list?如何使用 malloc 在链表中插入节点?
【发布时间】:2012-01-10 03:13:06
【问题描述】:

我正在编写一个程序,它调用 main 中的一个函数,该函数创建一个节点来构建一个链表。该程序从文件中读取要放置在节点中的字符。该程序运行良好,直到它进入创建节点的函数。我不确定是否存在逻辑或语法错误或其他错误。很抱歉篇幅太长,但错误离开始不远了。另外,如果你想要我的头文件,请告诉我。代码来自我的 C 书籍:计算机科学:使用 C 的结构化编程方法,第三版。任何帮助(以及对我的错误的解释)将不胜感激!

谢谢!

这里是 main.c:

#include "my.h"

int main (int argc, char* argv[])
{
     int y;
     NODE* pList;
     NODE* pPre;
     NODE* pNew;
     DATA item;
     FILE* fpntr;
     int closeResult;

     pList = NULL;         

     fpntr = fopen(argv[1], "r");                            // open file 
        if(!fpntr)
            {
                 printf("Could not open input file.\n");
                 exit (101);
            }
        else printf("The file opened.\n");


printf("starting InNode.\n");                                 //testing to see if this is working

    while((y = fscanf(fpntr, "%c", &(item.key))) != EOF)
           pList = InNode(pList, pPre, item);

printf("end InNode.\n");                                      //testing to see if this is working, doesn't get this far

printf("starting printme.\n");


    printme(pList);


printf("end printme.\n");

     closeResult = fclose(fpntr);                             //close file 
        if(closeResult == EOF)
            {
                 printf("Could not close input file.\n");
                 exit (102);
            }
        else printf("The file closed.\n");

     free(a);
     return 0;
}

这里是 InNode.c(直接来自我的书):

#include "my.h"

NODE* InNode (NODE* pList, NODE* pPre, DATA item)

{
    NODE* pNew;

printf("start malloc.\n");                                   //testing to see if this is working

    if (!(pNew = (NODE*) malloc(sizeof(NODE))))
         printf("Memory overflow in insert.\n");
         exit(100);


printf("malloc complete.\n");                                //testing to see if this is working, doesn't get this far


    pNew->data = item;

printf("start if statement.\n");

    if (pPre == NULL)
        {
            pNew->link = pList;
            pList = pNew;
        }
     else
        {
            pNew->link = pPre->link;
            pPre->link = pNew;
        }

printf("end else statement.\n");

     return pList;
}

【问题讨论】:

    标签: c linked-list malloc nodes


    【解决方案1】:

    你不见了{ }

    if (!(pNew = (NODE*) malloc(sizeof(NODE))))
    {
         printf("Memory overflow in insert.\n");
         exit(100);
    }
    

    【讨论】:

      【解决方案2】:

      我立即注意到的一个问题:

      if (!(pNew = (NODE*) malloc(sizeof(NODE))))
           printf("Memory overflow in insert.\n");
           exit(100);
      

      你有一个 if 语句,它的主体周围没有大括号,并且有两行缩进,就好像它们应该在 if 语句的主体中一样。只有第一行被解析为 if 语句的一部分;第二行,exit(100),是无条件发生的,所以无论如何你的程序都会退出。

      您可能应该将其更改为:

      if (!(pNew = (NODE*) malloc(sizeof(NODE)))) 
      {
           printf("Memory overflow in insert.\n");
           exit(100);
      }
      

      如果这不能解决您的问题,或者一般来说对于未来的问题,我建议您发布您获得的输出。如果您发布有关实际发生的事情的详细信息(例如输出、意外行为和您的预期等),而不是仅仅说它不适用,人们会更容易发现问题没有更多的细节。

      【讨论】:

      • 谢谢,这行得通(就像 initializig pPre 一样)。很抱歉遗漏了输出。下次我会做笔记!
      • @Piseagan 很高兴我能帮上忙!是的,初始化 pPre 也很重要。我还刚刚注意到您在说明它停止工作的位置发表了评论,但我没有看到该评论,因为它离右边太远了。所以,你确实发布了我正在寻找的信息(它死在哪里),但格式本来可以更好。
      【解决方案3】:

      一个问题是你在使用它的值之前没有初始化pPre

      【讨论】:

        猜你喜欢
        • 2021-11-30
        • 2018-07-28
        • 2021-05-23
        • 1970-01-01
        • 1970-01-01
        • 2016-06-28
        • 1970-01-01
        相关资源
        最近更新 更多