顺序表、搜索表的类定义以及顺序搜索:
#include <iostream> #include <assert.h> using namespace std; const int defaultSize=100; template <class E,class K> class dataList; //数据表类的前视定义 template <class E,class K> class dataNode{ //数据表中结点类的定义 friend class dataList<E,K>; //声明友元类为dataList public: dataNode(const K x):key(x){} K getKey()const{return key;} //表的存储结构封装在类的私有或保护部分 void setKey(K k){key=k;} //通过getKey()和setKey()函数对关键码进行存取 private: K key; }; template <class E,class K> class dataList{ //数据表类定义 public: dataList(int sz=defaultSize):ArraySize(sz),CurrentSize(0){ Element=new dataNode<E,K>[sz]; assert(Element!=NULL); } dataList(dataList<E,K>& R); virtual ~dataList(){delete []Element;} virtual int Length(){return CurrentSize;} virtual K getKey(int i)const{ //提取第i个元素的值,i从1开始 assert(i>0||i<=CurrentSize); return Element[i-1].key; } virtual void setKey(K x,int i){ //修改第i个元素的值,i从1开始 assert(i>0||i<=CurrentSize); Element[i-1].key=x; } virtual int SeqSearch(const K x)const; virtual int SeqSearch(const K x,int loc)const; virtual bool Remove(const K x,E& el); virtual bool Insert(E& el); friend ostream& operator<<(ostream& out,const dataList<E,K>& OutList); friend istream& operator>>(istream& in,dataList<E,K>& InList); private: dataNode<E,K> *Element; int ArraySize,CurrentSize; }; template <class E,class K> class searchList:public dataList<E,K>{ //搜索表类继承dataList,并且增加了成员函数Search() public: searchList(int sz=defaultSize):dataList<E,K>(sz){} virtual int SeqSearch(const K x)const; }; template <class E,class K> bool dataList<E,K>::Insert(E& el){ if(CurrentSize==ArraySize) return false; Element[CurrentSize]=el; //在dataList尾部插入元素el CurrentSize++; return true; }; template <class E,class K> bool dataList<E,K>::Remove(const K x,E& el){ if(CurrentSize==0) return false; int i; for (i = 0; i <CurrentSize&&Element[i].key!=x; i++); if(i==CurrentSize) return false; el=Element[i]; Element[i]=Element[CurrentSize-1]; //用尾元素填充被删除元素 CurrentSize--; return true; } template <class E,class K> ostream& operator<<(ostream& out, const dataList<E,K>& OutList){ out<<"Array Contents: "; for (int i = 1; i <=OutList.CurrentSize; i++) out<<OutList.Element[i-1].getKey()<<''; out<<endl;. out<<"Array Current Size:"<<OutList.CurrentSize<<endl; return out; }; template <class E,class K> istream& operator>>(istream& in,dataList<E,K>& InList){ cout<<"Enter array Current Size:"; in>>InList.CurrentSize; cout<<"Enter array elements: "; for (int i = 1; i <=InList.CurrentSize; i++) { cout<<"Element"<<i<<"i"; in>>InList.Element[i-1]; } return in; }; template <class E,class K> int searchList<E,K>::SeqSearch(const K x) const { //使用监视哨的顺序搜索算法,第CurrentSize号位置作为控制搜索过程自动结束第监视哨使用 Element[CurrentSize].key=x; //将x设置为监视哨 int i=0; while(Element[i].key!=x) i++; return i+1; } template <class E,class K> int searchList<E,K>::SeqSearch(const K x,int loc)const{ //顺序搜索的递归算法,从第loc个元素开始搜索 if(loc>CurrentSize) return 0; //先判断是否检测到表尾,此时loc停留在CurrentSize+1的位置。搜索不成功,返回0 else if(Element[loc-1].key==x) return loc; else return SeqSearch1(x,loc+1); } const int Size=10; main(){ searchList<int>L1(Size); int Target; int Location; cin>>L1; cout<<L1; cout<<"Search for a integer:"; cin>>Target; //输入要搜索的数据 if((Location=L1.Seqsearch(Target))!=L1.Length()) cout<<"Found at index"<<Location<<endl; else cout<<"Not found. "; }
基于有序顺序表的顺序搜索和折半搜索:
#include "SeqList.h" template <class E,class K> class SortedList:public SearchList<E,K>{ public: SortedList(int sz=100):SearchList<E,K>(sz){} ~SortedList(){} int SequentSearch(const K x)const; int BinarySearch(const K x)const; int BinarySearch(const K x,int low,int high); bool Insert(const E& el); int Begin(){return(CurrentSize==0)?0:1;} int Next(int i){return(i>=1&&&i<=CurrentSize)?i+1:0;} } template <class E,class K> int SortedList<E,K>::SequentSearch(const K x)const{ for(int i=1;i<=CurrentSize;i++){ if(Element[i-1].key==x) return i; else if(Element[i-1].key>x) break; } return 0; //顺序搜索失败,且x大于所有关键码 } template <class E,class K> int SortedList<E,K>::BinarySearch(const K x)const{ //迭代算法 int high=CurrentSize-1,low=0,mid; while(low<=high){ mid=(low+high)/2; if(x>Element[mid].key) low=mid+1; else if(x<Element[mid].key) high=mid-1; else return mid+1; //搜索成功 } return 0; //搜索失败 } template <class E,class K> int SortedList<E,K>::BinarySearch(const K x,int low,int high){ //递归算法 int mid=0; if(low<=high){ mid=(low+high)/2; if(x>Element[mid-1].key) mid=BinarySearch(x,mid+1,high); else if(x<Element[i-1].key) mid=BinarySearch(x,low,mid-1); } return mid; } template <class E,class K> void SortedList<E,K>::Insert(const E& el)const{ assert(CurrentSize<ArraySize); while(i<=CurrentSize&&Element[i-1].key<=el.key) i++; for(int j=CurrentSize;j>=i;j--) Element[j]=Element[j-1]; Element[i-1]=el; CurrentSize++; }