利用模板类完成顺序表
#include
using namespace std;
//模板类
template
class SeqTab{
T arr[20];
int maxsize;
public:
SeqTab():maxsize(0){}
void Insert(T a);
void Search(T a);
void Delete(int index);
void Show();
};
//尾插
template
void SeqTab::Insert(T a){
if(maxsize == 20){
cout << "FULL" << endl;
return;
}
arr[maxsize] = a;
maxsize++;
}
//显示所有值
template
void SeqTab::Show(){
for (int i=0; i
cout << arr[i] << ' ';
}
cout << endl;
}
//删除指定数组下标的值
template
void SeqTab::Delete(int index){
if(maxsize == 0){
cout << "NULL" << endl;
return;
}
for (int i=index; i
arr[i] = arr[i+1];
}
maxsize--;
}
//查找值
template
void SeqTab::Search(T a){
if(maxsize == 0){
cout << "NULL" << endl;
return;
}
for (int i=0; i
if(arr[i] == a){
cout << "arr[" << i << "]=" << a <
SeqTab seq;
for (int i=0; i<20; i++) {
seq.Insert(i);
}
seq.Show();
seq.Insert(20);
seq.Delete(0);
seq.Show();
seq.Insert(20);
seq.Show();
seq.Search(5);
return 0;
}
思维导图