数据结构-线性表

来源:互联网 发布:阿拉丁控股集团 知乎 编辑:程序博客网 时间:2024/06/05 23:52

数组实现线性表:

#include <iostream>#include <sstream>#include <string>using namespace std;//纯虚函数//ADT抽象的过程template <class T>class LinearList{public:virtual ~LinearList(){};//为空,返回truevirtual bool empty() const = 0;//返回线性表元素的个数virtual int size() const = 0;//查找:通过theIndex查找元素返回virtual T& get(int theIndex)const = 0;//查找:通过索引查找virtual int indexOf(const T& theElement)const = 0;virtual void DeleteElement(int theIndex) = 0;virtual void insertElement(int theIndex, const T& theElement) = 0;virtual void PrintList(ostream& out) const = 0;//线性表放到输出流中,重载<<};template <class T>//继承的模板的类时候,注意类型名怎么写class ArrayList:public LinearList<T>{public://构造函数+析构函数ArrayList(int initialCapacity = 10);//缺省参数构造函数ArrayList(const ArrayList<T> &);~ArrayList(){ delete[] element; }//ADT 方法bool empty()const { return listSize == 0; };//返回线性表元素的个数int size() const{ return listSize; };//查找:通过theIndex查找元素返回 T& get(int theIndex)const;//查找:通过索引查找int indexOf(const T& theElement)const ;void DeleteElement(int theIndex);void insertElement(int theIndex, const T& theElement);void PrintList(ostream& out) const;//其他方法int Capacity()const{ return arrayLengh; }protected:void checkIndex(int theIndex) const;T* element;//存储线性表的数组int arrayLength;//数组的容量int listSize;//线性元素的个数};//线性表长度可以改变 -----动态数组template <class T >void ChangeLength(T*& a, int oldLength, int newLength){//新的长度有问题if (newLength < 0)throw 1;T* temp = new T[newLength];int number = oldLength>newLength ? oldLength : newLength;//初始位置,终止位置,目标copy(a, a + number, temp);//库函数delete[] a;//释放老空间a = temp;}template <class T>ArrayList<T>::ArrayList(int initialCapacity = 10){if (initialCapacity < 1){throw 1;}arrayLength = initialCapacity;element = new T[arrayLength];listSize = 0;}template <class T>ArrayList<T>::ArrayList(const ArrayList<T>& theList){arrayLength = theList.arrayLength;listSize = theList.ListSize;element = new T[arrayLength];copy(theList.element, theList.element + listSize, element);}template <class T>void  ArrayList<T>::checkIndex(int theIndex) const{//索引从0开始到listSize-1(为数组保持一致)if (theIndex < 0 || theIndex >= listSize){throw 1;}}//通过索引查找元素template <class T>T& ArrayList<T>::get(int theIndex)const{try{checkIndex(theIndex);}catch (int){cout << "索引不存在!" << endl;}return element[theIndex];}template <class T>int ArrayList<T>::indexOf(const T& theElement)const{//find没找到 返回-1;int theIndex = (int)(find(element, element + listSize, theElement) - element);if (theIndex == listSize){return -1;}else{return theIndex;}}template<class T>void ArrayList<T>::DeleteElement(int theIndex){try{checkIndex(theIndex);copy(element + theIndex + 1, element + listSize, element + theIndex);element[--listSize].~T();}catch (int){cout << "索引无效。无法查找" << endl;}}template <typename T>void  ArrayList<T>::insertElement(int theIndex, const T& theElement){if (theIndex<0 || theIndex>listSize){throw 1;}//有效索引,判断数组是否满了if (listSize == arrayLength){try{ChangeLength(element, arrayLength, 2 * arrayLength);arrayLength *= 2;}catch (int){cout << "数组长度必须大于0" << endl;}}//把元素往右移动一个位置copy_backward(element + theIndex, element + listSize, element + listSize + 1);element[theIndex] = theElement;listSize++;//线性表大小+1}//线性表放到输出流中,重载<<template <class T>void ArrayList<T>::PrintList(ostream& out)const{copy(element, element + listSize, ostream_iterator<T>(out, " "));}template <class T>ostream& operator<<(ostream& out, const ArrayList<T>& x){x.PrintList(out);return out;}int main(){ArrayList <char> z(4);try{z.insertElement(0, 'a');z.insertElement(1, 'b');z.insertElement(2, 'c');z.insertElement(3, 'd');z.insertElement(4, 'd');z.insertElement(5, 'd');}catch (int){cout << "插索引错误" << endl;}ArrayList <int> t(4);try{t.insertElement(0, 1);t.insertElement(1, 2);t.insertElement(2, 3);t.insertElement(3, 4);t.insertElement(4, 5);t.insertElement(5, 6);}catch (int){cout << "插索引错误" << endl;}t.DeleteElement(2);t.DeleteElement(7);cout << z << endl;cout << t << endl;return 0;}