头歌——数据结构与算法 - 查找

第1关:实现折半查找 

/*************************************************************
    date: April 2009
    copyright: Zhu En
    DO NOT distribute this code.
**************************************************************/
//折半查找的顺序表 实现文件
//每个结点的数据是关键码
//
#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的结点,返回结点的编号
//返回值大于等于0时表示找到值为key的结点的编号,-1表示没有找到
{
    /*请在BEGIN和END之间实现你的代码*/
    /*****BEGIN*****/

    int l,h,m;//k是low,r是high,m是mid
    l=0;
    h=blist->len-1;
    while(l<=h)
    {
        m=(l+h)/2;
        if(key==blist->pkey[m])
        {
            return m;
        }
        else
        {
            if(key<blist->pkey[m])
            {
                h=m-1;
            }
            else
            {
                l=m+1;
            }
        }
    }
    return -1;
    /******END******/
    /*请不要修改[BEGIN,END]区域外的代码*/
}

int BSL_InsKey(BSeqList* blist, int key)
//在排序的顺序表中插入一个值为key的结点
//返回值大于等于0时表示插入的位置, -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");
}

 

第2关:实现散列查找 

#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;
    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;
    /******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*****/
    
    /******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");
    }
}

  • 3
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值