单链表的删除
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node* next;
}Node,*ListNode;
void CreatList(ListNode name,int n){
ListNode temp=name;
ListNode p;
for(int i=0;i<n;i++)
{
p=(ListNode)malloc(sizeof(ListNode));
temp->next=p;
temp=temp->next;
}
temp->next=NULL;
}
ListNode CompareList(ListNode a,ListNode b){
ListNode head=(ListNode)malloc(sizeof(ListNode));
ListNode temp=head,p;
ListNode aa=a->next;
ListNode bb=b->next;
while(aa)
{
while(bb)
{
if(aa->data == bb->data)
{
p=(ListNode)malloc(sizeof(Node));
temp->next=p;
temp=temp->next;
temp->data=aa->data;
bb=bb->next;
}
else
{
bb=bb->next;
}
}
aa=aa->next;
bb=b->next;
}
ListNode q=NULL;
temp->next=q;
return head;
}
ListNode Sub(ListNode a,ListNode b){
ListNode head=(ListNode)malloc(sizeof(Node));
ListNode temp=head,q;
ListNode aa=a->next;
ListNode bb=b->next;
while(aa)
{
while(bb)
{
if(aa->data == bb->data)
{
break;
}
if(bb->next==NULL)
{
q=(ListNode)malloc(sizeof(Node));
temp->next=q;
temp=temp->next;
temp->data=aa->data;
break;
}
bb=bb->next;
}
aa=aa->next;
bb=b;
}
ListNode qq=NULL;
temp->next=qq;
return head;
}
int main()
{
int m,n,p;
scanf("%d %d %d",&m,&n,&p);
ListNode A=(ListNode)malloc(sizeof(Node));
ListNode B=(ListNode)malloc(sizeof(Node));
ListNode C=(ListNode)malloc(sizeof(Node));
CreatList(A,m);
CreatList(B,n);
CreatList(C,p);
ListNode aa=A->next;
ListNode bb=B->next;
ListNode cc=C->next;
for(int i=0;i<m;i++)
{
scanf("%d",&aa->data);
aa=aa->next;
}
for(int i=0;i<n;i++)
{
scanf("%d",&bb->data);
bb=bb->next;
}
for(int i=0;i<p;i++)
{
scanf("%d",&cc->data);
cc=cc->next;
}
ListNode cha=CompareList(B,C);
ListNode answer=Sub(A,cha);
ListNode newanswer=answer->next;
while(newanswer)
{
printf("%d ",newanswer->data);
newanswer=newanswer->next;
}
return 0;
}