构造链表ABC,删除在A中的ABC的公共元素_C++实现

来源:互联网 发布:烟雨唱扬州 知乎 编辑:程序博客网 时间:2024/04/28 14:06

"head.h"


#include<iostream>using namespace std;class NODE{public:NODE();int num;NODE *next;};NODE::NODE(){num=0;next=NULL;}class DATA{public:DATA();void Constructor();void Print();void Process();private:NODE *head1,*head2,*head3,*p,*pr,*keep;int first;};DATA::DATA(){head1=head2=head3=p=pr=keep=NULL;first=1;}void DATA::Constructor(){if(first<=3){cout<<"Constructor "<<first<<" called !"<<endl;}elsereturn;cout<<"How Many Numbers Do You Want To Enter ?"<<endl<<endl;int nnum,input;cin>>nnum;if(nnum!=0){p=new NODE;cin>>input;p->num=input;switch(first){case 1:head1=p;break;case 2:head2=p;break;case 3:head3=p;break;}nnum--;}while(nnum--){p->next=new NODE;p=p->next;cin>>input;p->num=input;}p->next=NULL;first++;}void DATA::Print(){cout<<"Print called !"<<endl<<endl;p=head1;if(p==NULL)cout<<"No Data !"<<endl;while(p!=NULL){cout<<p->num<<endl;p=p->next;}cout<<endl;}void DATA::Process(){cout<<"Processor called !"<<endl<<endl;pr=p=head1;while(head2!=NULL&&head3!=NULL){if(head2->num<p->num){keep=head2;head2=head2->next;delete keep;}if(head3->num<p->num){keep=head3;head3=head3->next;delete keep;}if(head2==NULL||head3==NULL)return;if(head2->num==p->num&&head3->num==p->num){if(p==head1){head1=head1->next;delete p;p=pr=head1;}else{pr->next=p->next;delete p;p=pr->next;}if(p==NULL)return;}if((head2->num>=p->num&&head3->num>p->num)||(head2->num>p->num&&head3->num>=p->num)){pr=p;p=p->next;if(p==NULL)return;}}}



"main.cpp"



#include<iostream>#include"head.h"using namespace std;int main(){DATA data;data.Constructor();data.Constructor();data.Constructor();data.Process();data.Print();system("pause");}