// LinerList.cpp : 定义控制台应用程序的入口点。//c++语言编写, //实现线性表中的顺序存储结构,用动态申请的数组实现,注意释放内存 //实现表的构造、析构、插入、删除、显示操作#include "stdafx.h"#includeusing namespace std;template class LinerListSqu{ T*elem;//表首地址 int length;//表长 long maxsize;//表容量public: LinerListSqu(int);//构造函数 ~LinerListSqu();//析构函数 bool insertElem(int k, const T&x);//在k个元素之后插入x bool deleteElem(int k, T&x);//删除第k个元素并将它返回至x中 void display();};template LinerListSqu ::LinerListSqu(int num){ elem = new T[num]; length = 0; maxsize = num;}template bool LinerListSqu ::insertElem(int k,const T&x){ if (maxsize == 0 || k<0 || k>length) return false; if (length >= maxsize){ elem = (T*)realloc(elem, maxsize + sizeof(T)*maxsize); maxsize *= 2; } for (int i = length-1+1 ; i > k; i--){ elem[i] = elem[i - 1]; } elem[k] = x; length++; return true;}template bool LinerListSqu ::deleteElem(int k, T&x){ if (length == 0 || k<1 || k>length) return false; for (int i = k -1; i < length - 1; i++) elem[i] = elem[i + 1]; length--; return true;}template void LinerListSqu ::display(){ int len = length; T *p = elem; while (len--) cout << *p++<<","; cout << endl;}template LinerListSqu ::~LinerListSqu(){ delete elem;}int _tmain(int argc, _TCHAR* argv[]){ LinerListSqu L(5); for (int i = 1; i <= 5; i++) L.insertElem(i-1, i*2); L.display(); L.insertElem(3, 0); L.display(); int x; L.deleteElem(3, x); L.display(); cout << x; return 0;}