實驗項目3——基本線性表運算(順序存儲方式)

實驗內容

[問題描述]

基本線性表經常進行的運算操作有創建基本線性表、求基本線性表的長度、在基本線性表中查找某個數據元素、在某個位置插入一個新數據元素、在某個線性表中刪除某個數據元素以及基本線性表的輸出等操作。試編程實現基本線性表的這些基本運算。

[基本要求]

  實現基本線性表的基本運算可以採用鏈式存儲方式實現,也可以採用順序存儲的方式實現,在此給出這兩種存儲方式的實現方法,學生可任選其一進行具體實現。

【代碼】

 

  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. };  
  52.  
  53. template <class Elem>  
  54. bool  AList<Elem>::insert(const Elem& item)   //在fence後插入元素  
  55. {  
  56.     if(listSize==maxSize)  
  57.         return false;  
  58.     for(int i=listSize;i>fence;i--)  
  59.         listArray[i] = listArray[i-1];  
  60.     listArray[fence]=item;  
  61.     listSize++;  
  62.     return true;  
  63. }  
  64.   
  65. template <class Elem>  
  66. bool  AList<Elem>::append(const Elem& item)     //在表尾插入元素  
  67. {  
  68.     if(listSize == maxSize)  
  69.         return false;  
  70.     listArray[listSize++]=item;  
  71.         return true;  
  72. }  
  73.   
  74. template <class Elem>  
  75. bool AList<Elem>::remove(Elem& it)      //移出fence後元素  
  76. {   
  77.     if(rightLength() == 0)  
  78.         return false;  
  79.     it=listArray[fence];  
  80.     for(int i=fence;i<maxSize;i++)  
  81.         listArray[i]=listArray[i+1];  
  82.     listSize--;  
  83.     return true;      
  84. }  
  85.   
  86. template <class Elem>  
  87. void AList<Elem>::setStart()    //將fence設置爲表的開始位置  
  88. {  
  89.     fence=0;  
  90. }  
  91.   
  92. template <class Elem>  
  93. void AList<Elem>::setEnd()      //將fence設置爲表尾位置  
  94. {  
  95.     fence=listSize;  
  96. }  
  97.   
  98. template <class Elem>  
  99. void AList<Elem>::prev()    //將fence前移一位  
  100. {  
  101.     if(fence!=0)  
  102.         fence--;  
  103. }  
  104.   
  105. template <class Elem>  
  106. void AList<Elem>::next()    //將fence後移一位  
  107. {  
  108.     if(fence<=listSize)  
  109.         fence++;  
  110. }  
  111.   
  112. template <class Elem>  
  113. int AList<Elem>::leftLength()  //fence左側元素數  
  114. {  
  115.     return fence;  
  116. }  
  117.   
  118. template <class Elem>  
  119. int AList<Elem>::rightLength() //fence右側元素數  
  120. {  
  121.     return (listSize-fence);  
  122. }  
  123.   
  124. template <class Elem>  
  125. bool AList<Elem>::setPos(int pos) //設置fence位置  
  126. {  
  127.     if((pos >= 0)&&(pos <=listSize))  
  128.         fence=pos;  
  129.     return ((pos >=0)&&(pos <=listSize));  
  130. }  
  131.   
  132. template <class Elem>  
  133. bool AList<Elem>::getValues(Elem& it) //獲取fence後面一個元素值  
  134. {  
  135.     if(listSize==0)  
  136.         return false;  
  137.     it=listArray[fence];  
  138.     return true;  
  139. }  
  140.   
  141. template <class Elem>  
  142. void AList<Elem>::print() const //打印線性表  
  143. {  
  144.     int temp=0;  
  145.     cout<<"In AList:<";  
  146.     while(temp < fence)  
  147.         cout<<listArray[temp++]<<" ";  
  148.     cout<<"|";  
  149.     while(temp < listSize)  
  150.         cout<<listArray[temp++]<<" ";  
  151.     cout<<">\n";  
  152. }  
  153.  
  154. int main() 
  155.     AList<int> a; 
  156.     int b; 
  157.     cout<<"測試append函數:"<<endl; 
  158.     a.append(2); 
  159.     a.append(5); 
  160.     a.append(12);  
  161.     a.append(10); 
  162.     a.append(22); 
  163.     a.append(30); 
  164.     cout<<"append後:"<<endl; 
  165.     a.print(); 
  166.     cout<<"--------------------------------------"<<endl; 
  167.      
  168.     cout<<"測試insert函數:"<<endl; 
  169.     a.insert(11); 
  170.     a.insert(23); 
  171.     cout<<"insert後:"<<endl; 
  172.     a.print(); 
  173.     cout<<"--------------------------------------"<<endl; 
  174.      
  175.     cout<<"測試remove函數:"<<endl; 
  176.     a.remove(b); 
  177.     cout<<"remove後:"<<endl; 
  178.     a.print(); 
  179.     cout<<"--------------------------------------"<<endl; 
  180.  
  181.     cout<<"測試setEnd函數:"<<endl; 
  182.     a.setEnd(); 
  183.     cout<<"setEnd後:"<<endl; 
  184.     a.print(); 
  185.     cout<<"--------------------------------------"<<endl; 
  186.      
  187.     cout<<"測試setStart函數:"<<endl; 
  188.     a.setStart(); 
  189.     cout<<"setStart後:"<<endl; 
  190.     a.print(); 
  191.     cout<<"--------------------------------------"<<endl; 
  192.      
  193.  
  194.      
  195.     cout<<"測試next函數:" <<endl; 
  196.     a.next(); 
  197.     a.next(); 
  198.     cout<<"next後:"<<endl; 
  199.     a.print(); 
  200.     cout<<"--------------------------------------"<<endl; 
  201.      
  202.     cout<<"測試prev函數:"<<endl; 
  203.     a.prev(); 
  204.     cout<<"prev後:"<<endl; 
  205.     a.print(); 
  206.     cout<<"--------------------------------------"<<endl; 
  207.      
  208.     cout<<"測試leftLength函數:"<<endl; 
  209.     cout<<"leftLength = "<<a.leftLength()<<endl; 
  210.     cout<<"--------------------------------------"<<endl; 
  211.      
  212.     cout<<"測試rightLength函數:"<<endl; 
  213.     cout<<"rightLength = "<<a.rightLength()<<endl; 
  214.     cout<<"--------------------------------------"<<endl; 
  215.      
  216.     cout<<"測試setPos函數:"<<endl; 
  217.     a.setPos(4); 
  218.     cout<<"setPos後:"<<endl; 
  219.     a.print(); 
  220.     cout<<"--------------------------------------"<<endl; 
  221.      
  222.     cout<<"測試getValues函數:"<<endl; 
  223.     a.getValues(b); 
  224.     cout<<"獲得的值爲:"<<b<<endl; 
  225.     cout<<"--------------------------------------"<<endl; 
  226.     system("pause"); 

【運行結果】

 

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

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