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

"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; } else return; 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"); }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值