【发布时间】:2011-09-20 12:23:32
【问题描述】:
我认为我对链表的工作方式存在一些问题,请记住,我不是 C 专家,而且我以前没有使用过链表。
我正在尝试获取包含事物列表的文本文件并将其存储在链接列表中。我现在有以下代码:
typedef struct linked_list {
struct linked_list *next_ptr;
char name;
float price1;
float price2;
}linked_list;
struct linked_list *first_ptr;
char name_temp;
int writeList(void) {
// read input files
FILE *sh_list;
sh_list=fopen("case1/shoppingList.dat", "rw");
if (sh_list == NULL) {
perror("Cannot open file, you seem to have something mixed up...");
exit(8);
}
struct linked_list *current_ptr;
current_ptr = first_ptr;
while ((fscanf(sh_list, "%s", &name_temp)) !=EOF) {
next_ptr = malloc(sizeof(linked_list));
strcpy(name, name_temp);
//move to next node and complete the same task
}
};
我在 //move... 处停了下来,因为我正在努力让代码正确 - 我的 IDE 给了我错误。同样,我无法让它读取我需要执行的变量“name”,以便将字符串复制到节点。
【问题讨论】:
-
始终指定您遇到的错误。
-
我要修复的错误是它说“next_ptr”是函数 writeList 中的一个未识别变量。除此之外,我一直在寻找一些建议,以找到正确的方向,以便将这个东西存储在一个链表中。
-
您可能需要一个数组作为名称:
char name[36];或附近(选择您的大小)。这需要在结构中;您还在函数中引用了一个变量name,但没有提供它的定义;你还有char name_temp;,它应该(a)在函数内部,(b)也应该是一个数组。您应该在格式中指定数组的大小减一(例如"%35s")。 -
不要在函数定义末尾的大括号后放置分号。
标签: c data-structures linked-list