Trying to make linkedlist in C

后端 未结 4 903
余生分开走
余生分开走 2021-01-27 19:43

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         


        
相关标签:
4条回答
  • 2021-01-27 20:21

    here's a corrected version of your program :

    #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);
         }   
    }
    
    0 讨论(0)
  • 2021-01-27 20:21

    in makeList change

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

    to

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

    in the original you were giving the adresses of the int variables instead of the linkedList nodes. also, you had a variable 'first' which was never declared, that's where one of errors were taking place.

    also try declaring all your variables first, it makes it easier to read.

    0 讨论(0)
  • 2021-01-27 20:37

    The C compiler doesn't have a complete typedef of linkedList before you attempt to use it in your struct. You have a couple of options:

    typedef struct linkedList
    {
        int first;
        struct linkedList* rest;
    } linkedList;
    

    Or:

    typedef struct linkedList linkedList;  // C allows this forward declaration
    
    struct linkedList
    {
        int first;
        linkedList* rest;
    };
    

    This is your starting point.

    Additional problems include but are not limited to:

    • Your makeList function refers to variable first but it doesn't appear to be defined anywhere.
    • ll->rest = newL; assigned a type linkedList to a pointer to linkedList (linkedList *) you can't assign a value to a pointer-to-value. The compiler error message linked.c:43:13:... states this. It would need to be ll->rest = &newL;... HOWEVER...
    • newL is LOCAL to the function addToList, so you can't assign it's address to a persistent list item since it will go out of scope when the code leaves that block.
    • In addToList you are assigning pointer to integer to a variable that holds pointer to linkedList, e.g., second.rest = &c;.
    0 讨论(0)
  • 2021-01-27 20:37

    A few observations,

    • declare a struct name so that you can use it in the linkedList struct.
    • DRY - Don't Repeat Yourself, that is why the below ListNew() function is provided
    • use pointers, that is the whole point to building a linked list anyway,
    • your list uses one type of node, storing data and the list pointer,
    • name the pointer to the next node in the list whatever you want, how about 'next'?
    • name the thing that holds data anything you want, how about 'data'?
    • print the list, it will help figure out what is going on, :-)
    • a pointer can be printed in hexadecimal using the %x print format

    Anyway, here is a single linked list, without keeping track of the tail of the list, or counting the elements.

    #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;
    }
    
    0 讨论(0)
提交回复
热议问题