顺序表Test1
2017-09-17 09:38
197 查看
//ArrayList.h文件
const int MaxSize = 50;
template<class T> //定义模板类ArrayList
class ArrayList
{
public:
ArrayList() { length = 0 }; //无参构造函数
ArrayList(T a[], int n); //有参构造函数
~ArrayList() {};
void PrintList();
bool InsertList(int i, T a);
bool DeleteList(int i, T *a);
int Length();
int GetList(T x);
T GetNum(int i);
private:
int length; //线性表的长度
T data[MaxSize]; //存放线性表的数组
};
//ArrayList.cpp文件
#include"ArrayList.h"
template<class T>
ArrayList<T>::ArrayList(T a[], int n)
{
if (n > MaxSize)
throw("参数非法");
for (int i = 0; i < n; i++)
{
data[i] = a[i];
}
length = n;
}
template<class T>
bool ArrayList<T>::DeleteList(int i, T *x)
{
if (i>length || i<1)
throw("参数非法");
else
{
*x = data[i - 1];
for (int j = i; j < length; j++)
{
data[j - 1] = data[j];
}
length--;
return true;
}
}
template<class T>
int ArrayList<T>::GetList(T a)
{
for (int i = 0; i < length; i++)
{
if (a == data[i])
{
return i + 1;
}
}
return -1;
}
template<class T>
T ArrayList<T>::GetNum(int x)
{
if (x > length || x < 1)
{
throw "查找位置非法";
}
else
return data[x - 1];
}
template<class T>
bool ArrayList<T>::InsertList(int i, T a)
{
if (i > length + 1 || i < 1)
{
throw("参数错误");
}
else
{
for (int j = length; j >= i; j--)
{
data[j] = data[j - 1];
}
data[i - 1] = a;
length++;
}
}
template<class T>
int ArrayList<T>::Length()
{
return length;
}
template<class T>
void ArrayList<T>::PrintList()
{
for (int i = 0; i < length; i++)
{
cout << data[i] << endl;
}
}
//源.cpp文件
#include<iostream>
#include<string>
using namespace std;
#include"ArrayList.cpp"
int main(void)
{
string a[5] = {"大明","二明","三明","四明","五明"};
ArrayList<string> x(a,5);
cout << "分别是:" << endl;
x.PrintList();
cout <<"总共人数:"<< x.Length() << endl;
cout<<"小明的位置是:"<<x.GetList("小明")<<endl;
cout<<"第三个位置的学生是:"<<x.GetNum(3)<<endl;
x.InsertList(6, "小明");
cout << "插入操作:" << endl;
x.PrintList();
string b;
x.DeleteList(2,&b);
cout << "删除操作:" <<"(删除的学生姓名是"<<b<<")"<< endl;
x.PrintList();
x.~ArrayList();
system("pause");
return 0;
}
运行结果如下:
const int MaxSize = 50;
template<class T> //定义模板类ArrayList
class ArrayList
{
public:
ArrayList() { length = 0 }; //无参构造函数
ArrayList(T a[], int n); //有参构造函数
~ArrayList() {};
void PrintList();
bool InsertList(int i, T a);
bool DeleteList(int i, T *a);
int Length();
int GetList(T x);
T GetNum(int i);
private:
int length; //线性表的长度
T data[MaxSize]; //存放线性表的数组
};
//ArrayList.cpp文件
#include"ArrayList.h"
template<class T>
ArrayList<T>::ArrayList(T a[], int n)
{
if (n > MaxSize)
throw("参数非法");
for (int i = 0; i < n; i++)
{
data[i] = a[i];
}
length = n;
}
template<class T>
bool ArrayList<T>::DeleteList(int i, T *x)
{
if (i>length || i<1)
throw("参数非法");
else
{
*x = data[i - 1];
for (int j = i; j < length; j++)
{
data[j - 1] = data[j];
}
length--;
return true;
}
}
template<class T>
int ArrayList<T>::GetList(T a)
{
for (int i = 0; i < length; i++)
{
if (a == data[i])
{
return i + 1;
}
}
return -1;
}
template<class T>
T ArrayList<T>::GetNum(int x)
{
if (x > length || x < 1)
{
throw "查找位置非法";
}
else
return data[x - 1];
}
template<class T>
bool ArrayList<T>::InsertList(int i, T a)
{
if (i > length + 1 || i < 1)
{
throw("参数错误");
}
else
{
for (int j = length; j >= i; j--)
{
data[j] = data[j - 1];
}
data[i - 1] = a;
length++;
}
}
template<class T>
int ArrayList<T>::Length()
{
return length;
}
template<class T>
void ArrayList<T>::PrintList()
{
for (int i = 0; i < length; i++)
{
cout << data[i] << endl;
}
}
//源.cpp文件
#include<iostream>
#include<string>
using namespace std;
#include"ArrayList.cpp"
int main(void)
{
string a[5] = {"大明","二明","三明","四明","五明"};
ArrayList<string> x(a,5);
cout << "分别是:" << endl;
x.PrintList();
cout <<"总共人数:"<< x.Length() << endl;
cout<<"小明的位置是:"<<x.GetList("小明")<<endl;
cout<<"第三个位置的学生是:"<<x.GetNum(3)<<endl;
x.InsertList(6, "小明");
cout << "插入操作:" << endl;
x.PrintList();
string b;
x.DeleteList(2,&b);
cout << "删除操作:" <<"(删除的学生姓名是"<<b<<")"<< endl;
x.PrintList();
x.~ArrayList();
system("pause");
return 0;
}
运行结果如下:
相关文章推荐
- python unittest控制用例的执行顺序
- testNG中同一个test节点上class的执行顺序
- python修改python unittest的运行顺序
- 请运行TestStaticInitializeBlock.java示例,观察输出结果,总结出“静态初始化块的执行顺序”。
- unity3d shader控制渲染顺序,Queue,ZWrite,ZTest
- jmeter: Execution order, jmeter test plan执行顺序
- junit4按顺序执行test方法
- unittest 执行测试用例的顺序
- JUnit中@Test的执行顺序
- google test,libgtest.a和libgtest_main.a的链接顺序问题
- OVM的run.test的执行顺序
- selenium---unittest框架测试用例函数执行顺序 优先级
- 关于JavaScript的执行顺序,如果多个js中(a.js,b.js,c.js)都有一个方法(如test方法),那么如果再html中触发执行该test方法,是执行的那个js中的test方法(a.)?
- 第二种方式,修改python unittest的执行顺序,使用猴子补丁
- junit中before和beforeclass,多个test执行顺序
- 【程序37】Test3Quit.java 题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最 //后留下 的是原来第几号的那位。
- selenium---unittest框架测试用例函数执行顺序 优先级
- JUnit中@Test的运行顺序
- selenium---unittest框架测试用例函数执行顺序 优先级