#include <iostream>
using namespace std;
#define MaxLength 100
class SequentialList{
private:
int length;//长度
int data[MaxLength];
public:
SequentialList(){
// 初始化
this->length = 0;
};
// 复制构造函数
SequentialList(const SequentialList &seq){
this->length = seq.length;
for (int i = 0; i < seq.length; ++i) {
this->data[i] = seq.data[i];
}
}
// 添加
void add(int value){
if(this->length<MaxLength){
data[length++]=value;
} else{
cout<<"已满,添加失败"<<endl;
}
}
void del(){
if(this->length==0){
cout<<"顺序表为空"<<endl;
return;
}
cout<<"删除成功!"<<endl;
this->length--;
}
// 删除
void del(int pos){
for (int i = pos; i < this->length; ++i) {
this->data[i-1] = this->data[i];
}
this->length--;
};
void insert(int pos,int value)
{
if(this->length>MaxLength){
cout<<"禁止插入"<<endl;
return ;
} else{
if(pos>0&&pos<this->length){
for (int i = this->length; i >=pos-1; --i) {
this->data[i+1] = this->data[i];
}
}
this->length++;
this->data[pos-1]=value;
}
}
// 打印
void printInfo(){
for (int i = 0; i <this->length; ++i) {
cout<<data[i]<<endl;
}
}
// 返回顺序表长度
int getLength(){
return this->length;
}
void clear(){
this->length=0;
}
// 获取位置上的数值
int getValue(int pos){
return this->data[pos-1];
}
// 判断是否为空空
int isEmpty(){
return this->length==0;
}
~SequentialList(){
cout<<"释放成功!"<<endl;
}
};
int main(){
SequentialList seq = SequentialList();
for (int i = 0; i <5 ; ++i) {
seq.add(i);
}
seq.printInfo();
cout<<"长度为:"<<seq.getLength()<<endl;
seq.del(2);
seq.printInfo();
cout<<"长度为:"<<seq.getLength()<<endl;
seq.insert(2,1);
seq.printInfo();
return 0;
}