C/C++泛型编程实现数据结构之线性表

#C/C++泛型编程实现数据结构之线性表

泛型编程与面向对象编程的目标相同,即使重用代码和抽象通用概念的技术更加简单。但是面向对象编程强调编程的数据方面,泛型编程强调的是独立于特定数据类型。侧重点不同。

template <typename DataType,int MaxSize>
class Liner_table {
private:
public:
	DataType data[MaxSize];
	int length;

public:
	Liner_table(DataType arr[]) {									//构造函数,初始化线性表
		length = 0;
		for (int i = 0; arr[i]!='\0'; i++) {
			if (i > MaxSize) { cout << "error" << endl;}
			data[i] = arr[i];
		}
	}
		
	int ListLength() {								//求线性表长度
		return  length;
	}

	DataType GetNode(int position) {						//返回线性表position位置上的元素值
		if(position <= length){
			return data[position];
		}
	}

	int LocateNode(DataType data_info) {					//在线性表中查找元素data_info的位置
		for (int i = 0; i <  length; i++)
		{
			if ( date[i] == data_info) {
				return i;
			}
			else {
				return -1;
			}
		}
	}

	bool InsertList(int position, DataType value) {			//在线性表位置为position前插入元素data
		if ( length + 1 < MaxSize) {
			for (int i = this->length+1; i > position; i++) {
				 data[i] =  data[i-1];
			}
			++ length;
			this->data[position] = value;
			return true;
		}
		else {
			return false;
		}
	}

	bool DeleteList(int position) {							//删除线性表位置为position的元素
		if ( length - 1 < 0) {
			return false;
		}
		else {
			for (int i = position; i <=  length - 1; i++) {
				 data[i] =  data[i + 1];
			}
			-- length;
			return true;
		}
	}

	void purge() {											//删除线性表中所有的重复元素
		for (int i = 0; i < length; i++) {
			for (int j = i + 1; j <= length; j++) {
				if (data[i] == GetNode(j)) {
					DeleteList(i);
				}
			}
		}
	}

	DataType Find_max_value() {
		DataType max = GetNode(0);
		for (int i = 1; i <= length; i++) {
			if (max < GetNode(i)) {
				max = GetNode(i);
			}
		}
		return max;
	}
};
已标记关键词 清除标记
看《数据结构与算法分析》,用书上的代码创建线性表的顺序表。 可是编译出现错误,求解答。 AList.h ``` #include <list> template < class Elem > class AList : public List<Elem> { private: int maxSize; int listSize; int fence; Elem* listArray; public: AList(int size=DefaultListSize) { maxSize = size; listSize = fence = 0; listArrary = new Elem[maxSize]; } ~AList(void) { delete [] listArrary; } void clear() { delete [] listArrary; listArrary = fence = 0; listArrary = 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 liftLength() const { return fence; } int right() const { return listSize - 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 false; else { it = listArrary[fence]; return true; } } void print() const { int temp = 0; cout<<"< "; while(temp < fence) cout<<listArrary[temp++]<<" "; cout<<"| "; while(temp < listSize) cout<<">\n" } }; ``` AList.cpp ``` #include "AList.h" template <class Elem> bool AList<Elem>::insert(const Elem& item) { if(listSize == maxSize) return false; for(int i=listSize;i>fence;i++) listArrary[i] = listArrary[i-1]; listArrary[fence] = item; listSize++; return true; } template <class Elem> bool AList<Elem>::append(const Elem& item) { if(listSize == maxSize) return false; listArrary[listSize++] = item; return true; } template <class Elem> bool AList<Elem>::remove(Elem& it) { if(rightLength() == 0) return false; it = listArrary[fence]; for(int i=fence;i < listSize-1;i++) listArrary[i] = listArrary[i+1]; listSize--; return true; } ``` 编译错误为:alist.h(4): error C2143: 语法错误 : 缺少“,”(在“<”的前面)
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页