0

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);
}
Arjun
  • 53
  • 9
  • Does this answer your question? [Changing address contained by pointer using function](https://stackoverflow.com/questions/13431108/changing-address-contained-by-pointer-using-function) – kaylum Dec 16 '20 at 06:04
  • Tried that but if condition assumes pointer variable as operator i think. Sorry i am a newbie to coding. I might be wrong. – Arjun Dec 16 '20 at 06:18
  • What does "pointer variable as operator" mean? You just declare the function to accept a `struct node **head`, set it with `*head = new_node` and call the function as `create(data, &head);`. – kaylum Dec 16 '20 at 06:20
  • Why do you have a need to pass a list address when both of your pointers are declared `global`? – David C. Rankin Dec 16 '20 at 06:21

1 Answers1

0
#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 *newnode=(struct node *) malloc(sizeof( struct node));
    newnode->data=data;
    newnode->next=NULL;
    if(head1==NULL)
        head1=newnode;
    else {
        struct node *temp=head1;
        while(temp->next!=NULL)
            temp=temp->next;
        temp->next=newnode;
    }   
}
void create1(int data) {
    struct node *newnode=(struct node *) malloc(sizeof( struct node));
    newnode->data=data;
    newnode->next=NULL;
    if(head2==NULL)
        head2=newnode;
    else {
        struct node *temp=head2;
        while(temp->next!=NULL)
            temp=temp->next;
        temp->next=newnode;
    }   
}
void identical() {
    struct node *temp1=head1,*temp2=head2;
    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);
    }
    scanf("%d",&n);
    for(int i=0;i<n;i++) {
        scanf("%d",&val);
        create1(val);
    }
    identical();
    return 0;
}
Arjun
  • 53
  • 9