#include<iostream>
#include <stdlib.h>
using namespace std;
#define SPACE 100
//顺序查找表的定义
typedef int ElemType;
typedef struct {
ElemType *elem; //数据元素存储空间基址,0 号单元留空
int length; //表长度
}SSTable;
//创建查找表
void CreateTable(SSTable &ST) {
//根据用户输入的长度,创建查找表空间,即为 elem 数组分配空间
//利用循环,提示用户输入查找表数据,并存储在查找表内
//为查找表长度赋值
cout << "请输入查找表的长度";
cin >> ST.length;
ST.elem = new ElemType[SPACE];//动态分布一个数组空间
cout << "请输入数据";
for (int i=1; i< ST.length; i++)
{
cin >> ST.elem[i];
}
}
//输出查找表元素
void Traverse(SSTable ST)
{
for (int i=1; i < ST.length; i++)
cout << ST.elem[i] << " ";
cout << endl;
//利用循环依次输出查找表元素
}
//在顺序表 ST 中顺序查找等于 key 的数据元素。
//若找到,则返回该元素在表中的位置,否则返回 0
int Search_Seq(SSTable &ST, ElemType key) {
ST.elem[0] = key;//设立一个哨兵
int i = ST.length;
for ( i; ST.elem[0] != ST.elem[i]; i--);//不用比
折半查找(c++)
最新推荐文章于 2022-09-29 16:28:44 发布