【发布时间】: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