实现折半查找
相关知识
折半查找通常是针对顺序存储的线性表,线性表的结点按关键码从小到大排序,后面称之为折半查找的顺序表。为了简化讨论,假设折半查找的顺序表中每个结点只含一个关键码,关键码为整数。图1给出了一个存储了4个关键码的折半查找的顺序表的存储结构图。
下面描述线性表的顺序存储的一种实现方案。该实现方案的示意图为:
指针pkey是存储关键码的连续空间的起始地址,顺序表中当前的关键码的个数由len给出,该顺序表中最多可存储max个关键码。
#include <stdio.h>
#include <stdlib.h>
#include "bsList.h"
BSeqList* BSL_Create(int size)
//创建一个顺序表
//与BSL_Free()配对
{
BSeqList* blist=(BSeqList*)malloc(sizeof(BSeqList));
blist->pkey = (int*)malloc(sizeof(int)*size);
blist->max=size;
blist->len=0;
return blist;
}
void BSL_Free(BSeqList* blist)
//释放/删除顺序表
//与BSL_Create()配对
{
free(blist->pkey);
free(blist);
}
int BSL_FindKey(BSeqList* blist, int key)
//在排序的顺序表中查找关键码值为key的结点,返回结点的编号
//返回值大于等于时表示找到值为key的结点的编号,-1表示没有找到
{
// 请在此添加代码,补全函数BSL_FindKey
/********** Begin *********/
int k = 0, r = blist->len - 1;
while (k <= r) {
int m = (k + r) / 2; // 计算中间位置
if (key == blist->pkey[m]) {
return m; // 找到关键码,返回位置
} else if (key < blist->pkey[m]) {
r = m - 1; // 关键码在左半部分
} else {
k = m + 1; // 关键码在右半部分
}
}
return -1; // 未找到关键码,返回-1
/********** End **********/
}
int BSL_InsKey(BSeqList* blist, int key)
//在排序的顺序表中插入一个值为key的结点
//返回值大于等于表示插入的位置, -1表示表满(无法插入)
{
if (blist->len>=blist->max) return -1;
int k, r, m;
k=0; r=blist->len-1;
//寻找插入位置
while (k<=r) {
m=(k+r)>>1; //m=(k+r)/2
if (key == blist->pkey[m]) return -2;////若不允许插入已存在的值,则需要此行
if (key<blist->pkey[m]) r=m-1;
else k=m+1;
}
//插入位置为k, 腾出k号位置
for (r=blist->len; r>k; r--)
blist->pkey[r]=blist->pkey[r-1];
//key放入k号位置
blist->pkey[k]=key;
blist->len++;
return k;
}
int BSL_DelKey(BSeqList* blist, int key)
//在排序的顺序表中删除值为key的结点,
//存在值为x的结点则返回结点编号, 未找到返回-1
{
int k=BSL_FindKey(blist, key);
if (k<0) return -1;
int i=k;
while(i < blist->len-1) {
blist->pkey[i] = blist->pkey[i+1];
i++;
}
blist->len --;
return k;
}
void BSL_Print(BSeqList* blist)
//打印整个顺序表
{
if (blist->len==0) {
printf("The list is empty.\n");
return;
}
printf("The list contains: ");
for (int i=0; i<blist->len; i++) {
printf("%d ", blist->pkey[i]);
}
printf("\n");
}
实现散列查找
定义如下操作:
LHTable* ILH_Create(int n);
void ILH_Free(LHTable* pt);
bool ILH_InsKey(LHTable* pt, int x);
bool ILH_FindKey(LHTable* pt, int x);
bool ILH_DelKey(LHTable* pt, int x);
void ILH_Print(LHTable *pt);
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "indLnkHash.h"
LHTable* ILH_Create(int n)
//创建散列表, n为表长度,最佳取值:n取小于等于数据个数的最大质数
{
HNode* pn=(HNode*)malloc(sizeof(HNode)*n);
for (int i=0; i<n; i++) {
pn[i].key=0;
pn[i].next=NULL;
}
LHTable* pt=(LHTable*)malloc(sizeof(LHTable));
pt-> pn=pn;
pt->n=n;
return pt;
}
void ILH_Free(LHTable* pt)
//释放散列表
{
if (pt==NULL) return;
for (int i=0; i<pt->n; i++) {
HNode* curr=pt->pn[i].next;
while (curr) {
HNode* next=curr->next;
free(curr);
curr=next;
}
}
free(pt->pn);
free(pt);
}
bool ILH_InsKey(LHTable* pt, int x)
//插入关键码x
//返回true,表示插入成功
//返回false,表示插入失败(关键码已经存在)
{
/*请在BEGIN和END之间实现你的代码*/
/*****BEGIN*****/
int d = x % pt->n;
HNode* curr = &pt->pn[d];
// 检查是否已经存在
if (curr->key == x) {
return false; // 关键码已存在
}
if (curr->key == 0) {
// 如果当前位置为空,直接插入
curr->key = x;
return true;
}
// 遍历链表查找关键码
while (curr->next != NULL) {
if (curr->next->key == x) {
return false; // 关键码已存在
}
curr = curr->next;
}
// 插入关键码到链表末尾
HNode* newNode = (HNode*)malloc(sizeof(HNode));
if (newNode == NULL) {
return false; // 内存分配失败
}
newNode->key = x;
newNode->next = NULL;
curr->next = newNode;
return true;
/******END******/
/*请不要修改[BEGIN,END]区域外的代码*/
}
bool ILH_FindKey(LHTable* pt, int x)
//查找关键码x
//返回true表示找到
//返回false表示没找到
{
int d=x%pt->n;
if (pt->pn[d].key==0) {
return false;
}
else if (pt->pn[d].key==x)
return true;
HNode* curr=pt->pn[d].next;
while (curr && curr->key!=x) curr=curr->next;
if (curr) return true;
else return false;
}
bool ILH_DelKey(LHTable* pt, int x)
//删除关键码
//返回true表示该关键码存在,且成功删除
//返回false表示该关键码不存在
{
/*请在BEGIN和END之间实现你的代码*/
/*****BEGIN*****/
int d = x % pt->n;
HNode* curr = &pt->pn[d];
HNode* prev = NULL;
// 查找关键码
while (curr != NULL && curr->key != x) {
prev = curr;
curr = curr->next;
}
// 如果未找到关键码
if (curr == NULL) {
return false;
}
// 如果关键码在表头
if (prev == NULL) {
if (curr->next != NULL) {
// 如果链表不为空,将链表第一个元素移动到表头
HNode* temp = curr->next;
pt->pn[d].key = temp->key;
pt->pn[d].next = temp->next;
free(temp);
} else {
// 如果链表为空,直接清空表头
pt->pn[d].key = 0;
}
} else {
// 如果关键码不在表头,删除节点
prev->next = curr->next;
free(curr);
}
return true;
/******END******/
/*请不要修改[BEGIN,END]区域外的代码*/
}
void ILH_Print(LHTable *pt)
{
for (int i=0; i<pt->n; i++) {
printf("%5d:", i);
if (pt->pn[i].key) {
printf("%d", pt->pn[i].key);
HNode* curr=pt->pn[i].next;
while (curr) {
printf("->%d", curr->key);
curr=curr->next;
}
printf("\n");
}
else
printf("-\n");
}
}