0

In my c program I creating a singly linked list where we have to insert a node using the prev node, I'm getting this error in my main() function:

Dereferencing pointer to incomplete type ‘struct Node’

I am not sure why is it giving that error.

while((temp->next != NULL) && (temp->next->data < randomNumData)) -- error here

typedef struct _Node
{       
        int data;
        struct Node *next;
} ListNode;

ListNode *newList();
ListNode *insertNode(ListNode *prev, int data);

int main()
{
        ListNode *head = newList();

        int randomNumData;
        ListNode *temp = head;
        int i;

        for(i = 0; i < 11; i++)
        {
                randomNumData = random()%1001;
                while((temp->next != NULL) && (temp->next->data < randomNumData))
                {
                        temp = temp->next;
                }
                temp->data = randomNumData;
                head = insertNode(temp, randomNumData);
        }

        printList(head);
}

// returning the head of a new list using dummy head node
ListNode *newList()
{
        ListNode *head;
        head = malloc(sizeof(ListNode));
        if(head == NULL)
        {
                printf("ERROR");
                exit(1);
        }

        head->next = NULL;
        return head;
}

ListNode *insertNode(ListNode *prev, int data)
{
        ListNode *temp = prev;
        prev->next = temp->next;
        temp->next->data = data;

        return temp;
}
13
  • 2
    Where do you define ListNode and struct Node? Commented Oct 27, 2017 at 13:29
  • Please provide an example which builds. Where is the definition of ListNode? Commented Oct 27, 2017 at 13:29
  • 2
    struct Node *next; --> struct _Node *next; Commented Oct 27, 2017 at 13:32
  • Sorry, I added the struct for ListNode Commented Oct 27, 2017 at 13:32
  • You've declared struct Node *next; but there is no struct Node defined anywhere. Commented Oct 27, 2017 at 13:34

2 Answers 2

2

You are missing underscore when declaring next.

typedef struct _Node
{       
    int data;
    struct _Node *next;
} ListNode;
Sign up to request clarification or add additional context in comments.

Comments

0

It looks like you don't have a struct called Node. Import it from other file or just implement it. C is good but it's not a magician so far.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.