最基本的顺序表(经典顺序表)

// 顺序表.cpp -- 最基本的顺序表(经典顺序表)
// 完整的class.

// List abstract class -- 线性表的C++抽象类声明
template<class Elem> class List()
{
 public:
  // Reinitialize the list. the client is responsible for
  // reclaiming the storange used by the list elements.
  virtual void clear() = 0;
  // Insert an element at the front of the right partition.
  // Return true if successful, false if the list is full.
  virtual bool insert(const Elem&) = 0;
  // Append an element at the end of the right partition.
  // Return true if successful, false if the list is full.
  virtual bool append(const Elem&) = 0;
  // Remove the first element of right partition. Return
  // true if successful, false if the list is empty.
  // The element removed is returned in the parameter.
  virtual bool remove(Elem&) = 0;
  // Place fence at list start, making left partition empty.
  virtual void setStart() = 0;
  // Place fence at list end, making right partition empty.
  virtual void setEnd() = 0;
  // Move fence one step left; no change if already at start.
  virtual void prev() = 0;
  // Move fence one step right; no change if already at end
  virtual void next() = 0;
  // Return length of left partition
  virtual int leftLength() const = 0;
  // Return length of right partition
  virtual int rightLength() const = 0;
  // If pos or more elements are in the list, set the size
  // of left partition to pos and return true. Otherwise,
  // do nothing and return false.
  virtual bool setPos(int pos) = 0;
  // Return in first parameter the first element of the
  // right partition. Return true if successful, false 
  // if the right partition is empty.
  virtual bool getValue(Elem&) const = 0;
  // print the contents of the list
  virtual void print() const = 0;
};

// Array-based list implementation -- 线性表的实现
template <class Elem>
class Alist: public List<Elem> //继承
{
 private:
  int maxSize;   // Maximum size of list
  int listSize;   // Actual number of elements in list
  int fence;    // Position of fence
  Elem* listArray;  // Array holding list elementss
 public:
  AList(int size = DefaultListSize) // constructor
  {
  maxSize = size;
  lastSize = fence = 0;
  listArray = new Elem[maxSize];
  }
  ~AList() { delete [] listArray;} // Destructor
  
  void clear()
  {
   delete [] listArray;
   listSize = fence = 0;
   listArray = new Elem[maxSize];
  }
  bool insert(const Elem&);
  bool append(const Elem&);
  bool remove(Elem&);
  void setStart() { fence = 0;}
  void setEnd() { fence = listSize;}
  void prev()  { if (fence != 0) fence--;}
  void next()  { if (fence <= listSize) fence++;}
  int leftLength()const {return fence;}
  int rightLength()const {return lastsize - fence;}
  bool setPos(int pos)
  {
   if ((pos >= 0) && (pos <= listSize)) fence = pos;
   return (pos >= 0) && (pos <= listSize);
  }
  bool getValue(Elem& it)const()
  {
   if (rightLength() == 0) return flase;
   else { it = listArray[fence]; reutrn true;}
  }
  void print()const 
  {
   int temp = 0;
   cout << " < ";
   while (temp < fence) cout << listArray[temp++] << " ";
   cout << " | ";
   while (temp < ListSize) cout << listarray[temp++] << " ";
   cout << " >\n";
  }
};

template <class Elem>   // Insert at front of right partition
bool Alist <Elem>::insert(const Elem& item)
{
 if (listsize == maxSize) return flase; // List is full
 for (int i = listsize; i > fence; i--) // Shift Elem up
  listArray[i] = listArray[i-1];  // to make room
 listArray[fence] = item;
 listSize++;        // Increment list size
 return true;
}

// Remove and return first Elem in right partition
template <class Elem> bool AList <Elem>::remove(Elem& it)
{
 if (rightLength() == 0) return false; // nothing in right
 it = listArray[fence];     // copy removed elem
 for (int i = fence; i < listSize -1; i++) // Shift them down
  listArray[i] = listArray[i + 1];
 listSize--;        // Decrement Size
 return true;
}

// 看完以后就觉得,原来掌握顺序表是很简单的事,只需要掌握几个点;
// 其中最重要的思想是确定位置。
// 知道现在的位置,开始的位置,结束的位置,数组的大小,该位置左右的大小。
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值