mccormickt12
mccormickt12

Reputation: 575

Trying to make linkedlist in C

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

Upvotes: 0

Views: 2205

Answers (4)

ChuckCottrill
ChuckCottrill

Reputation: 4444

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

Upvotes: 0

Farouq Jouti
Farouq Jouti

Reputation: 1667

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);
     }   
}

Upvotes: 2

lurker
lurker

Reputation: 58244

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;.

Upvotes: 4

awkwardChair
awkwardChair

Reputation: 31

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.

Upvotes: 1

Related Questions