0

I am trying to make a struct in C that is a linked list. I am not really sure what is going wrong though. My errors are:

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’

if anybody can see what is wrong and explain it to me, it would be much appreciated. My code is below.

#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;
            }
    }
}
4

4 回答 4

4

在您尝试typedef在. 你有几个选择:linkedListstruct

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;将类型分配给linkedList指向(linkedList )的指针,linkedList *您不能将值分配给指向值的指针。编译器错误消息linked.c:43:13:...说明了这一点。它需要是ll->rest = &newL;......但是......
  • newL是函数的 LOCAL addToList,因此您不能将其地址分配给持久列表项,因为当代码离开该块时它将超出范围。
  • addToList您将指向整数的指针分配给保存指向 的指针的变量时linkedList例如second.rest = &c;
于 2013-10-02T19:18:35.300 回答
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);
     }   
}
于 2013-10-02T19:58:37.033 回答
1

在makeList改变

second.rest = &c;
first.rest = &b;

ll.rest = &second;
second.rest = &third;

在原始版本中,您提供的是 int 变量的地址,而不是 linkedList 节点。另外,您有一个从未声明过的变量“first”,这就是发生错误的地方。

还可以尝试先声明所有变量,这样更容易阅读。

于 2013-10-02T19:27:14.993 回答
0

几点观察,

  • 声明一个结构名称,以便您可以在linkedList 结构中使用它。
  • DRY - 不要重复自己,这就是提供以下 ListNew() 函数的原因
  • 使用指针,这就是构建链表的重点,
  • 您的列表使用一种类型的节点,存储数据和列表指针,
  • 随意命名指向列表中下一个节点的指针,'next'怎么样?
  • 将保存数据的东西命名为您想要的任何东西,“数据”怎么样?
  • 打印列表,这将有助于弄清楚发生了什么,:-)
  • 可以使用 %x 打印格式以十六进制打印指针

无论如何,这里是一个单链表,没有跟踪链表的尾部,也没有计算元素。

#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;
}
于 2013-10-03T07:42:32.977 回答