已知无头单链表A和B表示两个集合,用算法实现A=A-B补集数据结构

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/30 17:14:46
已知无头单链表A和B表示两个集合,用算法实现A=A-B补集数据结构

已知无头单链表A和B表示两个集合,用算法实现A=A-B补集数据结构
已知无头单链表A和B表示两个集合,用算法实现A=A-B补集
数据结构

已知无头单链表A和B表示两个集合,用算法实现A=A-B补集数据结构
data_int
#include "head.h"
struct LNode{
// char data[10];
int data;
struct LNode *next;
};
typedef struct LNode * LinkList;
void InitList_L(LinkList &L)//链表构造函数
{
L=new LNode;
L->next=NULL;
}
void PrintList_L(LinkList &H)//链表显示函数
{
LinkList L=H;
L=L->next;
while(1)
{
cout<<"data value is "<<L->data<<endl;
L=L->next;
if (L==NULL)
return;
}
}
void Insert_L(LinkList &H,int n=0)//插入链表
{
LinkList L=H;
LinkList p=L;
int i=0;
if (n==0)
{
n=1;
while(p->next!=NULL)
{
p=p->next;
n++;
}

}
else if (n<1)
{
cout<<"error"<<endl;
return;
}
for (i=0;i<n-1;i++)
{
if (L->next==NULL)
{
cout<<"error"<<endl;
return;
}
L=L->next;
}
p=new LNode;
cout<<"please input a value:";
cin>>p->data;
p->next=L->next;
L->next=p;
}
LinkList bing_LinkList(LinkList a,LinkList b)
{
LinkList c;
LinkList nc;
LinkList t;
InitList_L(c);
nc=c;
a=a->next;
while (a!=NULL)//复制a到c
{
t=new LNode;
t->data=a->data;
nc->next=t;
t->next=NULL;
nc=nc->next;
a=a->next;
}
b=b->next;
while (b!=NULL)
{
nc=c;
while (nc->next!=NULL)
{
if (nc->next->data==b->data)
break;
nc=nc->next;
}
if (nc->next==NULL)
{
t=new LNode;
t->data=b->data;
nc->next=t;
t->next=NULL;
nc=nc->next;
}
b=b->next;
}
return c;
}
void main()
{
LinkList a,b,c;
int i=0;

InitList_L(a);
cout<<"\nI will input date."<<endl;
for (i=1;i<=3;i++)
Insert_L(a,i);
// PrintList_L(a);
InitList_L(b);
cout<<"\nI will input date."<<endl;
for (i=1;i<=3;i++)
Insert_L(b,i);
// PrintList_L(b);
c=bing_LinkList(a,b);
PrintList_L(c);

}