Segmentation Fault (core dumped)
i am new to this language kindly can anyone help me with this code.
Code:
#include<stdio.h>
struct node{
int data;
struct node *lft;
struct node *rgt;
};
struct node * cons(struct node *p,int a)
{if(p==NULL)
{p=(struct node *)malloc(sizeof(struct node));
p->data=a;
p->lft=NULL;
p->rgt=NULL;
}
else
{if(a<p->data)
p->lft=cons(p->lft,a);
else if(a>p->data)
p->rgt=cons(p->rgt,a);
}
return p;
}
int preorder(struct node *p)
{struct node *q;
q=(struct node *)malloc(sizeof(struct node));
q=p;
printf("%d",q->data);
preorder(q->lft);
preorder(q->rgt);
return 0;
}
int main()
{int num;struct node *p;
p=NULL;
while(num!=0)
{scanf("%d",&num);
p=cons(p,num);
}
preorder(p);
return 0;
}