C 为.txt文件中的每个条目创建一个新节点
好的,我一直在做一个程序,可以使用scanf(cmd输入重定向)读取txt文件的元素。必须为文件中的每个条目创建一个新节点,并将其添加到列表的末尾。以下是我目前的代码:C 为.txt文件中的每个条目创建一个新节点,c,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 }
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;
}
现在,我无法让程序正常运行。虽然没有运行时错误,但输出似乎是垃圾。为了这个,我已经工作了好几个小时了,但我的头脑还不能清醒过来。谢谢你的帮助 #包括
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Elem
{
int AtNum;
char Na[31];
char Sym[4];
};
struct nodeTag
{
/* entry must be a pointer in order to not lose the values
and/or encounter memory conflicting errors
*/
struct Elem *entry;
struct nodeTag *pNext;
};
typedef struct nodeTag Node;
// insert node at the first location
Node *insertFirst(Node *head, struct Elem *data)
{
Node *node = (Node *) malloc(sizeof(Node));
// fill in data
node->entry = data;
/* point it to old first node
in simple words: "put this node before the head"
*/
node->pNext = head;
// point first to new first node
head = node;
return head;
}
Node *InitializeList(int n)
{
int i;
Node *head = NULL;
struct Elem *data;
for (i = 0; i < n; i++)
{
// allocate memory for the struct Elem of each node
data = (struct Elem*) malloc(sizeof(struct Elem));
scanf("%d", &data->AtNum);
scanf("%s", data->Na);
scanf("%s", data->Sym);
head = insertFirst(head, data);
}
return head;
}
//display the list
void printList(Node *head)
{
Node *ptr = head;
printf("\nStatus of the linked list is:\n");
//start from the beginning
while(ptr != NULL)
{
printf("%d %s %s", ptr->entry->AtNum, ptr->entry->Na, ptr->entry->Sym);
printf("\n");
ptr = ptr->pNext;
}
}
int main(int argc, char *argv[])
{
Node *head;
head = InitializeList(3);
printList(head);
return 0;
}
#包括
#包括
结构元素
{
int-AtNum;
char-Na[31];
char-Sym[4];
};
结构节点
{
/*条目必须是指针才能不丢失值
和/或遇到内存冲突错误
*/
结构元素*条目;
结构节点tag*pNext;
};
typedef结构节点tag节点;
//在第一个位置插入节点
节点*insertFirst(节点*head,结构元素*data)
{
Node*Node=(Node*)malloc(sizeof(Node));
//填写数据
节点->条目=数据;
/*将其指向旧的第一个节点
简单地说:“将此节点放在头部之前”
*/
节点->pNext=头部;
//首先指向新的第一个节点
头部=节点;
回流头;
}
节点*初始值列表(int n)
{
int i;
Node*head=NULL;
结构元素*数据;
对于(i=0;iAtNum);
扫描频率(“%s”,数据->Na);
扫描频率(“%s”,数据->符号);
头=插入第一个(头,数据);
}
回流头;
}
//显示列表
无效打印列表(节点*头)
{
节点*ptr=头部;
printf(“\n链表的状态为:\n”);
//从头开始
while(ptr!=NULL)
{
printf(“%d%s%s”,ptr->entry->AtNum,ptr->entry->Na,ptr->entry->Sym);
printf(“\n”);
ptr=ptr->pNext;
}
}
int main(int argc,char*argv[])
{
节点*头;
head=初始值列表(3);
印刷品清单(标题);
返回0;
}
我希望我没有来得太晚!如果没有,请检查此答案作为解决方案,谢谢!:-) 您将它们按相反的顺序放置,因此,您必须从
InitializeList()
返回temp,而不是pffirst。并在上述行中给出temp->pNext=0代码>和pFirst=temp
是错误的。要小心;除非你在Windows上,否则它不一定能满足你的要求。嘿,伙计,既然我注意到你还没有回复,你能给我一个关于我答案的反馈吗?我随时准备帮助你
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Elem
{
int AtNum;
char Na[31];
char Sym[4];
};
struct nodeTag
{
/* entry must be a pointer in order to not lose the values
and/or encounter memory conflicting errors
*/
struct Elem *entry;
struct nodeTag *pNext;
};
typedef struct nodeTag Node;
// insert node at the first location
Node *insertFirst(Node *head, struct Elem *data)
{
Node *node = (Node *) malloc(sizeof(Node));
// fill in data
node->entry = data;
/* point it to old first node
in simple words: "put this node before the head"
*/
node->pNext = head;
// point first to new first node
head = node;
return head;
}
Node *InitializeList(int n)
{
int i;
Node *head = NULL;
struct Elem *data;
for (i = 0; i < n; i++)
{
// allocate memory for the struct Elem of each node
data = (struct Elem*) malloc(sizeof(struct Elem));
scanf("%d", &data->AtNum);
scanf("%s", data->Na);
scanf("%s", data->Sym);
head = insertFirst(head, data);
}
return head;
}
//display the list
void printList(Node *head)
{
Node *ptr = head;
printf("\nStatus of the linked list is:\n");
//start from the beginning
while(ptr != NULL)
{
printf("%d %s %s", ptr->entry->AtNum, ptr->entry->Na, ptr->entry->Sym);
printf("\n");
ptr = ptr->pNext;
}
}
int main(int argc, char *argv[])
{
Node *head;
head = InitializeList(3);
printList(head);
return 0;
}