第1关:实现折半查找
#include <stdio.h>
#include <stdlib.h>
#include "BSlist.h"
BSeqList* BSL_Create(int size)
{
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)
{
free(blist->pkey);
free(blist);
}
int BSL_FindKey(BSeqList* blist, int key)
{
int low=0,high=blist->len-1,mid;
while(low<=high)
{
mid=(low+high)/2;
if(blist->pkey[mid]==key)
return mid;
if(blist->pkey[mid]<key)
{
low=mid+1;
}
if(blist->pkey[mid]>key)
{
high=mid-1;
}
}
return -1;
}
int BSL_InsKey(BSeqList* blist, int key)
{
if (blist->len>=blist->max) return -1;
int k, r, m;
k=0; r=blist->len-1;
while (k<=r) {
m=(k+r)>>1;
if (key == blist->pkey[m]) return -2;
if (key<blist->pkey[m]) r=m-1;
else k=m+1;
}
for (r=blist->len; r>k; r--)
blist->pkey[r]=blist->pkey[r-1];
blist->pkey[k]=key;
blist->len++;
return k;
}
int BSL_DelKey(BSeqList* blist, int key)
{
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");
}
第二关:实现散列查找
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "indLnkHash.h"
LHTable* ILH_Create(int 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)
{
int d=x%pt->n;
if(pt->pn[d].key==0)
{
pt->pn[d].key=x;
return true;
}
else if(pt->pn[d].key==x)
{
return false;
}
HNode* prev=&(pt->pn[d]);
HNode* curr=pt->pn[d].next;
while (curr && curr->key!=x)
{
prev=curr;
curr=curr->next;
}
if (curr)
{
return false;
}
HNode* pnode=(HNode*)malloc(sizeof(HNode));
pnode->key=x;
pnode->next=NULL;
prev->next=pnode;
return true;
}
bool ILH_FindKey(LHTable* pt, int x)
{
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)
{
int d=x%pt->n;
if (pt->pn[d].key==0) {
return false;
}
else if (pt->pn[d].key==x)
{
if(pt->pn[d].key==0)
{
pt->pn[d].key=0;
}
else
{
HNode* first=pt->pn[d].next;
pt->pn[d].key=first->key;
pt->pn[d].next=first->next;
free(first);
}
return true;
}
HNode* prev=&(pt->pn[d]);
HNode* curr=pt->pn[d].next;
while (curr && curr->key!=x)
{
prev=curr;
curr=curr->next;
}
if (curr==NULL)
{
return false;
}
prev->next=curr->next;
free(curr);
return true;
}
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");
}
}