【问题标题】:Read integers into linked list将整数读入链表
【发布时间】:2014-02-24 06:12:27
【问题描述】:

这是一个班级项目。 (C语言)

我遇到了需要实施的first function 问题。这是一个加载函数,我在其中获取输入文件名,打开用户在运行时提供的文件,然后从每行一个整数的文件中将其读取为链表格式。

我坚持使用how to create the linked list,因此它们实际上都是链接的,没有same node in the list being overwritten each time

这是我到目前为止所拥有的,我知道这是不正确的。

#include"sortingheaders.h"
#include <stdio.h>
#include<stdlib.h>
#include<time.h>

///////////////////////////////
Node* List_Create(Node * ln)
{
  if(ln==NULL)
    { 
   ln = malloc(sizeof(Node));
      ln->value = 0;
      ln->next = NULL;
    }
  return ln;
}

/////////////////////////////////////////////////////////////////////////////
Node* Load_File(char *Filename)
{
  //Open file
  FILE* fptr = fopen(Filename, "r");
  Node* ln=NULL;
  Node* temp=NULL;
  long int *x = 0;
//Validity Check, return 0 if unsuccesful
  if(fptr ==NULL)
    {printf("File didnt open!"); return 0;}
 ln= List_Create(ln);
  while(!feof(fptr))
    {
      fscanf(fptr,"%li",x);
      ln->value = *x;
      ln->next = List_Create(temp);
      return(ln);
    }

【问题讨论】:

  • 你至少想要long int xln-&gt;value = x
  • 你从循环内部返回,你没有检查 fscanf 是否工作。
  • 只需在调用 List_Create() 之前将 temp=NULL 添加到 while 循环中,这样节点就不会重叠。为什么你把 return() 放在循环中?
  • 我没有像那样把它放在循环中,转移我的格式有点混乱,有人把它编辑成那样,它被批准了。所以我需要做的就是让 temp=null 让它被初始化??感谢您谈论 long int x 和检查我的 fscanf

标签: c linked-list


【解决方案1】:

在 while 循环的第一个添加这个。

    temp = NULL;

【讨论】:

    猜你喜欢
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多