ERROR IN LINKED LIST

#include<stdio.h>
#include<malloc.h>
struct data
{ int x;
struct data *link;
}x;

typedef struct data NODE;
NODE*head=NULL;
NODE*curr=NULL;

void insert(int val);
void print();
void del();

int main()
{ int i,j,val;
  printf("How many nos you want to enter\n");
  scanf("%d",&j);

  for(i=0;i<j;i++)
  { scanf("%d",&val);
  insert(val);
  }

  print();
 // del();
  _getch();
  _getch();
}
void insert (int val)
{ NODE*ptr=(NODE*)malloc(sizeof(NODE));
  if(ptr==NULL)
  {printf("allocation failed\n");
  return;
  }
  ptr->x=val;
  if(curr==NULL)
  { head=ptr;
    curr=ptr;
    ptr->link=NULL;
  }
  else
	  curr->link=ptr;
      curr=ptr;
	  ptr->link=NULL;
	  free(ptr);
}
void print()
{ NODE*ptr=(NODE*)malloc(sizeof(NODE));
  if(ptr==NULL)
  {printf("allocation failed\n");
  return;
  }
  int ex;
  ptr=head;
  while(ptr!=NULL)
  {   ex=ptr->x;
	  printf("%d\n",ex);

	  ptr=ptr->link;
  }
  
}
void del()
{ NODE*ptr=(NODE*)malloc(sizeof(NODE));
  if(ptr==NULL)
  {printf("allocation failed\n");
  return;
  }
  while(head!=curr)
  { printf("value to be deleted is %d \n",head->x);
    head=head->link;
  }
  if(head==curr)
  {printf("last data to be deleted is %d ",head->x);
   head=curr=NULL;
  }
  
}

It would be better if you edit the code in readable format. It is looking very complex.

syantax error,it will be like that
NODE *head=NULL,NODE *curr=NULL;
NODE *ptr=NODEmalloc(sizeof(NODE));

You are missing curly braces for the else block in your insert function. It should look like

void insert(int val) {
    ...
    ...
    ...
    else {
        curr->link = ptr;
        curr = ptr;
        ptr->link = NULL;
        // You should not free this here. This means, you are de-allocating
        // the memory you just allocated. That should be done in the del function
        // free(ptr);
    }
}

And your del is all wrong.

  1. You need not malloc any memory in del
  2. You need to actually free the memory inside this function. What you are doing is traversing through the whole list and printing the values, and then just setting head to NULL. The original allocated memory is still allocated. Only, you cant get to that anymore and/or use it.

Try to improve using these ideas on what is going wrong, and using the ideas you have. If you are getting stuck somewhere, we all at codechef are happy to help.

The original statements are syntactically right. It is the statements in this answer that is wrong in syntax.