线性表)众所周知,递增有序的单链表a、b分别容纳一个集合。 求两个集合a和b的差集合,即由出现在a上且不出现在b上的要素构成的集合,用同样的形式存储,同时返回该集合的要素数。
#包含
用户命名空间STD;
#定义空值0
int i,len,j,n,m;
定义结构链接列表//结构
{
int num;
链接列表*下一步;
(;
使用linklist *creat ()//末尾插入法创建链表
{ int x;
cinm;
链接列表*p2、*head、*p2; //p1是指新形成的节点,p2是指末尾的节点
P1=新链接列表;
for(x=1; x=m; x )
{
cinp1-num;
if(x==1) head=p1;
p2=p1;
P1=新链接列表;
p2-next=p1;
if(x==m ) p2-next=NULL;
}
返回头;
}
void build _ print _ link list (link list * a,linklist *b ) /获取两个链表的差集的函数
{
链接列表* P1、*p2、*head、*p、*c、*p3;
p1=a; p2=b;
int m=0,n=0;
wile(P1!=遍历空//P1
{
n=0; p2=b;
while(P2!=移动=null(//p2,查找出现在p1上但不出现在p2上的值
{
if(P2-num==P1-num ) n=1; //有相等的值,记录n=1
p2=p2-next;
}
if(n==0) ) )。
{
/* * * * * * * * *
添加代码
/* * * * * * * * *
if(m==1) c=p,p3=p; //差集保管才c中
else
{
p3-next=p;
p3=p;
}
}
p1=p1-next;
}
p3-next=NULL;
n=0;
威廉(c!=空输出差集
{
n;
coutnum
c=c-next;
}
出局了
出局了
}
void destroy (结构链接列表*头) )。
{
结构链接列表* p;
wile (头!=NULL )
{
p=head-next;
delete (头;
head=p;
}
}
int main () )
{
链接列表*headb,*headb;//创建两个链表,用build_print_linklist函数执行A-B输出
heada=creat (;
headb=creat (;
build_print_linklist(Heada,headb );
Estroy(Heada );
Estroy(Headb );
返回0;
}