List.h
/******************************************************************** purpose: 公式化描述实现线性表 author: xianyun1230 QQ: 836663997 e-mail: xianyun1230@163.com created: 2014/02/16 *********************************************************************/#includetemplate class List{ public: List(int MaxListSize = 10); ~List(); bool empty() const { return _length == 0;} int length() const { return _length;} int search(const T &x) const; bool del(int k,T &val); bool insert(int k, const T& x); void show(); T operator [](int i){return element[i];} private: int _length; T *element; int MaxSize;};template List ::List(int MaxListSize){ MaxSize = MaxListSize; _length = 0; element = new T[MaxSize];}template List ::~List(){ delete [] element;}template int List ::search(const T &x) const{ for (int i = 0; i < _length && element[i] != x; ++i) continue; if (i != _length) return i-1; else return -1;}template bool List ::del(int k, T &val){ if (k < 0 || k >= _length) return false; int n = k; val = element[n]; while(n < _length - 1) { element[n] = element[n+1]; ++n; } --_length; return true; }template bool List ::insert(int k, const T& x){ if (k < 0 || k > _length || _length == MaxSize) return false; int n = _length - 1; while (k <= n) { element[n + 1] = element[n]; --n; } element[k] = x; ++_length; return true;}template void List ::show(){ using namespace std; cout <<"共" <<_length <<"项:" <
main.cpp
#include "List.h"
int main() { List<int> link(12);link.insert(0,21);link.insert(0,12);link.insert(2,34);link.insert(1,12);link.insert(5,13); //没有第五项,不会插入link.show();int a;link.del(2,a);link.show();std::cout <<a;return 0; }