C++线性表操作

  1. #include<iostream>  
  2. using namespace std;  
  3. class ChainNode {  
  4.     friend class ChainIterator;  
  5.     friend class Chain;  
  6.     private:  
  7.         int date;  
  8.         ChainNode *link;  
  9.   
  10. };  
  11.   
  12. class Chain {  
  13.     friend class ChainIterator;  
  14.     public :  
  15.         Chain(){first=0;}  
  16.       
  17.       
  18.         int Length() const;  
  19.         int Rearch(const int& x)const;  
  20.         Chain& Delete(int k,int& x);  
  21.         Chain& Insert(const int& x);  
  22.         Chain& LastInsert(int k,const int& x);  
  23.       
  24.     private:  
  25.         ChainNode *first;  
  26.   
  27.   
  28.   
  29. };  
  30. //链表遍历器类  
  31. class ChainIterator{  
  32.   
  33. public:  
  34.     int* Initialize(const Chain &c){  
  35.       
  36.         location =c.first;  
  37.         if(location) return &location->date;  
  38.         return 0;  
  39.     }  
  40.    int* Next(){  
  41.       
  42.         if(!location) return 0;  
  43.         location=location->link;  
  44.         if(location) return &location->date;  
  45.         return 0;  
  46.   
  47.       
  48.     }  
  49.      
  50. private:  
  51.     ChainNode *location;  
  52.   
  53. };  
  54. //计算长度  
  55. int Chain::Length() const{  
  56.   
  57.     ChainNode *current=new ChainNode;  
  58.     int len=0;  
  59.     while(current){  
  60.       
  61.         len++;  
  62.         current=current->link;  
  63.       
  64.     }  
  65.   
  66. return len;  
  67. }  
  68. //插入  
  69. Chain &Chain::Insert(const int & x){  
  70.   
  71.     ChainNode *y=new ChainNode;  
  72.     y->date=x;  
  73.     y->link=first;  
  74.     first=y;  
  75.     return *this;  
  76. }  
  77. //在末尾插入  
  78. Chain &Chain::LastInsert(int k,const int & x){  
  79.      ChainNode *p=first;  
  80.     for(int index=1;index<k&&p;index++)  
  81.         p=p->link;  
  82.     ChainNode *y=new ChainNode;  
  83.     y->date=x;  
  84.     if(k){  
  85.       
  86.         y->link=p->link;  
  87.         p->link=y;  
  88.     }else{  
  89.       
  90.         y->link=first;  
  91.         first=y;  
  92.     }  
  93.   
  94.        
  95.   
  96. return *this;  
  97.   
  98.   
  99.   
  100. }  
  101. //删除  
  102. Chain &Chain::Delete(int k,int& x){  
  103.   
  104.     if(k<1||!first)  
  105.     cout<<"不存在第"<<k<<"个元素,异常";  
  106.     ChainNode *p=first;  
  107.     if(k==1)  
  108.         first=first->link;  
  109.     else{  
  110.       
  111.         ChainNode *q=first;  
  112.         for(int index=1;index<k-1&&q;index++)  
  113.             q=q->link;  
  114.         if(!q||!q->link)  
  115.             cout<<"不存在第"<<k<<"个元素,异常";  
  116.         p=q->link;  
  117.         q->link=p->link;  
  118.     }  
  119. x=p->date;  
  120. delete p;  
  121. return *this;  
  122.   
  123. }  
  124. //搜索  
  125. int Chain::Rearch(const int& x)const{  
  126.   
  127.     ChainNode *current=first;  
  128.     int index=1;  
  129.     while(current && current->date!=x){  
  130.       
  131.         current=current->link;  
  132.         index++;  
  133.       
  134.     }  
  135. if(current)  
  136. return index;  
  137. return 0;  
  138.   
  139. }  
  140. //遍历器输出  
  141.   
  142. void shuchu( Chain &X){  
  143.     int *x;  
  144.     int s=0;  
  145.     ChainIterator c;  
  146.     x=c.Initialize(X);  
  147.     while(x){  
  148.         s++;  
  149.         x=c.Next();}  
  150.       
  151.     x=c.Initialize(X);  
  152.     int ss=0;  
  153.     while(x){  
  154. ss++;  
  155. if(ss<s)  
  156.         cout<<*x<<",";  
  157. else  
  158. cout<<*x;  
  159.         x=c.Next();}  
  160.     cout<<endl;  
  161.       
  162. }  
  163. //遍历器输出2  
  164. void shuchu2( Chain &X){  
  165.     int *x;  
  166.     ChainIterator c;  
  167.     x=c.Initialize(X);  
  168.     while(x){  
  169.   
  170.         cout<<*x<<",";  
  171.         x=c.Next();  
  172.       
  173.     }  
  174.   
  175.       
  176. }  
  177. int main(){  
  178.     Chain s,s2;  
  179.     int num=0,num2=0;  
  180.      cout<<"Input1"<<endl;  
  181.     int in;  
  182.     cin>>in;  
  183.     while(in!=0){  
  184.      s.LastInsert(num,in);  
  185.     num++;  
  186.     cin>>in;  
  187.     }  
  188.   
  189.      cout<<"Output1"<<endl;  
  190.      shuchu(s);  
  191.      cout<<"Input2"<<endl;  
  192.      cin>>in;  
  193.      s.Insert(in);  
  194.      cout<<"Output2"<<endl;  
  195.      shuchu(s);  
  196.      cout<<"Input3"<<endl;  
  197.      cin>>in;  
  198.      int aa=s.Rearch(in);  
  199.      cout<<"Output3"<<endl;  
  200.      cout<<aa<<endl;  
  201.      cout<<"Input4"<<endl;  
  202.      cin>>in;  
  203.      aa=s.Rearch(in);  
  204.      cout<<"Output4"<<endl;  
  205.      cout<<aa<<endl;  
  206.      cout<<"Input5"<<endl;  
  207.      int in2;  
  208.     cin>>in2;  
  209.     while(in2!=0){  
  210.      s2.LastInsert(num2,in2);  
  211.     num2++;  
  212.     cin>>in2;  
  213.     }  
  214.   
  215.      cout<<"Output5"<<endl;  
  216.      shuchu(s2);  
  217. shuchu2(s);  
  218. shuchu(s2);  
  219. cout<<"End"<<endl;  
  220.   
  221.   
  222. return 0;  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值