#include<iostream>
#define LIST_MAXSIZE 100
#define LIST_INCREMENT 10
using namespace std;
class SqList{
protected:
int *elem;
int listSize;
int n;
public:
SqList();
virtual~SqList();
int insertData();
int bin_Search(int key);
void clear();
int deleteElem(int i,int &e);
int getLength();
int insert(int i,int e);
int getElem(int i,int &s);
bool isEmpty();
int display();
};
int SqList::bin_Search(int key){
int low,mid,high;
low=0; high=n-1;
while(low<=high){
mid=(low+high)/2;
if(elem[mid]==key)
return mid+1;
else if(elem[mid]<key)
low=mid+1;
else if(elem[mid]>key)
high=mid-1;
}
return 0;
}
int SqList::deleteElem(int i,int &e){
if(i<1||i>n) return 1;
e=elem[i-1];
for(int j=i+1;j<=n;++j)
elem[j-2]=elem[j-1];
n--;
return 0;
}
void SqList::clear(){
n=0;
}
int SqList::getLength(){
return n;
}
int SqList::insert(int i,int e){
int *newbase;
if(i<1||i>n) return 1;
if(n>=listSize){
newbase=new int[listSize+LIST_INCREMENT];
for(int j=1;j<=n;++j)
newbase[j-1]=elem[j-1];
delete[] elem;
elem=newbase;
listSize+=LIST_INCREMENT;
}
for(int j=n;j>=i;--j)
elem[j]=elem[j-1];
elem[i-1]=e;
++n;
return 0;
}
int SqList::getElem(int i,int &s){
s=elem[i-1];
return 0;
}
bool SqList::isEmpty(){
return n? false:true;
}
SqList::SqList(){
elem=new int[LIST_MAXSIZE];
listSize=LIST_MAXSIZE;
n=0;
}
int SqList::display(){
int i=0;
while(i<n){
cout<<elem[i]<<" ";
i++;
}
cout<<endl;
return 0;
}
int SqList::insertData(){
cout<<"按升序输入个整数"<<endl;
for(int i=0;i<10;i++){
cin>>elem[i];
n++;
}
return 0;
}
SqList::~SqList(){
delete[] elem;
}
int main(){
int c;//要插入的位置
int g;//要插入位置的数
int s;//要查找位置的数
int e;//要删除的数
int b;//折半查找的数
int f;//你查找的位置的数
SqList a;
a.insertData();
cout<<"输入你要插入的位置和插入的数"<<endl;
cin>>c>>g;
a.insert(c,g);
cout<<"此时顺序表为"<<endl;
a.display();
cout<<"为了折半查找,我们删除了你插入你那个元素,因为你插入的元素可能是这个顺序表不为升序"<<endl;
a.deleteElem(c,e);
cout<<"此时顺序表为"<<endl;
a.display();
cout<<"输入你要查找的那个数"<<endl;
cin>>b;
cout<<endl<<"这个数在顺序表中的第"<<a.bin_Search(b)<<"个"<<endl;
cout<<"请输入你要查找的位置"<<endl;
cin>>f;
a.getElem(f,s);
cout<<"你查找位置的数为"<<s<<endl;
return 0;
}