为.txt文件中的每个条目创建一个新节点

时间:2017-03-12 09:48:06

标签: c

好的,所以我一直在做一个程序,它会使用scanf(cmd输入重定向)读取txt文件的元素。必须为文件中的每个条目创建一个新节点,并将其添加到列表的末尾。到目前为止,这是我的代码:

struct Elem{
    int Atnum;
    char Na[31];
    char Sym[4]; 
};


struct nodeTag {
    struct Elem entry; 
    struct nodeTag *pNext; // pointer to the next node
};

typedef struct nodeTag Node;

初始化它的函数是:

Node *
InitializeList(Node *pFirst, int n)
{
    int i;
    Node *head, *temp = 0;

   pFirst = 0;

   for (i=0; i<n; i++){
        head  = (Node *)malloc(sizeof(Node));
        scanf("%d", &head->entry.AtNum); 
        scanf("%s", head->entry.Na);
        scanf("%s", head->entry.Sym);

        if (pFirst != 0)
        {
            temp->pNext = head;
            temp = head;
        }
        else
        {
            pFirst = temp = head;
        }
        fflush(stdin); 
        temp->pNext = 0;
    }

    return pFirst;
}

最后,打印

void
Print( Node *pFirst )
{
    Node *temp;
    temp = pFirst;
    printf("\n status of the linked list is\n");
    while (temp != 0)
    {
        printf("%d %s %s", temp->entry.AtNum, temp->entry.Na, temp->entry.Sym);
        temp = temp -> pNext;
    }

}

现在,我无法让程序正常运行。虽然没有运行时错误,但输出似乎是垃圾。我已经为此工作了几个小时,我无法理解它。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

build.gradle

我希望我来得太晚了!如果没有,请检查此答案作为解决方案,谢谢! :-)