c语言实现散列查找,部分函数已给出,需要在原代码上进行编写

除了ILH_InsKey(LHTable* pt, int x)、ILH_DelKey(LHTable* pt, int x)两个函数都是给好的,为什么这样写pt->pn[val].next永远为NULL啊

题目:
本关要求通过补全函数ILH_InsKey和ILH_DelKey来分别实现插入和删除操作。

img

img


#include 
#include 
#include 
#include "indLnkHash.h"
#include 

using namespace std;

LHTable* ILH_Create(int n)
//创建散列表, n为表长度,最佳取值:n取小于等于数据个数的最大质数
{
    HNode* pn=(HNode*)malloc(sizeof(HNode)*n);
    for (int i=0; i0;
        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; in; 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*****/
    if(ILH_FindKey(pt,x))
        return false;

    int val=x%(pt->n);

    if(pt->pn[val].key==0)
    {
        pt->pn[val].key=x;
        pt->pn[val].next=NULL;

        return true;
    }
    else
    {
        cout<<"============"<"-------------"<pn[val].next;
        HNode* l=&p;

        // HNode* l=pt->pn[val].next;
        cout<<"2222"<while(l->next!=NULL)
            l=l->next;

        cout<<"11111"<new HNode;
        pp->key=x;
        pp->next=NULL;

        l->next=pp;
        
        //cout<<"l->next="<<(l->next==NULL)<
        cout<<"pt->pn[val].next="<<(pt->pn[val].next==NULL)<return true;
    }

    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*****/

    if(!ILH_FindKey(pt,x))
        return false;

    int val=x%(pt->n);

    HNode l=pt->pn[val];

    if(l.key==x)
    {
        if(l.next==NULL)
        {
            l.key=0;
            return true;
        }
        else
        {
            HNode* p=&l;

            while(p!=NULL)
            {
                if(p->next->key==x)
                {
                    HNode* pp=p->next;

                    p->next=pp->next;
                    delete pp;

                    return true;
                }

                p=p->next;
            }
        }
    }
    
    /******END******/
    /*请不要修改[BEGIN,END]区域外的代码*/
}

void ILH_Print(LHTable *pt)
{
    for (int i=0; in; 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");
    }
}