#include <iostream>
#include <cstring>
#include <string>
#include <bits/stdc++.h>
#define REP(i, a, b) for(int i = a; i < b; i++)
#define REP_(i, a, b) for(int i = a; i <= b; i++)
#define sl(n) scanf("%lld", &n);
#define si(n) scanf("%d", &n);
#define RepAll(a) for(auto x: a)
#define cout(ans) cout << ans << endl;
typedef long long ll;
const int maxn = 1e5 + 10;
/**
* This code has been written by YueGuang, feel free to ask me question. Blog: http://www.yx.telstudy.xyz
* created:2019.10.21
* 顺序表的实现
*/
using namespace std;
template <class T>
class SeqList{
private:
T data[maxn];//顺序表元素
int length; //顺序表当前长度
public:
/*
* 声明及定义SeqList模板类;
* 初始化空的顺序表;
* 使用一维数组初始化顺序表;
* 返回顺序表的长度;
* 在顺序表中按位查找元素;
* 在顺序表中按值查找元素;
* 在顺序表中插入元素;
* 在顺序表中删除元素;
* 遍历顺序表。
*/
//清 0初始化
SeqList(){
memset(data, 0, sizeof(data));
length = 0;
}
//一维数组初始化
SeqList(T *rdata, int size){
length = 0;
if(size >= maxn - 2||size < 0){
throw "error";
}
else
for(int i = 0; i < size; i++) {
length++;
data[i] = rdata[i];
}
}
//返回长度
int GetLength(){
return length;
}
//按位置查找
T Floc(int i){
if(i >= length || i < 0){throw "error";}
else
return data[i];
}
//按值查找
int Fval(T rdata){
for(int i = 0; i < length; i++){
if(data[i] == rdata){
return i;
}
}
return -1;
}
//插入
int Ins(T InsData,int InsPla){
if(InsPla < length && InsPla >= 0 && length < maxn - 2){
for(int i = length; i - 1 >= InsPla; --i){
data[i] = data[i - 1];
}
length++;
}
else if(length >= maxn - 2){
return printf("can no ins anything") * 0;
}
else{
return printf("error, check again") * 0;
}
}
//删除p位置上的元素
void Delp(int p){
if(p < length && p >= 0){
for(int i = p; i+1 < length; i++){
data[i] = data[i+1];
}
length--;
return ;
}
else{
printf("error, check again");
return ;
}
}
//删除所有值为d的
void Dald(T d){
int Tmplen = length;
REP(i, 0, length){
if(data[i] == d){
for(int j = i ; j+1 < length; j++){
data[j] = data[j+1];
}
length--;
}
}
printf("%d delements deleted
",Tmplen - length);
printf("now len = %d", length);
return ;
}
//遍历顺序表输出
void Pri(){
for(int i = 0; i < length; i++){
i == 0 ? cout << data[i] : cout << " " << data[i];
}
cout << '
';
return ;
}
};
int main(){
int n;
cout << "please input n
";
scanf("%d", &n);
int *inp = new int [n + 1];
cout << "please input n ele
";
for(int i = 0; i < n; i++){
cin >> inp[i];
}
cout << "使用一维数组赋值
";
SeqList<int> first(inp, n);
cout << "输出测试" << '
';
first.Pri();
cout << "输出长度" << '
' << first.GetLength() << '
';
cout << "输入i值, 插入到j位置上" << '
';
int i, j;
cin >> i >> j;
first.Ins(i, j);
cout << "输出测试" << '
';
first.Pri();
int k;
cout << "输入k进行按位置查找(0 <= k <= " << first.GetLength() <<")" << '
';
cin >> k;
cout << "查找到 : " << first.Floc(k) << '
';
}