我需要传递head引用来创建用于创建两个链表head1和head2的函数,并检查它们是否相同。我正试图将头指针地址传递给create((并直接处理它。我该如何做到这一点。
#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;
}
当我使用*head访问地址时,它在if条件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);
}
#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;
}