实验项目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. }  

【运行结果】

注:以上内容仅供参考,如有问题请指正。

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