用C++在 exp7lib 项目中编程实现自定义的顺序查找表类 LinearSearchList, 实现其中的造表(Add,Insert)、顺序查找(IndexOf)、二分查找 (BinarySearch) 等 算 法 。
时间: 2023-11-28 19:51:21 浏览: 27
好的,我来为你介绍如何实现 LinearSearchList 类。
首先,我们需要定义 LinearSearchList 类。该类应该包含一个数组用来存储元素,一个整型变量用来记录数组中已经存储的元素个数,以及一些基本的操作函数,如Add、Insert、IndexOf和BinarySearch等。下面是一个简单的 LinearSearchList 类的定义:
```cpp
template <typename T>
class LinearSearchList {
private:
T* data; // 存储元素的数组
int count; // 当前元素个数
int capacity; // 数组容量
public:
LinearSearchList(int capacity); // 构造函数
~LinearSearchList(); // 析构函数
void Add(T value); // 添加元素
void Insert(int index, T value); // 插入元素
int IndexOf(T value); // 查找元素
int BinarySearch(T value); // 二分查找元素
};
```
接下来,我们需要实现 LinearSearchList 类的成员函数。在实现具体算法之前,我们需要先实现一些基本的操作函数,如Add、Insert和IndexOf等。
```cpp
template <typename T>
LinearSearchList<T>::LinearSearchList(int capacity) {
this->count = 0;
this->capacity = capacity;
this->data = new T[capacity];
}
template <typename T>
LinearSearchList<T>::~LinearSearchList() {
delete[] this->data;
}
template <typename T>
void LinearSearchList<T>::Add(T value) {
if (this->count >= this->capacity) {
// 如果数组已满,需要扩容
T* newData = new T[this->capacity * 2];
for (int i = 0; i < this->count; i++) {
newData[i] = this->data[i];
}
delete[] this->data;
this->data = newData;
this->capacity *= 2;
}
this->data[this->count++] = value;
}
template <typename T>
void LinearSearchList<T>::Insert(int index, T value) {
if (index < 0 || index > this->count) {
throw "Index out of range!";
}
if (this->count >= this->capacity) {
// 如果数组已满,需要扩容
T* newData = new T[this->capacity * 2];
for (int i = 0; i < this->count; i++) {
newData[i] = this->data[i];
}
delete[] this->data;
this->data = newData;
this->capacity *= 2;
}
// 将元素依次后移
for (int i = this->count; i > index; i--) {
this->data[i] = this->data[i - 1];
}
// 插入新元素
this->data[index] = value;
this->count++;
}
template <typename T>
int LinearSearchList<T>::IndexOf(T value) {
for (int i = 0; i < this->count; i++) {
if (this->data[i] == value) {
return i;
}
}
return -1;
}
```
现在,我们已经实现了 LinearSearchList 类的基本操作函数。接下来,我们需要实现二分查找算法。
```cpp
template <typename T>
int LinearSearchList<T>::BinarySearch(T value) {
int left = 0;
int right = this->count - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (this->data[mid] == value) {
return mid;
} else if (this->data[mid] < value) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
```
最后,我们需要在主函数中测试 LinearSearchList 类的实现。
```cpp
#include <iostream>
#include "LinearSearchList.h"
using namespace std;
int main() {
LinearSearchList<int> list(10);
list.Add(1);
list.Add(3);
list.Add(5);
list.Add(7);
list.Add(9);
list.Insert(2, 4);
list.Insert(4, 6);
list.Insert(6, 8);
cout << "List: ";
for (int i = 0; i < list.Count(); i++) {
cout << list[i] << " ";
}
cout << endl;
cout << "IndexOf 5: " << list.IndexOf(5) << endl;
cout << "IndexOf 10: " << list.IndexOf(10) << endl;
cout << "BinarySearch 5: " << list.BinarySearch(5) << endl;
cout << "BinarySearch 10: " << list.BinarySearch(10) << endl;
return 0;
}
```