1001: 顺序表(1)
Description
已知顺序表类的定义如下,实现各个成员函数。主函数中输入数据(以0结束)利用Insert函数依次将数据插入到表的1号位置,利用DispList按照逻辑次序输出表中元素,再输入一个要查找的元素,利用查找函数Locate查找其在表中的位置,最后利用Reverse函数将数据逆序,再利用DispList输出。
template <class T>
class SeqList{
public:
SeqList(); //构造函数,将表置空
~SeqList(){} //析构
int Locate(T x); //查找元素x在表中的位置,成功返回x在表中的位置,否则返回0
void Insert(int i, T x); //在表的i位置插入元素x
void Reverse(); //逆序表中数据
void DispList();
private:
T data[MaxSize]; //存储元素
int length; //顺序表实际长度
};
Input
输入样例说明:例如输入数据为:1 2 3 4 5 6 0 3,即将1,2,3,4,5,6插入表中的1号位置,得到逻辑次序为6,5,4,3,2,1的顺序表,3为在表中待查找的数据,3的位置为4。
输入:1 2 3 4 5 6 0 3
Output
输出:
The length:6
The elements:
6 5 4 3 2 1
Found position:4
The length:6
The elements:
1 2 3 4 5 6
若查找的数据不存在,则输出:No found,例如下例
输入:1 2 3 4 5 6 0 9
输出:
The length:6
The elements:
6 5 4 3 2 1
No found
The length:6
The elements:
1 2 3 4 5 6
Sample Input
1 2 3 4 5 6 0 3
Sample Output
The length:6
The elements:
6 5 4 3 2 1
Found position:4
The length:6
The elements:
1 2 3 4 5 6
#include <iostream>
using namespace std;
const int MaxSize = 1000;
template<class T>
class SeqList {
public:
SeqList(); //构造函数,将表置空
~SeqList() {} //析构
int Locate(T x); //查找元素x在表中的位置,成功返回x在表中的位置,否则返回0
void Insert(int i, T x); //在表的i位置插入元素x
void Reverse(); //逆序表中数据
void DispList();
private:
T data[MaxSize]; //存储元素
int length; //顺序表实际长度
};
template<class T>
SeqList<T>::SeqList() {
length = 0;//将表置为空
}
template<class T>
int SeqList<T>::Locate(T x) {
for (int i = 0; i < length; ++i) {
if (data[i] == x) {
return i + 1;
}
}
return 0;
}
template<class T>
void SeqList<T>::Reverse() {
for (int i = 0; i < length / 2; i++) {
swap(data[i], data[length - i - 1]);
}
}
template<class T>
void SeqList<T>::Insert(int i, T x) {
if (length >= MaxSize) throw "上溢";
if (i < 1 || i > length + 1) throw "位置";
for (int j = length; j >= i; j--) {
data[j] = data[j - 1];
}
data[i - 1] = x;
length++;
}
template<class T>
void SeqList<T>::DispList() {
for (int i = 0; i < length; ++i) {
cout << data[i] << " ";
}
cout << endl;
}
int main() {
SeqList<int> list;
int num, i = 0;
while (cin >> num, num) {
list.Insert(1, num);
i++;
}
int temp;
cin >> temp;
cout << "The length:" << i << "\nThe elements:\n";
list.DispList();
int locate = list.Locate(temp);
if (locate > 0) {
cout << "Found position:" << locate << endl;
} else {
cout << "No found" << endl;
}
list.Reverse();
cout << "The length:" << i << "\nThe elements:\n";
list.DispList();
return 0;
}