-4

I have written this code using C and i am a new to programming world so please help me regarding my error. I am getting dereferencing pointer to incomplete type. what are the changes I need to make to run this code?

enter code here
#include <stdio.h>
#include <stdlib.h>
struct {
int data;
struct node* next;
};
struct node* head;

void Insert(int data, int n)
{
int i;
struct node* temp1, *temp2;
temp1 = (struct node*)malloc(sizeof(struct node));
temp1->data = data;
temp1->next = NULL;
if(n==1)
    {
        temp1->next = head;
        head = temp1;
        return;
    }

 temp2 = head;
  for(i=0; i<n-2; i++)
  {
      temp2 = temp2->next;
  }
  temp1->next = temp2->next;
  temp2->next = temp1;

}

void print()
{
struct node* temp = head;
while(temp != NULL)
{
    printf("%d", temp->data);
    temp = temp->next;
}
print("\n");
}

int main()
{
head = NULL;
Insert(2,1);
Insert(3,2);
Insert(4,1);
Insert(5,2);
Insert(1,3);
Print();
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    Possible duplicate of [dereferencing pointer to incomplete type error when using typedef struct in C](http://stackoverflow.com/questions/35607042/dereferencing-pointer-to-incomplete-type-error-when-using-typedef-struct-in-c) – Cyan Feb 25 '16 at 15:28

1 Answers1

2

You omitted tag name node in the structure definition

struct {
int data;
struct node* next;
};

Write instead

struct node {
       ^^^^ 
int data;
struct node* next;
};

Take into account that by the analogy with arrays it is better when positions in the list start from 0.

Also function main without parameters shall be declared like

int main( void )

and function print should be declared like

void print( void );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335