|
发表于 2018-4-16 20:25:33
|
显示全部楼层
回帖奖励 +20 鱼币
#include <iostream>
using namespace std;
template<typename T,int size>class SeqList{
T slist[size];
int maxsize;
int last;
public:
SeqList(){last=-1;maxsize=size;}
void insert(T a);
void insertOrder(T a);
void show();
int searchOrder(T key);
bool delete1(int k);
void delete2();
};
template<typename T,int size>void SeqList<T,size>::insert(T a){
last++;
slist[last]=a;
}
template<typename T,int size>void SeqList<T,size>:: insertOrder(T a){
last++;
slist[last]=a;
int j=last;
while(j>0 && a<slist[j-1]){
slist[j]=slist[j-1];
j--;}
slist[j]=a;
}
template<typename T,int size>void SeqList<T,size>::show(){
int i=0;
for(i=0;i<=last;i++){
cout<<slist[i]<<'\t';
}
}
template<typename T,int size>int SeqList<T,size>::searchOrder(T key){
int i=0;
while(i<=last && slist[i]!=key)i++;
if(i>last) return -1;
else return i;
}
template<typename T,int size>bool SeqList<T,size>::delete1(int k){
if(k>=0){
last--;
for(int j=k;j<=last;j++)slist[j]=slist[j+1];
return true;
}
return false;
}
template<typename T,int size>void SeqList<T,size>:: delete2(){
for(int i=0;i<last;i++){
if(slist[i]==slist[i+1]){
last--;
for(int j=i;j<=last;j++)slist[j]=slist[j+1];
}
}
}
class Element{
int key;
public:
bool operator < (Element ele){return key<ele.key;}
bool operator == (Element ele){return key==ele.key;}
};
int main(){
SeqList<int,20> l1;
int a;
cout<<"输入数组1:";
for(int i=0;i<10;i++){
cin>>a;
l1.insertOrder(a);
}
cout<<"输入数组2:";
for(i=0;i<10;i++){
cin>>a;
l1.insertOrder(a);
}
l1.delete2();
l1.show(); cout<<endl;
return 0;
} |
|