[英]c linked list read from txt file
I'm pretty new in c and want to make a program that can read linked list from a txt file. 我是c语言的新手,想开发一个可以从txt文件读取链表的程序。 For example, my file is something like
例如,我的文件是这样的
0 1 6 4 8 6 8 15 56 4 864 68
and I want to read the info and then try display it to user. 我想阅读信息,然后尝试将其显示给用户。 This is what I have made.
这就是我所做的。 This gives me error that expected expression before head it writes it in main part where I declared.
这给了我错误,它期望表达式在将其写在我声明的主要部分之前。
A = readList(head);
printList(head);
My code: 我的代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct linkedList{
int value;
struct linkedList *next;
} linkedList, head;
linkedList *readList(linkedList *head)
{
FILE *dataFile;
dataFile = fopen("duom.txt", "r");
if(dataFile == NULL) {
printf("Nepasisekė atidaryti failo\n");
} else {
printf("Duomenų failą pavyko atidaryti\n");
}
while (!feof (dataFile))
if (head == NULL) {
head = malloc(sizeof(linkedList));
fscanf(dataFile, "%d", &head -> value);
head -> next = NULL;
} else {
struct linkedList *current = head;
struct linkedList *temp = malloc(sizeof(linkedList));
while(current -> next != NULL) {
current = current -> next;
}
fscanf(dataFile, "%d", &temp -> value);
current -> next = temp;
temp -> next = NULL;
}
return head;
}
void printList(linkedList *head)
{
linkedList *current = head;
while (current != NULL) {
printf("%d->", current -> value);
current = current -> next;
}
printf("NULL\n");
return;
}
int main()
{
linkedList A;
A=readList(head);
printList(head);
return 0;
}
You are allocating space for head in readList, yet you're passing some un-existing parameter there (readList(head) and printList(head)). 您在readList中为head分配了空间,但您在那里传递了一些不存在的参数(readList(head)和printList(head))。 What you could do to fix your issue is: Change your main to:
您可以采取以下措施解决此问题:将主要地址更改为:
int main()
{
linkedList *A = NULL; /* creating pointer to your linked list. */
A=readList(A); /* Read linked list from file (And allocate it if it's not allocated */
printList(A); /* print list */
return 0;
}
If you want A to be globally acessible just move declaration of pointer A outside of main. 如果要使A在全局范围内可用,只需将指针A的声明移到main之外。
To answer your problem that is 要回答你的问题是
this writes me error that expected expression before head it writes it in main part where I declare A = readList(head);
这给我写了一个错误,它期望的表达式在head之前就写在了我声明A = readList(head)的主要部分中; printList(head);
printList(head);
You should change from 您应该从
linkedList A;
A=readList(head);
to 至
linkedList* A;
A=readList(head);
It should solve the issue you are describing. 它应该可以解决您正在描述的问题。
I think there is a problem about your typedef
我认为您的
typedef
有问题
typedef are a part of operations which are made at the pre-proccessing
typedef是在
pre-proccessing
中进行的操作的一部分
It will replace all the #define and typedefs inyour code by their oponent. 它将替换它们中所有#define和typedefs代码。
So your double typedef typedef struct Linkedlist { .... } linkedList, head;
因此,您的double typedef
typedef struct Linkedlist { .... } linkedList, head;
create 2 new type wich are linkedList and head. 创建2个新类型,其中linkedList和head。
When u write the word head
in your code it will be replaced by struct Linkedlist
so there is an error in your prototype after de pre-processing operation because the end statment will be Linkedlist *struct linkedlist
当您在代码中写单词
head
时,它将被struct Linkedlist
替换,因此在进行预处理操作后,原型struct Linkedlist
出现错误,因为最终陈述将为Linkedlist *struct linkedlist
(the prototype of your read list function) (您的读取列表功能的原型)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.