As others say: your error occurs because you have to include stdlib.h instead of alloc.h
To print your list, you must change p to insert . You are currently passing NULL every time you call insert . Change your code this way (pass a pointer to a pointer to insert ):
#include <stdio.h> #include <stdlib.h> struct node { int data; struct node * link; }; /* note **p instead of *p */ void insert (struct node **p, int d) { struct node *temp; temp = malloc(sizeof(struct node)); temp->data=d; temp->link=NULL; if(*p==NULL) { *p=temp; } else{ while((*p)->link!=NULL) *p=(*p)->link; (*p)->link=temp; } } void disp(struct node *p) { while(p!=NULL) { printf("%d\n",p->data); p=p->link; } } int main() { struct node *p; p=NULL; insert(&p,7); insert(&p,9); disp(p); }
and he will print
7 9
eckes
source share