检索之顺序检索

上代码:

#include<iostream>
#include<vector>
using namespace std;

class Item
{
private:
    int Key;
public:
    Item(){}
    Item(int value):Key(value){}
    int getKey()
    {
        return Key;
    }
    void setKey(const int &K)
    {
        Key = K;
    
    }
};

int SeqSearch(vector<Item *> &dataList, int length, const int &K)
{
    int i = length;
    dataList[0]->setKey(K);
    while(dataList[i]->getKey() != K)
        i--;
    return i;
}

int main()
{
    vector<Item *> datalist(10);
    cout<<"输入Key值:" << endl;
    int key = 0;
    int i=0;
    int ifn;
    while(key >= 0)
    {
        cin>>key;
        datalist[i]=new Item(key);
        i++;
    }
    cout<<"input the Key to be found :" << endl;
    cin>> ifn;
    int fn=SeqSearch(datalist,i-1,ifn);
    if(fn>0)
        cout<<"Find the key int the position :" << fn << endl;
    else
        cout<<"can't find the Key" <<endl;
    return 0;
}

程序运行结果:

posted @ 2013-05-28 23:46  Air Support  阅读(348)  评论(0编辑  收藏  举报