順序表的轉置

 

  1. #include<iostream>  
  2. using namespace std;  
  3. typedef int ElemType;  
  4. const int MaxSize = 1000;  
  5. class SequenList  
  6. {  
  7.      protected:  
  8.          ElemType a[MaxSize];  
  9.          int len;  
  10.      public:  
  11.          void exchange( int n)  //逆置  
  12.          {  
  13.              for(int i = 0;i<=n/2-1;i++)  
  14.              {  
  15.                  ElemType t = a[i];  
  16.                  a[i] = a[n-i-1];  
  17.                  a[n-i-1]=t;  
  18.              }  
  19.          }  
  20.          void input(int n)  
  21.          {  
  22.              len = n;  
  23.              for(int i = 0;i<n;i++)  
  24.              {  
  25.                  cin>>a[i];  
  26.              }  
  27.          }  
  28.          void print(int n)  
  29.          {  
  30.              for(int i = 0;i<=n-1;i++)  
  31.              {  
  32.                  cout<<a[i]<<' ';  
  33.              }  
  34.              cout<<endl;  
  35.          }  
  36.  
  37. };  
  38.  
  39.  
  40. int main()  
  41. {  
  42.     int n;  
  43.     cout<<"請輸入表中元素個數"<<endl;  
  44.     cin>>n;  
  45.     SequenList a;  
  46.     cout<<"請輸入表中元素值"<<endl;  
  47.     a.input(n);  
  48.     a.print(n);  
  49.     a.exchange(n);  
  50.     a.print (n);  
  51.     system("pause");  
  52.     return 0;  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章