数据结构——BinarySearch两种方法的代码实现

本文主要介绍了如何使用C++实现二分查找,包括详细步骤和代码示例,参考书籍为《Data Structures and Program Design in C++》。内容涵盖了二分查找在有序列表中的应用。
摘要由CSDN通过智能技术生成

Reference Book: 《Data Structures and Program Design in C++》

---------------------------------------------------------------------------------------------------------------------------------------------------

1. Double_Linked_List见文章Double_Linked_List代码实现

2. (1) Record.h

#ifndef RECORD_H
#define RECORD_H

#include <iostream>

using namespace std;

typedef int Key_type;
typedef Key_type Record_type;
class Key
{
    private:
        Key_type key;

    public:
        static Key_type comparisons;
        Key(int x=0);
        Key_type the_key()const;

};
bool operator == (const Key &x, const Key &y);
bool operator > (const Key &x, const Key &y);
bool operator < (const Key &x, const Key &y);
bool operator >= (const Key &x, const Key &y);
bool operator <= (const Key &x, const Key &y);
bool operator != (const Key &x, const Key &y);
ostream &operator << (ostream &output, Key &x);
/*
class Record
{
    public:
        Record(int x=0, int y=0);
        int the_key()const;

    protected:

    private:
        int key;
        int other;
};
*/
#endif // RECORD_H

2. (2) Record.cpp

#include "Record.h"

int Key::comparisons = 0;
Key::Key(int x)
{
    key = x;
}

Key_type Key::the_key()const
{
    return key;
}

bool operator == (const Key &x, const Key &y)
{
    Key::comparisons++;
    return x.the_key()==y.the_key();
}

bool operator > (
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值