求差集
输入
A集合:1 2 3 4 5
B集合:4 5 6 7 8 9 10 11
输出
A-B :1 2 3
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode
{
@H_404_13@int data;
struct LNode *next;
}LNode;
@H_404_13@void saveListnext(LNode *&L,@H_404_13@int x[],@H_404_13@int n)//尾插
{
L = (LNode *)malloc (sizeof(LNode));
L->next=NULL;
LNode *q;
q=L;
LNode *s;
@H_404_13@for(@H_404_13@int i=0;i<n;i++)
{
s=(LNode *)malloc(sizeof(LNode));
s->data = x[i];
q->next=s;
q=q->next;
}
q->next=NULL;
}
@H_404_13@void printList(LNode *L)
{
LNode *q;
q=L->next;
@H_404_13@int i=0;
@H_404_13@while(q!=NULL)
{
@H_404_13@if(i++)
putchar(' ');
printf("%d",q->data);
q=q->next;
}
printf("\n");
}
@H_404_13@void difference(LNode *L,LNode *B)
{
LNode *q,*b;
q=L->next;
b=B->next;
LNode *pre;//要删出节点前面那一个节点
pre=L;
LNode *s;
@H_404_13@while(q!=NULL&&b!=NULL)//必须都不为空
{
@H_404_13@if(q->data<b->data)
{
pre=q;//这一句至关重要,影响到后面删除操作
q=q->next;
}
@H_404_13@else @H_404_13@if(q->data>b->data)
{
b=b->next;
}
@H_404_13@else
{
pre->next=q->next;
s=q;
q=q->next;
free(s);
}
}
}
@H_404_13@int main (@H_404_13@void)
{
LNode *L,*B;
@H_404_13@int x1[5]={1,2,3,4,5};
@H_404_13@int x2[8]={4,5,6,7,8,9,10,11};
saveListnext(L,x1,5);
saveListnext(B,x2,8);
printList(L);
printList(B);
difference(L,B);
printList(L);
@H_404_13@return 0;
}