下面是用 数组实现的 顺序表
template<typename T>
struct sq_LList {
private:
int mm; //the max length
int nn; //the length
T *v; // the array
public:
sq_LList() {
mm = 0;
nn = 0;
v = NULL;
}
sq_LList(int n) {
mm = n;
nn = 0;
v = new T[n];
}
int Insert(int position, T element) {
if (nn >= mm) {
std::cout << "overflow" << std::endl;
return false;
} else {
if (position < 1)
position = 1;
if (position > nn)
position = nn + 1;
for (int k = nn; k >= position; k--)
{
v[k] = v[k - 1];
}
v[position - 1] = element;
++nn;
return true;
}
}
int flag ()
{
if(nn==mm) return -1;
if(nn==0) return 0;
return 1;
}
void Display()
{
for(int i=0;i<nn;++i)
std::cout<<v[i]<<" "<<std::endl;
}
T Remove (int position)
{
if(position<1 && position>nn)
return T(0);
else
{
T temp=v[position-1];
for(int k=position;k<nn;++k)
{
v[k-1]=v[k];
}
v[nn-1]=0;
--nn;
return temp;
}
}
~sq_LList()
{
delete [] v;
}
};
#include<iostream>
int main(int argc,char *argv[])
{
sq_LList<int> test(20);
test.Insert(1,123);
test.Display();
return true;
}
使用的是 eclipse + cdt 写的