|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<iostream.h>
#include<stdlib.h>
int j=1;
typedef struct node
{
int data;
struct node *next;
}node;
node *creat(int len)
{
node *head,*p;
int m;
head=(node*)malloc(sizeof(node));
cout<<"请输入第"<<j++<<"个线性表的数据:";
cin>>m;
head->data=m;
head->next=NULL;
for(int i=1;i<len;i++)
{
int k;
p=(node*)malloc(sizeof(node));
cin>>k;
p->data=k;
p->next=head->next;
head->next=p;
}
return(head);
}
void print(node *List)
{
node *p;
p=List;
if(List!=NULL)
{
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
}
}
void unionL(node *La,int len_La,node *Lb,int len_Lb)
{
node *p;
p=La;
for(int i=1;i<=len_La;i++)
{
for(int n=1;n<=len_Lb;n++)
{
if(Lb->data!=p->data)
{
len_Lb++;
Lb->data=p->data;
p=p->next;
}
if(Lb->data==p->data)
{
p=p->next;
}
}
}
node *q;
q=Lb;
if(Lb!=NULL)
{
while(q!=NULL)
{
cout<<p->data<<" ";
q=q->next;
}
}
}
void main()
{
void unionL(node *La,int len_La,node *Lb,int len_Lb);
node *creat(int len);
void print(node *List);
node *ListLa,*ListLb;
int La_len,Lb_len;
cout<<"请输入第1个线性表的长度:";
cin>>La_len;
ListLa=creat(La_len);
cout<<"第1个线性表为:";
print(ListLa);
cout<<endl;
cout<<"请输入第2个线性表的长度:";
cin>>Lb_len;
ListLb=creat(Lb_len);
cout<<"第2个线性表为:";
print(ListLb);
cout<<endl;
cout<<"合并后的线性表为:";
unionL(ListLa,La_len,ListLb,Lb_len);
cout<<endl;
}
|
|