数据结构实验报告-查找.doc

本章共3道实验题目。 一、线性表的顺序查找 1、定义查找表的顺序存储结构 2、定义创建函数(CreateSTable),实现查找表数据元素的输入 3、定义包含有监视哨的顺序查找函数(Search_Seq) 4、在主函数中调用CreateSTable函数和Search_Seq函数,实现顺序查找功能。 例如: 输入查找表的表长和数据如下: 5 5? 3? 8? 7? 9 输入待查记录的关键字: 8 程序输出计算的位置为: 3 程序: #include iostream using namespace std; int a[100003]; typedef struct ah { int aaaa; }; int main () { int n, c; cin n; for (int i = 1; i = n; ++i) cin a[i]; cin c; for (int i = 1; i = n; ++i) if (a[i] == c) { cout i endl; break; } return 0; } 二、线性表的折半查找——递归实现 1、定义查找表的顺序存储结构 2、定义创建函数(CreateSTable),实现查找表数据元素的输入 3、定义折半查找函数(Search_Bin)——递归实现 4、在主函数中调用CreateSTable函数和Search_Bin函数,实现折半查找功能。 例如: 输入查找表的表长和数据如下: 5 5? 3? 8? 7? 9 输入待查记录的关键字: 8 程序输出—计算的位置为: 3 程序: #includeiostream using namespace std; #define MAXSIZE 100 #define OK 1 typedef int KeyType; typedef struct{ int key;//关键字域 }ElemType; typedef struct{ ElemType *R; int length; }SSTable; //在此处定义 Search_Bin函数; int a[100003]; void Search_half(int l, int r, int key) { int mid = l + ((r - l) 1); if (a[mid] == key) { cout mid endl; return; } if (a[mid] key) { Search_half(l, mid - 1, key); } else { Search_half(mid + 1, r, key); } } int main() { int n, c; cin n; for (int i = 1; i = n; ++i) { cin a[i]; } cin c; for (int i = 1; i = n; ++i) { if (c == a[i]) { cout i endl; break; } } return 0; } 三、线性表的折半查找——非递归实现 1、定义折半查找函数(Search_Bin2)——非递归实现 4、在主函数中调用Search_Bin2函数,实现折半查找功能。 例如: 输入查找表的表长和数据如下: 5 5? 3? 8? 7? 9 输入待查记录的关键字: 8 程序输出计算的位置为: 3 程序: #includeiostream using namespace std; #define MAXSIZE 100 #define OK 1 typedef int KeyType; typedef struct{ int key;//关键字域 }ElemType; typedef struct{ ElemType *R; int length; }SSTable; int a[100003]; int main() { int n, c; cin n; for (int i = 1; i = n; ++i) cin a[i]; cin c; for (int i =

文档评论(0)

1亿VIP精品文档

相关文档