#include <iostream>
using namespace std;
template <class T>
class SqListClass{
private:
T *Data;
int Length;
public:
void CreateList(T a[],int n){
Data=new T[200];
Length=0;
for (int i=1;i<=n;i++){
Data[i]=a[i];
}
Length=n;
}
void DeleteList(){
delete [] Data;
}
void DispList(){
cout << Data[1];
for (int i=2;i<=Length;i++){
cout << " " << Data[i];
}
cout << endl;
}
int ListLength(){
return Length;
}
void ListExist(){
if (Length){
cout << "no" << endl;
}
else{
cout << "yes" << endl;
}
}
T GetElem(int i){
if (i<1||i>Length+1) return false;
return Data[i];
}
int LocateElem(T e){
for (int i=1;i<=Length;i++){
if (Data[i]==e) return i;
}
}
bool InsertElem(int P,T e){
if (P<1||P>Length+1){
return false;
}
for (int i=Length+1;i>P;i--){
Data[i]=Data[i-1];
}
Data[P]=e;
Length++;
return true;
}
bool DeleteElem(int P){
if (P<1||P>Length+1){
return false;
}
for (int i=P;i<Length;i++){
Data[i]=Data[i+1];
}
Length--;
}
};
int main(){
SqListClass <char> sqList;
char a[100];
int n;
cin >> n;
for (int i=1;i<=n;i++){
cin >> a[i];
}
sqList.CreateList(a,n);
sqList.DispList();
cout << sqList.ListLength() << endl;
sqList.ListExist();
cout << sqList.GetElem(3) << endl;
cout << sqList.LocateElem('a') << endl;
sqList.InsertElem(4,'f');
sqList.DispList();
sqList.DeleteElem(3);
sqList.DispList();
sqList.DeleteList();
return 0;
}
用c++类构建顺序表
最新推荐文章于 2024-04-27 18:58:47 发布