#include
using namespace std;
template
class SqList
{
private:
T *elem;
int length;
int listsize;
public:
SqList(int m);
~SqList();
void CreateList(int m);
void Insert(int i, T e);
T Delete(int i);
};
template
SqList::SqList(int m)
{
elem = new T[m];
length = 0;
listsize = m;
}
template
SqList::~SqList()
{
delete[]elem;
length = 0;
listsize = 0;
}
template
void SqList::CreateList(int n)
{
if (n > listsize)throw"参数非法";
cout << "请依次输入" << n << "个元素值:" << endl;
for (int i = 1; i <= n; i++)
cin >> elem[i - 1];
length = n;
}
template
void SqList::Insert(int i, T e)
{
if (length >= listsize)cout << "上溢" << endl;
if (ilength + 1)cout << "插入位置异常" << endl;
for (int j = length; j >= i; j++)
elem[j] = elem[j - 1];
elem[i - 1] = e;
length++;
}
template
T SqList::Delete(int i)
{
T x;
if (length == 0) cout << "删除位置异常" << endl;
x = elem[i - 1];
for (int j = i; j < length; j++)
elem[j - 1] = elem[j];
length--;
return x;
}
typedef int T;
int main()
{
int i;
T e;
SqListL(5);
int choice;
do
{
cout << "1-创建顺序表\n";
cout << "2-在顺序表的第i位置插入元素\n";
cout << "3-删除顺序表中第i个位置的元素\n";
cout << "4-显示表\n";
cout << "5-退出\n";
cout << "Enter choice:";
cin >> choice;
switch (choice)
{
case 1:
cout << "请输入要创建的顺序表中的个数:";
cin >> i;
L.CreateList(i);
cout << endl;
break;
case 2:
cout << "请输入插入的位置:";
cin >> i;
cout << endl;
cout << "请输入插入元素的值:";
cin >> e;
cout << endl;
L.Insert(i,e);
break;
case 3:
cout << "请输入删除的位置:";
cin >> i;
cout << endl;
e = L.Delete(i);
cout << "被删除的元素为:" << e << endl;
break;
case 4:
break;
default:
cout << "Invalid choice !\n";
break;
}
} while (choice != 4);
return 0;
}