循环链表按条件分割_C++实现

来源:互联网 发布:刹车片 知乎 编辑:程序博客网 时间:2024/06/05 06:20

构造一个循环链表

存放一个含有三种字符(数字,字母,其他)字符序列

不开辟新空间

将该链表分割成三个循环链表

每个存放一种字符


"head.h"


#include<iostream>#include<ctype.h>#define DIGIT 1#define ALPHA 2#define OTHER 3using namespace std;class NODE{public:char atom;int flag;NODE *next;};class DATA{public:DATA();void Constructor();void PrePrint();void Process();void Print();private:NODE * head,*head1,*head2,*head3,*p,*p1,*p2,*p3;};DATA::DATA(){head=head1=head2=head3=p=p1=p2=p3=NULL;}void DATA::Constructor(){cout<<"Constructor Called !"<<endl<<endl;bool hasinput=false;char input;while(cin>>input){if(!hasinput){head=new NODE;head->atom=input;if(isdigit(input))head->flag=DIGIT;else if(isalpha(input))head->flag=ALPHA;elsehead->flag=OTHER;p=head;hasinput=true;}else{p->next=new NODE;p=p->next;p->atom=input;if(isdigit(input))p->flag=DIGIT;else if(isalpha(input))p->flag=ALPHA;elsep->flag=OTHER;}}if(!hasinput){cout<<"No Data Input !"<<endl<<endl;}else{p->next=head;}}void DATA::PrePrint(){cout<<"PrePrint Called !"<<endl<<endl;if(head==NULL){cout<<"No Data !"<<endl<<endl;return;}p=head;cout<<p->atom<<endl;p=p->next;while(p!=head){cout<<p->atom<<endl;p=p->next;}cout<<endl;}void DATA::Print(){cout<<"Print Called !"<<endl<<endl;if(head1==NULL){cout<<"No Data !"<<endl<<endl;}else{p1=head1;cout<<p1->atom<<endl;p1=p1->next;while(p1!=head1){cout<<p1->atom<<endl;p1=p1->next;}cout<<endl<<endl;}if(head2==NULL){cout<<"No Data !"<<endl<<endl;}else{p2=head2;cout<<p2->atom<<endl;p2=p2->next;while(p2!=head2){cout<<p2->atom<<endl;p2=p2->next;}cout<<endl<<endl;}if(head3==NULL){cout<<"No Data !"<<endl<<endl;}else{p3=head3;cout<<p3->atom<<endl;p3=p3->next;while(p3!=head3){cout<<p3->atom<<endl;p3=p3->next;}cout<<endl<<endl;}}void DATA::Process(){cout<<"Process Called !"<<endl<<endl;if(head==NULL){cout<<"No Data !"<<endl<<endl;return;}p1=head1;p2=head2;p3=head3;p=head;while(1){if(p->flag==DIGIT){if(p1==NULL){head1=p;p1=head1;}else{p1->next=p;p1=p1->next;}}else if(p->flag==ALPHA){if(p2==NULL){head2=p;p2=head2;}else{p2->next=p;p2=p2->next;}}else{if(p3==NULL){head3=p;p3=head3;}else{p3->next=p;p3=p3->next;}}p=p->next;if(p==head){if(p1!=head1)p1->next=head1;if(p2!=head2)p2->next=head2;if(p3!=head3)p3->next=head3;return;}}}



"main.cpp"


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