I need to pass the head reference to create function for creating 2 linked list head1 and head2 and check if they are identical. I am trying to pass the head pointer address to create() and work on it directly. How do I achieve this.
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node* next;
};
struct node *head1=NULL,*head2=NULL;
void create(int data,struct node *head) {
struct node *newnode=(struct node *) malloc(sizeof( struct node));
newnode->data=data;
newnode->next=NULL;
if(head==NULL)
head=newnode;
else {
struct node *temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=temp;
}
}
void identical(struct node *head3,struct node *head4) {
struct node *temp1=head3,*temp2=head4;
int f=0;
while(temp1!=NULL&&temp2!=NULL) {
if(temp1->data!=temp2->data) {
f=1;
break;
}
}
temp1=temp1->next;
temp2=temp2->next;
if(f==1||temp1||temp2)
printf("\nNon identical");
else
printf("\nIdentical");
}
int main() {
int n,val;
scanf("%d",&n);
for(int i=0;i<n;i++) {
scanf("%d",&val);
create(val,&head1);
}
scanf("%d",&n);
for(int i=0;i<n;i++) {
scanf("%d",&val);
create(val,&head2);
}
identical(&head1,&head2);
return 0;
}
When i use *head to access address it throws an error in if condition if(*headref==NULL)
void addend(node **headref,int data){
node* temp1=*headref;
if(*headref==NULL){
*headref=(node*)malloc(sizeof(node));
((*headref)->data)=data;
((*headref)->next)=NULL;
}else{
node* temp=*headref;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=createnewnode(data,NULL);
}
}
int main(){
node *head1=NULL;
node *head2=NULL;
addend(&head1,11);
addend(&head1,12);
}