實驗項目4——基本線性表就地逆置(順序結構)

實驗內容

[問題描述]

基本線性表就地逆置是指在基本線性表現有空間的基礎上,將基本線性表中的數據元素交換位置排列,排列完之後,新的順序序列與原來的順序序列剛好相反。如原來順序序列“abcdef”,就地逆置後的新順序序列爲“fedcba”。根據基本線性表的鏈式和順序兩種存儲結構分別完成:

(1) 順序結構的就地逆置。

(2) 鏈式結構的就地逆置。

[基本要求]

充分理解題目要求,在對基本線性表逆置時,必須是在基本線性表原有空間的基礎上進行,不能借助臨時變量所生成的臨時空間,也不能借助其他形式的臨時空間。且算法時間複雜度要求爲O(n)

【代碼】

  1. #include<iostream> 
  2. #define DefaultListSize 255 
  3. using namespace std; 
  4. template <class Elem> class List 
  5.     public
  6.         virtual bool insert(const Elem&) = 0;  
  7.         virtual bool append(const Elem&) = 0;  
  8.         virtual bool remove(Elem&) = 0;  
  9.         virtual void setStart() = 0;  
  10.         virtual void setEnd() = 0;  
  11.         virtual void prev() = 0;  
  12.         virtual void next() = 0;  
  13.         virtual int leftLength() = 0;  
  14.         virtual int rightLength() = 0;  
  15.         virtual bool setPos(int pos) = 0;  
  16.         virtual bool getValues(Elem&) = 0;  
  17.         virtual void print() const = 0;   
  18. }; 
  19.  
  20. template <class Elem>  
  21. class AList : public List<Elem> 
  22. {  
  23.     private:  
  24.         int maxSize;  
  25.         int listSize;  
  26.         int fence;  
  27.         Elem *listArray;  
  28.     public:  
  29.         AList(int size=DefaultListSize)  
  30.         {  
  31.             maxSize = size;  
  32.             listSize = fence = 0;  
  33.             listArray = new Elem[maxSize];  
  34.         }  
  35.         ~AList()  
  36.         {  
  37.             delete [] listArray;  
  38.         }  
  39.         bool insert(const Elem&);  
  40.         bool append(const Elem&);  
  41.         bool remove(Elem&);  
  42.         void setStart();  
  43.         void setEnd();  
  44.         void prev();  
  45.         void next();  
  46.         int leftLength();  
  47.         int rightLength();  
  48.         bool setPos(int pos);  
  49.         bool getValues(Elem&);  
  50.         void print() const;   
  51.         void reverlist();  
  52. };  
  53.    
  54. template <class Elem>  
  55. bool  AList<Elem>::insert(const Elem& item)     //在fence後插入元素  
  56. {  
  57.     if(listSize==maxSize)  
  58.         return false;  
  59.     for(int i=listSize;i>fence;i--)  
  60.         listArray[i] = listArray[i-1];  
  61.     listArray[fence]=item;  
  62.     listSize++;  
  63.     return true;  
  64. }  
  65.   
  66. template <class Elem>  
  67. bool  AList<Elem>::append(const Elem& item)     //在表尾插入元素  
  68. {  
  69.     if(listSize == maxSize)  
  70.         return false;  
  71.     listArray[listSize++]=item;  
  72.         return true;  
  73. }  
  74.   
  75. template <class Elem>  
  76. bool AList<Elem>::remove(Elem& it)      //移出fence後元素  
  77. {   
  78.     if(rightLength() == 0)  
  79.         return false;  
  80.     it=listArray[fence];  
  81.     for(int i=fence;i<maxSize;i++)  
  82.         listArray[i]=listArray[i+1];  
  83.     listSize--;  
  84.     return true;      
  85. }  
  86.   
  87. template <class Elem>  
  88. void AList<Elem>::setStart()    //將fence設置爲表的開始位置  
  89. {  
  90.     fence=0;  
  91. }  
  92.   
  93. template <class Elem>  
  94. void AList<Elem>::setEnd()      //將fence設置爲表尾位置  
  95. {  
  96.     fence=listSize;  
  97. }  
  98.   
  99. template <class Elem>  
  100. void AList<Elem>::prev()    //將fence前移一位  
  101. {  
  102.     if(fence!=0)  
  103.         fence--;  
  104. }  
  105.   
  106. template <class Elem>  
  107. void AList<Elem>::next()    //將fence後移一位  
  108. {  
  109.     if(fence<=listSize)  
  110.         fence++;  
  111. }  
  112.   
  113. template <class Elem>  
  114. int AList<Elem>::leftLength()  //fence左側元素數  
  115. {  
  116.     return fence;  
  117. }  
  118.   
  119. template <class Elem>  
  120. int AList<Elem>::rightLength() //fence右側元素數  
  121. {  
  122.     return (listSize-fence);  
  123. }  
  124.   
  125. template <class Elem>  
  126. bool AList<Elem>::setPos(int pos) //設置fence位置  
  127. {  
  128.     if((pos >= 0)&&(pos <=listSize))  
  129.         fence=pos;  
  130.     return ((pos >=0)&&(pos <=listSize));  
  131. }  
  132.   
  133. template <class Elem>  
  134. bool AList<Elem>::getValues(Elem& it) //獲取fence後面一個元素值  
  135. {  
  136.     if(listSize==0)  
  137.         return false;  
  138.     it=listArray[fence];  
  139.     return true;  
  140. }  
  141.   
  142. template <class Elem>  
  143. void AList<Elem>::print() const //打印線性表  
  144. {  
  145.     int temp=0;  
  146.     cout<<"In AList:<";  
  147.     while(temp < fence)  
  148.         cout<<listArray[temp++]<<" ";  
  149.     cout<<"|";  
  150.     while(temp < listSize)  
  151.         cout<<listArray[temp++]<<" ";  
  152.     cout<<">\n";  
  153. }   
  154.  
  155. template<class Elem> 
  156. void AList<Elem>::reverlist()   //轉置  
  157.     int i; 
  158.     if(listSize==0) 
  159.     { 
  160.         cout<<"線性表爲空!"
  161.     } 
  162.     if(listSize==DefaultListSize)//線性表爲滿時利用異或操作進行轉置  
  163.     { 
  164.        for(i=0;i<listSize/2;i++) 
  165.         { 
  166.             listArray[i]^=listArray[listSize-i-1]; 
  167.             listArray[listSize-i-1]^=listArray[i]; 
  168.             listArray[i]^=listArray[listSize-i-1]; 
  169.         }  
  170.     } 
  171.     if(listSize<DefaultListSize)//線性表不爲空時進行轉置  
  172.     { 
  173.         for(i=0;i<listSize/2;i++) 
  174.         { 
  175.             listArray[listSize+1]=listArray[i];//將第listSize+1號單元作爲中間存儲單元 
  176.             listArray[i]=listArray[listSize-i-1]; 
  177.             listArray[listSize-i-1]= listArray[listSize+1]; 
  178.         } 
  179.     } 
  180.  
  181. int main() 
  182.     AList<int> a; 
  183.     a.append(1); 
  184.     a.append(10); 
  185.     a.append(20); 
  186.     a.append(30); 
  187.     a.append(60); 
  188.     cout<<"原始表:"<<endl; 
  189.     a.print(); 
  190.     cout<<"轉置後:"<<endl; 
  191.     a.reverlist(); 
  192.     a.print(); 
  193.     system("pause"); 
  194. }  

【運行結果】

注:以上內容僅供參考,如有問題請指正。

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