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

构造一个循环链表

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

不开辟新空间

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

每个存放一种字符


"head.h"


#include<iostream> #include<ctype.h> #define DIGIT 1 #define ALPHA 2 #define OTHER 3 using 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; else head->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; else p->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"); }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值