天天看点

顺序表

        代码所示为模板类,无法直接编译。我感觉学习数据结构主要还是学习各种数据结构的原理,至于只用是比较简单的。

        顺序表AList是由线性表List抽象类继承来的。从代码数量上就能够看出AList类中insert和remove方法是比较重要的。将顺序表看成由两部分构成,在AList类中定义了fence属性,是为了定义当前元素的概念,同时也助于修改线性表的表示形式。

  1. //List abstract class  
  2. template <class Elem> class List 
  3.     public: 
  4.         //virtual void clear() = 0; 
  5.         virtual bool insert(const Elem&) = 0; 
  6.         virtual bool append(const Elem&) = 0; 
  7.         virtual bool remove(Elem&) = 0; 
  8.         virtual void setStart() = 0; 
  9.         virtual void setEnd() = 0; 
  10.         virtual void prev() = 0; 
  11.         virtual void next() = 0; 
  12.         virtual int leftLength() = 0; 
  13.         virtual int rightLength() = 0; 
  14.         virtual bool setPos(int pos) = 0; 
  15.         virtual bool getValues(Elem&) = 0; 
  16.         virtual void print() const = 0;  
  17. }; 
  18. template <class Elem> 
  19. class AList : public List<Elem> 
  20.     private: 
  21.         int maxSize; 
  22.         int listSize; 
  23.         int fence; 
  24.         Elem *listArray; 
  25.     public: 
  26.         AList(int size=DefaultListSize) 
  27.         { 
  28.             maxSize = size; 
  29.             listSize = fence = 0; 
  30.             listArray = new Elem[maxSize]; 
  31.         } 
  32.         ~AList() 
  33.         { 
  34.             delete [] listArray; 
  35.         } 
  36.         bool insert(const Elem&); 
  37.         bool append(const Elem&); 
  38.         bool remove(Elem&); 
  39.         void setStart(); 
  40.         void setEnd(); 
  41.         void prev(); 
  42.         void next(); 
  43.         int leftLength(); 
  44.         int rightLength(); 
  45.         bool setPos(int pos); 
  46.         bool getValues(Elem&); 
  47.         void print() const;  
  48. }; 
  49. template <class Elem> 
  50. bool  AList<Elem>::insert(const Elem& item) 
  51.     if(listSize==maxSize) 
  52.         return false; 
  53.     for(int i=listSize;i>fence;i--) 
  54.         listArray[i] = listArray[i-1]; 
  55.     listArray[fence]=item; 
  56.     listSize++; 
  57.     return true; 
  58. template <class Elem> 
  59. bool  AList<Elem>::append(const Elem& item) 
  60.     if(listSize == maxSize) 
  61.         return false; 
  62.     listArray[listSize++]=item; 
  63.         return true; 
  64. template <class Elem> 
  65. bool AList<Elem>::remove(Elem& it) 
  66. {  
  67.     if(rightLength() == 0) 
  68.         return false; 
  69.     it=listArray[fence]; 
  70.     for(int i=fence;i<maxSize;i++) 
  71.         listArray[i]=listArray[i+1]; 
  72.     listSize--; 
  73.     return true;     
  74. template <class Elem> 
  75. void AList<Elem>::setStart() 
  76.     fence=0; 
  77. template <class Elem> 
  78. void AList<Elem>::setEnd() 
  79.     fence=listSize; 
  80. template <class Elem> 
  81. void AList<Elem>::prev() 
  82.     if(fence!=0) 
  83.         fence--; 
  84. template <class Elem> 
  85. void AList<Elem>::next() 
  86.     if(fence<=listSize) 
  87.         fence++; 
  88. template <class Elem> 
  89. int AList<Elem>::leftLength() 
  90.     return fence; 
  91. template <class Elem> 
  92. int AList<Elem>::rightLength() 
  93.     return (listSize-fence); 
  94. template <class Elem> 
  95. bool AList<Elem>::setPos(int pos) 
  96.     if((pos >= 0)&&(pos <=listSize)) 
  97.         fence=pos; 
  98.     return ((pos >=0)&&(pos <=listSize)); 
  99. template <class Elem> 
  100. bool AList<Elem>::getValues(Elem& it) 
  101.     if(listSize==0) 
  102.         return false; 
  103.     it=listArray[fence]; 
  104.     return true; 
  105. template <class Elem> 
  106. void AList<Elem>::print() const 
  107.     int temp=0; 
  108.     cout<<"In AList:<"; 
  109.     while(temp < fence) 
  110.         cout<<listArray[temp++]<<" "; 
  111.     cout<<"|"; 
  112.     while(temp < listSize) 
  113.         cout<<listArray[temp++]<<" "; 
  114.     cout<<">\n"; 
上一篇: 顺序表
下一篇: 顺序表

继续阅读