以下是《数据结构实验指导书 实验五 查找.pdf》中的所有代码:
### 【SeqSearch.h】
```cpp
#ifndef INC_SEQSEARCH_H
#define INC_SEQSEARCH_H
#include <stdio.h>
typedef int InfoType;
typedef int KeyType;
typedef struct {
KeyType key;
InfoType data;
} RecType;
int SeqSearch(RecType R[], int n, KeyType k) {
int i = 0;
while (i < n) {
if (R[i].key != k) {
printf("第%d次:当前关键字为%d,与查找关键字%d不相等,继续查找下一个记录。\n", i + 1, R[i].key, k);
i++;
} else {
printf("第%d次:当前关键字为%d,与查找关键字%d相等,查找结束。\n", i + 1, R[i].key, k);
break;
}
}
if (i >= n) {
printf("查找失败,查找表中不存在关键字%d。\n", k);
return 0;
} else {
printf("查找成功,关键字为%d的记录是查找表中第%d个记录。\n", k, i + 1);
return i + 1;
}
}
#endif
```
### 【SeqSearch.cpp】
```cpp
#include <stdio.h>
#include "SeqSearch.h"
#define MAXSIZE 10
int main(void) {
int n = MAXSIZE;
int key[MAXSIZE] = {73, 61, 52, 10, 26, 98, 85, 47, 4, 39};
RecType R[MAXSIZE];
for (int i = 0; i < MAXSIZE; i++) {
R[i].key = key[i];
}
printf("实验五:查找,问题一:顺序查找,完成人:XXX\n");
printf("(1)查找关键字10:\n");
SeqSearch(R, MAXSIZE, 10);
printf("(2)查找关键字100:\n");
SeqSearch(R, MAXSIZE, 100);
return 0;
}
```
### 【BinSearch.h】
```cpp
#ifndef INC_BINSEARCH_H
#define INC_BINSEARCH_H
#include <stdio.h>
typedef int InfoType;
typedef int KeyType;
typedef struct {
KeyType key;
InfoType data;
} RecType;
int BinSearch(RecType R[], int low, int high, KeyType k) {
int mid;
int c = 0;
while (low <= high) {
mid = (low + high) / 2;
c += 1;
if (R[mid].key < k) {
low = mid + 1;
printf("第%d次:当前关键字为%d,与查找关键字%d不相等,继续查找右区间。\n", c, R[mid].key, k);
} else if (R[mid].key > k) {
high = mid - 1;
printf("第%d次:当前关键字为%d,与查找关键字%d不相等,继续查找左区间。\n", c, R[mid].key, k);
} else {
printf("第%d次:当前关键字为%d,与查找关键字%d相等,查找结束。\n", c, R[mid].key, k);
return mid + 1;
}
}
printf("查找失败,查找表中不存在关键字%d。\n", k);
return 0;
}
int BinSearch2(RecType R[], int low, int high, KeyType k, int c) {
int mid;
if (low > high) {
printf("查找失败,查找表中不存在关键字%d。\n", k);
return 0;
} else {
mid = (low + high) / 2;
c += 1;
if (R[mid].key < k) {
printf("第%d次:当前关键字为%d,与查找关键字%d不相等,继续查找右区间。\n", c, R[mid].key, k);
return BinSearch2(R, mid + 1, high, k, c);
} else if (R[mid].key > k) {
printf("第%d次:当前关键字为%d,与查找关键字%d不相等,继续查找左区间。\n", c, R[mid].key, k);
return BinSearch2(R, low, mid - 1, k, c);
} else {
printf("第%d次:当前关键字为%d,与查找关键字%d相等,查找结束。\n", c, R[mid].key, k);
return mid + 1;
}
}
}
#endif
```
### 【BinSearch.cpp】
```cpp
#include <stdio.h>
#include "BinSearch.h"
#define MAXSIZE 10
int main(void) {
int n = MAXSIZE;
int key[MAXSIZE] = {4, 10, 26, 39, 47, 52, 61, 73, 85, 98};
RecType R[MAXSIZE];
for (int i = 0; i < MAXSIZE; i++) {
R[i].key = key[i];
}
printf("实验五:查找,问题二:折半查找,完成人:XXX\n");
printf("(1)查找关键字10:\n");
BinSearch(R, 0, MAXSIZE - 1, 10);
printf("(2)查找关键字100:\n");
BinSearch(R, 0, MAXSIZE - 1, 100);
return 0;
}
```
### 【BinSearchTree.h】
```cpp
#ifndef INC_BINSEARCHTREE_H
#define INC_BINSEARCHTREE_H
#include <stdlib.h>
#include <stdio.h>
typedef int KeyType;
typedef int InfoType;
typedef struct node {
KeyType key;
InfoType data;
struct node *lchild;
struct node *rchild;
} BSTNode;
BSTNode *Insert(BSTNode *&bt, KeyType k) {
if (bt == NULL) {
bt = (BSTNode *)malloc(sizeof(BSTNode));
bt->key = k;
bt->lchild = bt->rchild = NULL;
} else {
if (k < bt->key)
bt->lchild = Insert(bt->lchild, k);
else if (k > bt->key)
bt->rchild = Insert(bt->rchild, k);
}
return bt;
}
BSTNode *Create(KeyType k[], int n) {
int i = 0;
BSTNode *bt = NULL;
while (i < n) {
bt = Insert(bt, k[i]);
i += 1;
}
return bt;
}
void Destroy(BSTNode *bt) {
if (bt != NULL) {
Destroy(bt->lchild);
Destroy(bt->rchild);
free(bt);
}
}
BSTNode *Search(BSTNode *bt, KeyType k) {
if (bt == NULL) {
printf("树表中没有关键字%d,查找失败。\n", k);
return NULL;
} else {
if (k == bt->key) {
printf("%d与当前结点的关键字%d相等,查找成功。\n", k, bt->key);
return bt;
} else if (k < bt->key) {
printf("%d小于当前结点的关键字%d,继续在左子树中查找。\n", k, bt->key);
return Search(bt->lchild, k);
} else {
printf("%d大于当前结点的关键字%d,继续在右子树中查找。\n", k, bt->key);
return Search(bt->rchild, k);
}
}
}
BSTNode *Search2(BSTNode *bt, KeyType k) {
BSTNode *rt = bt;
while (rt != NULL) {
if (k == rt->key) {
printf("%d与当前结点的关键字%d相等,查找成功。\n", k, rt->key);
return rt;
} else if (k < rt->key) {
printf("%d小于当前结点的关键字%d,继续在左子树中查找。\n", k, rt->key);
rt = rt->lchild;
} else {
printf("%d大于当前结点的关键字%d,继续在右子树中查找。\n", k, rt->key);
rt = rt->rchild;
}
}
printf("树表中没有关键字%d,查找失败。\n", k);
return NULL;
}
#endif
```
### 【BinSearchTree.cpp】
```cpp
#include "BinSearchTree.h"
#include <stdio.h>
#define MAXSIZE 10
int main(void) {
int n = MAXSIZE;
BSTNode *bt = NULL;
int key[MAXSIZE] = {73, 61, 52, 10, 26, 98, 85, 47, 4, 39};
bt = Create(key, MAXSIZE);
printf("实验五:查找,问题三:树表查找,完成人:XXX\n");
printf("(1)查找关键字98:\n");
Search2(bt, 98);
printf("(2)查找关键字42:\n");
Search2(bt, 42);
Destroy(bt);
return 0;
}
```
### 【Hash.h】
```cpp
#ifndef INC_HASH_H
#define INC_HASH_H
#include <stdio.h>
#include <stdlib.h>
#define M 13
#define P 13
#define NULLKEY -1
#define DELETEKEY -2
typedef int KeyType;
typedef struct {
KeyType key;
int count;
} ElemType;
typedef struct {
ElemType data[M];
int length;
} HashTable;
void CreateHash(HashTable *&ht) {
ht = (HashTable *)malloc(sizeof(HashTable));
ht->length = 0;
for (int i = 0; i < M; i++) {
ht->data[i].key = NULLKEY;
ht->data[i].count = 0;
}
}
void HashInsert(HashTable *ht, KeyType k) {
int adr = k % P;
if (ht->data[adr].key == NULLKEY || ht->data[adr].key == DELETEKEY) {
ht->data[adr].key = k;
ht->data[adr].count = 1;
} else {
int cnt = 1;
adr = (adr + 1) % M;
while (ht->data[adr].key != NULLKEY && ht->data[adr].key != DELETEKEY) {
adr = (adr + 1) % M;
cnt += 1;
}
ht->data[adr].key = k;
ht->data[adr].count = cnt;
}
ht->length += 1;
}
int Search(HashTable *ht, KeyType k) {
int adr = k % P;
while (ht->data[adr].key != k && ht->data[adr].key != NULLKEY) {
adr = (adr + 1) % M;
}
if (ht->data[adr].key == k)
return adr;
else
return -1;
}
bool HashDelete(HashTable *ht, KeyType k) {
int adr = Search(ht, k);
if (adr == -1)
return false;
ht->data[adr].key = DELETEKEY;
ht->length -= 1;
return true;
}
void Display(HashTable *ht) {
float total = 0;
printf("哈希地址:\t");
for (int i = 0; i < M; i++)
printf("%4d", i);
printf("\n");
printf("关键字:\t");
for (int i = 0; i < M; i++) {
if (ht->data[i].key == NULLKEY || ht->data[i].key == DELETEKEY)
printf(" ");
else
printf("%4d", ht->data[i].key);
}
printf("\n");
printf("探测次数:\t");
for (int i = 0; i < M; i++) {
if (ht->data[i].key == NULLKEY || ht->data[i].key == DELETEKEY)
printf(" ");
else {
printf("%4d", ht->data[i].count);
total += ht->data[i].count;
}
}
printf("\n");
printf("平均查找长度(ASL(%d))为:%f\n", ht->length, total / ht->length);
}
void DestroyHash(HashTable *&ht) {
free(ht);
}
#endif
```
### 【Hash.cpp】
```cpp
#include "Hash.h"
int main(void) {
int n;
KeyType K[] = {16, 74, 60, 43, 54, 90, 46, 31, 29, 88, 77};
KeyType k = 29;
HashTable *ht;
n = sizeof(K) / sizeof(KeyType);
printf("实验五:查找,问题四:哈希查找,完成人:XXX\n");
printf("(1)创建哈希表\n");
CreateHash(ht);
for (int i = 0; i < n; i++) {
HashInsert(ht, K[i]);
}
printf("(2)输出哈希表\n");
Display(ht);
printf("(3)查找关键字为%d的记录位置:", k);
int i = Search(ht, k);
if (i != -1)
printf("\tht[%d]=%d\n", i, k);
else
printf("\t未找到%d\n", k);
int k2 = 77;
printf("(4)删除关键字为%d\n", k2);
HashDelete(ht, k2);
Display(ht);
printf("(5)添加关键字为%d\n", k2);
HashInsert(ht, k2);
Display(ht);
DestroyHash(ht);
return 0;
}
```
以上是《数据结构实验指导书 实验五 查找.pdf》中的所有代码,已完整无误地复制。