我正在尝试在C中创建一个链接列表的结构。我不确定会出现什么问题。我的错误是:
linked.c:6:2: error: unknown type name ‘linkedList’
linked.c: In function ‘makeList’:
linked.c:30:2: error: ‘first’ undeclared (first use in this function)
linked.c:30:2: note: each undeclared identifier is reported only once for each function it appears in
linked.c: In function ‘addToList’:
linked.c:36:9: error: used struct type value where scalar is required
linked.c:43:13: error: incompatible types when assigning to type ‘int *’ from type ‘linkedList’
如果有人能看到有什么问题并向我解释,我将不胜感激。我的代码如下。
#include <stdio.h>
typedef struct linkedList
{
int first;
linkedList* rest;
} linkedList;
linkedList makeList(int a, int b, int c);
void addToList(linkedList* ll, int a);
int main()
{
linkedList ll = makeList(1,3,5);
addToList(&ll, 7);
addToList(&ll, 9);
return 0;
}
linkedList makeList(int a, int b, int c)
{
linkedList ll;
ll.first = a;
linkedList second;
second.first = b;
linkedList third;
third.first = c;
third.rest = NULL;
second.rest = &c;
first.rest = &b;
return first;
}
void addToList(linkedList* ll, int a)
{
while (*ll)
{
if (ll->rest == NULL)
{
linkedList newL;
newL.first = a;
newL.rest = NULL;
ll->rest = newL;
break;
} else
{
continue;
}
}
}
答案 0 :(得分:4)
在您尝试在typedef
中使用它之前,C编译器没有完整的linkedList
struct
。你有几个选择:
typedef struct linkedList
{
int first;
struct linkedList* rest;
} linkedList;
或者:
typedef struct linkedList linkedList; // C allows this forward declaration
struct linkedList
{
int first;
linkedList* rest;
};
这是你的出发点。
其他问题包括但不限于:
makeList
函数是指变量first
,但似乎没有在任何地方定义。ll->rest = newL;
将<{1>}类型分配给指针至linkedList
(linkedList
)您无法为指针指定值 - 实现价值。编译器错误消息linkedList *
表明了这一点。它需要linked.c:43:13:...
......但是...... ll->rest = &newL;
是函数newL
的LOCAL,因此您无法将其地址分配给持久列表项,因为当代码离开该块时它将超出范围。addToList
中,您将整数指针指定给一个变量,该变量包含指向addToList
的指针,例如,linkedList
。答案 1 :(得分:2)
这是您程序的更正版本:
#include <stdio.h>
#include <stdlib.h>
typedef struct linkedList
{
int first;
struct linkedList* rest; // add struct in the beginning
} linkedList;
linkedList* addToList(linkedList* ll, int a);
void go_trough(linkedList *ll); // here's an extra function to check
int main()
{
linkedList *ll ; // working with a pointer is easier and makelist is pointless work with add to list instead
ll = NULL; // initialize to NULL
ll = addToList(ll, 7);
ll = addToList(ll, 9);
go_trough(ll);
return 0;
}
linkedList* addToList(linkedList* ll, int a) // I didn't understand what you were trying to do so ... here's my version
{
if(!ll)
{
ll = malloc(sizeof(linkedList*)); //allocating enought space to hold the structure
ll->first = a;
ll->rest = NULL;
}
else
ll->rest = addToList(ll->rest , a);
return ll;
}
void go_trough(linkedList *ll)
{
if(ll)
{
printf("%d\n" , ll->first);
go_trough(ll->rest);
}
}
答案 2 :(得分:1)
在makeList中更改
second.rest = &c;
first.rest = &b;
到
ll.rest = &second;
second.rest = &third;
在原始文件中,您提供了int变量的地址而不是linkedList节点。另外,你有一个从未声明过的变量'first',那是一个错误发生的地方。
还首先尝试声明所有变量,这样可以更容易阅读。
答案 3 :(得分:0)
一些观察,
无论如何,这里是一个链表,没有跟踪列表尾部或计算元素。
#include <stdio.h>
#include <stdlib.h>
typedef struct listnode
{
int data;
struct listnode* next;
} linkedList;
linkedList* makeList(int a, int b, int c);
void addToList(linkedList* ll, int a);
void ListPrint(linkedList* ll);
int main()
{
linkedList* ll = makeList(1,3,5);
addToList(ll, 7);
addToList(ll, 9);
ListPrint(ll);
return 0;
}
linkedList* ListNew(int a) //new linkedList node
{
linkedList* newL = (linkedList*)malloc(sizeof(linkedList));
newL->data = a;
newL->next = NULL;
return newL;
}
linkedList* makeList(int a, int b, int c)
{
linkedList* ll = ListNew(a);
addToList(ll, b);
addToList(ll, c);
return ll;
}
void addToList(linkedList* ll, int a)
{
if(!ll) return;
//find end of list
while (ll->next)
{
ll = ll->next;
}
ll->next = ListNew(a);
return;
}
void ListPrint(linkedList* ll) //print list
{
if(!ll) return;
linkedList* p;
for( p=ll; p; p=p->next )
{
printf("%x: %d\n",p,p->data);
}
return;
}