【Pi Pilei】Set operation
Content requirements
Realize the combination of collections (ie the combination of two linked lists)
Source code
#include<iostream>//讨论课1:集合的运算
using namespace std;
class node
{
public:
int data;
node *next;
};
void creat(node *&head)
{
int n;
while(cin>>n&&n!=0)
{
int m=1;
node *s=new node;
s->data=n;
s->next=NULL;
node *p,*q;
p=NULL;
q=head;
while(q!=NULL&&n>=q->data)
{
if(n==q->data)
m=0;
if(m!=0)
p=q;
q=q->next;
}
if(m!=0)
{
if(p==NULL)
head=s;
else
{
p->next=s;}
s->next=q;
}
}
}
void show(node *p)
{
while(p)
{
cout<<p->data<<' ';
p=p->next;
}
cout<<endl;
}
int panduan(node *head,int x)//panduan链表中是否有x,有则0
{
node *q;
q=head;
while(q!=NULL)
{
if(x==q->data)
return 0;
else
q=q->next;
}
return 1;
}
void insert(int n,node *&head)
{
node *p,*q,*s;
s=new node;
s->next=NULL;
s->data=n;
if(head==NULL)
head=s;
else if(head->data>s->data)
{
s->next=head;
head=s;
}
else if(head->data<s->data)
{
int m=1;
q=head;
p=head->next;
while(p&&m!=0)
{
if(p->data>s->data)
{
s->next=p;
q->next=s;
m=0;
}
else
{
q=p;
p=p->next;
}
}
if(p==NULL)
{
q->next=s;
}
}
}
int main()
{
node *head1=NULL;node *head2=NULL;
creat(head1);
show(head1);
creat(head2);
show(head2);
node *p=head1;
while(p)
{
if(panduan(head2,p->data)==0)
{
}
else
insert(p->data,head2);
p=p->next;
}
show(head2);
}