What wrong in my code.segmentation fault diasplayig

#include
using namespace std;
struct node{
long long int data;
struct node *next;
};
struct node *p,*s;
void addtrail(node &head,long long int data){
node
n=new node;
n->data=data;
n->next=NULL;
if(head==NULL){

head=n;
p=n;
}
p->next=n;
p=n;

}
void kalternate(node *&head,node &head1){
node
cur=head;
while(cur){
int u=cur->data;
if(((u)%2!=0)&&(head1==NULL)){
head1=cur;
s=head1;
cout<data;
}
if(((u)%2!=0)&&(head1!=NULL)){
s->next=cur;
s=cur;
}
cur=cur->next;
}
node *w=head;
while(w){
int v=w->data;
if(((v)%2==0)&&(head1==NULL)){
head1=w;
s=head1;
}
if(((v)%2==0)&&(head1!=NULL)){
s->next=w;
s=w;
}
w=w->next;

}
s->next=NULL;
}
int main() {
long long int n,k;
cin>>n;
long long int a[n];
node *head=NULL;
for(long long int i=0;i<n;i++){
cin>>a[i];
addtrail(head,a[i]);
}

node* head1=NULL;
kalternate(head,head1);
node *u=head1;
while(u){
cout<data<<" ";
u=u->next;
}
return 0;
}

Hi,
There are several errors like insertion at tail in linked list ,
then there are corner cases in the code, which needs to be properly handled,
I have used descriptive names of pointers so that u can understand better as to how the question progress

  1. do it one go both even odd value
  2. have a odd_head that points to first odd node, then odd_iterator that moves till the end, then odd_iterator->next = even_head
  3. similarly even_head to point to first even_head , even_iterator to iterate to the last even node
  4. then corner cases
    a if odd_iterator == NULL
    b even_iterator is NULL
    The code is here->
    but first try it urself keep it as a reference
    https://ide.codingblocks.com/s/171187

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.