顺序表:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int *p=new int[n];
for(int i=0;i<n;i++)
cin>>p[i];
int m;
cin>>m;
int *q=new int [m];
for(int j=0;j<m;j++)
cin>>q[j];
int *bing=new int[n+m];
int *jiao = new int [n+m];
int *cha = new int [n+m];
for(int i=0;i<n;i++)
{
bing[i]=p[i];
}
int cnt_bing=n,cnt_jiao=0,cnt_cha=0;
for(int j=0;j<m;j++)
{
bool b_bing=1;
for(int i=0;i<n;i++)
{
if(p[i]==q[j])
{
b_bing=0;
jiao[cnt_jiao++]=p[i];
break;
}
}
if(b_bing)
{
bing[cnt_bing++]=q[j];
}
}
for(int ii=0;ii<n;ii++)
{
bool b_cha=1;
for(int jj=0;jj<cnt_jiao;jj++)
{
if(p[ii]==jiao[jj])
{
b_cha=0;
break;
}
}
if(b_cha)
{
cha[cnt_cha++]=p[ii];
}
}
for(int ii=0;ii<m;ii++)
{
bool b_cha=1;
for(int jj=0;jj<cnt_jiao;jj++)
{
if(q[ii]==jiao[jj])
{
b_cha=0;
break;
}
}
if(b_cha)
{
cha[cnt_cha++]=q[ii];
}
}
for(int i=0;i<cnt_bing;i++)
{
cout<<bing[i]<<" ";
}
if(cnt_bing==0)cout<<"NULL";
cout<<"\n";
for(int i=0;i<cnt_jiao;i++)
{
cout<<jiao[i]<<" ";
}
if(cnt_jiao==0)cout<<"NULL";
cout<<"\n";
for(int i=0;i<cnt_cha;i++)
{
cout<<cha[i]<<" ";
}
if(cnt_cha==0)cout<<"NULL";
return 0;
}
队列
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
cin>>n;
int *p=new int [n];
for(int i=0;i<n;i++)
{
cin>>p[i];
}
cin>>m;
int *q=new int [m];
for(int i=0;i<m;i++)
{
cin>>q[i];
}
int *bing=new int[n+m];
int *jiao = new int [n+m];
int *cha = new int [n+m];
int h_bing=0,t_bing=-1,h_jiao=0,t_jiao=-1,h_cha=0,t_cha=-1;
for(int i=0;i<n;i++)
{
t_bing++;
bing[t_bing]=p[i];
}
for(int j=0;j<m;j++)
{
bool b_bing=1;
for(int i=0;i<n;i++)
{
if(p[i]==q[j])
{
b_bing=0;
t_jiao++;
jiao[t_jiao]=p[i];
break;
}
}
if(b_bing)
{
t_bing++;
bing[t_bing]=q[j];
}
}
for(int i=0;i<n;i++)
{
bool b_cha=1;
h_jiao=0;
while (h_jiao<=t_jiao)
{
if(p[i]==jiao[h_jiao])
{
b_cha=0;
break;
}
h_jiao++;
}
if(b_cha)
{
t_cha++;
cha[t_cha]=p[i];
}
}
for(int i=0;i<m;i++)
{
bool b_cha=1;
h_jiao=0;
while (h_jiao<=t_jiao)
{
if(q[i]==jiao[h_jiao])
{
b_cha=0;
break;
}
h_jiao++;
}
if(b_cha)
{
t_cha++;
cha[t_cha]=q[i];
}
}
h_jiao=0;
while (h_bing<=t_bing)
{
cout<<bing[h_bing]<<" ";
h_bing++;
}
if(t_bing==-1)cout<<"NULL";
cout<<"\n";
while (h_jiao<=t_jiao)
{
cout<<jiao[h_jiao]<<" ";
h_jiao++;
}
if(t_jiao==-1)cout<<"NULL";
cout<<"\n";
while (h_cha<=t_cha)
{
cout<<cha[h_cha]<<" ";
h_cha++;
}
if(t_cha==-1)cout<<"NULL";
return 0;
}
链表:
#include <bits/stdc++.h>
using namespace std;
struct node{
int num;
node *next;
};
int main()
{
int n,m;
cin>>n;
node*head_1=new node,*head_2=new node,*temp,*tail;
head_1->next= nullptr;
head_2->next= nullptr;
tail=head_1;
for(int i=0;i<n;i++)
{
temp=new node;
int num;
cin>>num;
temp->num=num;
temp->next= nullptr;
tail->next=temp;
tail=tail->next;
}
cin>>m;
tail=head_2;
for(int i=0;i<m;i++)
{
temp=new node;
int num;
cin>>num;
temp->num=num;
temp->next= nullptr;
tail->next=temp;
tail=tail->next;
}
node*head_b=new node,*head_j=new node,*head_c=new node;
head_b->next= nullptr;head_j->next= nullptr;head_c->next= nullptr;
tail=head_b;
temp=head_1;
for(int i=0;i<n;i++)
{
temp=temp->next;
node *p=new node;
p->num=temp->num;
p->next= nullptr;
tail->next=p;
tail=tail->next;
}
temp=head_2;
node*tail_j=head_j;
for(int j=0;j<m;j++)
{
bool b_bing=1;
node *p=head_1;
temp=temp->next;
for(int i=0;i<n;i++)
{
p=p->next;
if(temp->num==p->num)
{
b_bing=0;
node *p_new=new node;
p_new->num=temp->num;
p_new->next= nullptr;
tail_j->next=p_new;
tail_j=tail_j->next;
break;
}
}
if(b_bing)
{
node *p_new=new node;
p_new->num=temp->num;
p_new->next=nullptr;
tail->next=p_new;
tail=tail->next;
}
}
node *tail_c=head_c;
temp=head_1;
for(int i=0;i<n;i++)
{
bool b_cha=1;
temp=temp->next;
node *temp_j=head_j->next;
while (temp_j!= nullptr)
{
if(temp->num==temp_j->num)
{
b_cha=0;
break;
}
temp_j=temp_j->next;
}
if(b_cha)
{
node *p_new=new node;
p_new->num=temp->num;
p_new->next= nullptr;
tail_c->next=p_new;
tail_c=tail_c->next;
}
}
temp=head_2;
for(int i=0;i<m;i++)
{
bool b_cha=1;
temp=temp->next;
node *temp_j=head_j->next;
while (temp_j!= nullptr)
{
if(temp->num==temp_j->num)
{
b_cha=0;
break;
}
temp_j=temp_j->next;
}
if(b_cha)
{
node *p_new=new node;
p_new->num=temp->num;
p_new->next= nullptr;
tail_c->next=p_new;
tail_c=tail_c->next;
}
}
temp=head_b->next;
while (temp!= nullptr)
{
cout<<temp->num<<" ";
temp=temp->next;
}
if(head_b->next== nullptr)cout<<"NULL";
cout<<"\n";
temp=head_j->next;
while (temp!= nullptr)
{
cout<<temp->num<<" ";
temp=temp->next;
}
if(head_j->next== nullptr)cout<<"NULL";
cout<<"\n";
temp=head_c->next;
while (temp!= nullptr)
{
cout<<temp->num<<" ";
temp=temp->next;
}
if(head_c->next== nullptr)cout<<"NULL";
}