#include <stdio.h>
typedef struct node{
int value;
struct node *pointer;
}link;
link *create(int *array,int num){//(数组,数组元素个数)使用数组创建单链表
link *head=NULL,*temp1,*temp2;
int i=0;
if(num>1){
head=(link *)malloc(sizeof(link));
temp1=head;
temp1->value=array[0];
temp2=temp1->pointer;
}
for(i=1;i<num;i++){
temp2=(link *)malloc(sizeof(link));
temp2->value=array[i];
temp1->pointer=temp2;
temp1=temp2;
}
temp1->pointer=NULL;
return head;
}
void show(link *temp){//显示链表
while(temp!=NULL){
printf("%d,",temp->value);
temp=temp->pointer;
}
printf("\n");
}
link *del(link *a,link *b){//删除链表a中与链表b相同的元素
link *tempa,*tempb,*prev,*del;
tempa=a;
prev=NULL;
while(tempa!=NULL){//遍历链表a
tempb=b;
while(tempb!=NULL){//遍历链表b
if(tempb->value==tempa->value){//找到a,b相同值的节点
del=tempa;
if(tempa==a){//找到的节点为头节点
a=a->pointer;
tempa=a;
free(del);
}else{
prev->pointer=tempa->pointer;
tempa=prev;
free(del);
}
}
tempb=tempb->pointer;
}
prev=tempa;
tempa=tempa->pointer;
}
return a;
}
int main(){
int array1[6]={0,1,3,4,6,9},array2[2]={1,4};
link *a,*b;
a=create(array1,6);//创建链表a,1-2-3-4-6-9
printf("create link a:");
show(a);
b=create(array2,2);//创建链表a,1-4
printf("create link b:");
show(b);
a=del(a,b);
printf("del later link a:");
show(a);
return 0;
}
很久没写C语言了,这个是我完全按照你的要求写的,不是copy的。
追问嗯嗯,我看看
本回答被提问者和网友采纳