Hi there. I am trying to add a node to linked list using function append(). It is giving me SegementViolation error at the highlighted lines. Does anybody have any clue as to what is wrong?
Code:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
};
int main(int argc, char **argv)
{
struct node *p;
int num;
p= (struct node *)malloc(sizeof(struct node));
if(p->link != NULL)
{
p= p->link;
}
printf("\n Enter num:");
scanf("%d", &num);
append(p,num);
printf("\n hello world\n");
return 0;
}
append(struct node **q, int num)
{
struct node *temp,*r;
printf("\n in append");
r = *q;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = num;
temp->link = NULL;
r->link = temp;
//Display linked list elements
temp = *q;
printf("\n Before while");
while(temp->link != NULL)
{
printf("%d",temp->data);
temp = temp->link;
}
}