- #include<iostream>
- using namespace std;
- class ChainNode {
- friend class ChainIterator;
- friend class Chain;
- private:
- int date;
- ChainNode *link;
- };
- class Chain {
- friend class ChainIterator;
- public :
- Chain(){first=0;}
- int Length() const;
- int Rearch(const int& x)const;
- Chain& Delete(int k,int& x);
- Chain& Insert(const int& x);
- Chain& LastInsert(int k,const int& x);
- private:
- ChainNode *first;
- };
- //链表遍历器类
- class ChainIterator{
- public:
- int* Initialize(const Chain &c){
- location =c.first;
- if(location) return &location->date;
- return 0;
- }
- int* Next(){
- if(!location) return 0;
- location=location->link;
- if(location) return &location->date;
- return 0;
- }
- private:
- ChainNode *location;
- };
- //计算长度
- int Chain::Length() const{
- ChainNode *current=new ChainNode;
- int len=0;
- while(current){
- len++;
- current=current->link;
- }
- return len;
- }
- //插入
- Chain &Chain::Insert(const int & x){
- ChainNode *y=new ChainNode;
- y->date=x;
- y->link=first;
- first=y;
- return *this;
- }
- //在末尾插入
- Chain &Chain::LastInsert(int k,const int & x){
- ChainNode *p=first;
- for(int index=1;index<k&&p;index++)
- p=p->link;
- ChainNode *y=new ChainNode;
- y->date=x;
- if(k){
- y->link=p->link;
- p->link=y;
- }else{
- y->link=first;
- first=y;
- }
- return *this;
- }
- //删除
- Chain &Chain::Delete(int k,int& x){
- if(k<1||!first)
- cout<<"不存在第"<<k<<"个元素,异常";
- ChainNode *p=first;
- if(k==1)
- first=first->link;
- else{
- ChainNode *q=first;
- for(int index=1;index<k-1&&q;index++)
- q=q->link;
- if(!q||!q->link)
- cout<<"不存在第"<<k<<"个元素,异常";
- p=q->link;
- q->link=p->link;
- }
- x=p->date;
- delete p;
- return *this;
- }
- //搜索
- int Chain::Rearch(const int& x)const{
- ChainNode *current=first;
- int index=1;
- while(current && current->date!=x){
- current=current->link;
- index++;
- }
- if(current)
- return index;
- return 0;
- }
- //遍历器输出
- void shuchu( Chain &X){
- int *x;
- int s=0;
- ChainIterator c;
- x=c.Initialize(X);
- while(x){
- s++;
- x=c.Next();}
- x=c.Initialize(X);
- int ss=0;
- while(x){
- ss++;
- if(ss<s)
- cout<<*x<<",";
- else
- cout<<*x;
- x=c.Next();}
- cout<<endl;
- }
- //遍历器输出2
- void shuchu2( Chain &X){
- int *x;
- ChainIterator c;
- x=c.Initialize(X);
- while(x){
- cout<<*x<<",";
- x=c.Next();
- }
- }
- int main(){
- Chain s,s2;
- int num=0,num2=0;
- cout<<"Input1"<<endl;
- int in;
- cin>>in;
- while(in!=0){
- s.LastInsert(num,in);
- num++;
- cin>>in;
- }
- cout<<"Output1"<<endl;
- shuchu(s);
- cout<<"Input2"<<endl;
- cin>>in;
- s.Insert(in);
- cout<<"Output2"<<endl;
- shuchu(s);
- cout<<"Input3"<<endl;
- cin>>in;
- int aa=s.Rearch(in);
- cout<<"Output3"<<endl;
- cout<<aa<<endl;
- cout<<"Input4"<<endl;
- cin>>in;
- aa=s.Rearch(in);
- cout<<"Output4"<<endl;
- cout<<aa<<endl;
- cout<<"Input5"<<endl;
- int in2;
- cin>>in2;
- while(in2!=0){
- s2.LastInsert(num2,in2);
- num2++;
- cin>>in2;
- }
- cout<<"Output5"<<endl;
- shuchu(s2);
- shuchu2(s);
- shuchu(s2);
- cout<<"End"<<endl;
- return 0;
- }
C++线性表操作
最新推荐文章于 2024-09-19 13:36:34 发布