zoukankan      html  css  js  c++  java
  • 数据结构与算法分析-分离链接散列表的实现

    #include<stdio.h>
    #include<math.h>
    
    typedef char* ElementType;
    typedef unsigned int Index;
    #define MinTableSize 15
    
    struct ListNode;
    typedef struct ListNode *Position;
    struct HashTbl;
    typedef struct HashTbl *HashTable;
    
    HashTable InitializeTable(int TableSize);
    void DestroyTable(HashTable H);
    Position Find(ElementType key,HashTable H);
    void Insert(ElementType key,HashTable H);
    ElementType Retrieve(Position P);
    void Delete(ElementType key,HashTable H);
    
    struct ListNode
    {
        ElementType Element;
        Position next;
    };
    
    typedef Position List;
    
    struct HashTbl
    {
        int TableSize;
        List *TheLists;
    };
    
    Index Hash(const char *key,int TableSize)
    {
        unsigned int HashVal=0;
        while(*key!='')
        {
            HashVal+=*key++;
        }
        return HashVal%TableSize;
    }
    
    int NextPrime(int TableSize)
    {
        int i,j=2;
        for(i=TableSize;i>0;i--)
        {
            while(j<sqrt(i))
            {
                if(i%j==0)
                    break;
                j++;
            }
            if(j==sqrt(i))
                break;
        }
        return i;
    }
    
    HashTable InitializeTable(int TableSize)
    {
        HashTable H;
        int i;
        if(TableSize<MinTableSize)
        {
            Error("Table size too small");
            return NULL;
        }
        H=malloc(sizeof(struct HashTbl));
        if(H==NULL)
        {
            FatalError("Out of space!!");
        }
        H->TableSize=NextPrime(TableSize);
        H->TheLists=malloc(sizeof(List)*H->TableSize);
        if(H->TheLists==NULL)
        {
            FatalError("Out of space!!");
        }
        for(i=0;i<H->TableSize;i++)
        {
            //分配头节点
            H->TheLists[i]=malloc(sizeof(struct ListNode));
            if(H->TheLists[i]==NULL)
                FatalError("Out of space!!");
            else
                H->TheLists[i]->next=NULL;
        }
        return H;
    }
    
    void DestroyTable(HashTable H)
    {
        int i;
        for(i=0;i<H->TableSize;i++)
        {
            free(H->TheLists[i]);
        }
        free(H->TheLists);
        free(H);
    }
    
    Position Find(ElementType key,HashTable H)
    {
        Position P;
        List L;
        L=H->TheLists[Hash(key,H->TableSize)];
        P=L->next;
        while(P!=NULL&&P->Element!=key)
            P=P->next;
        return P;
    }
    
    void Insert(ElementType key,HashTable H)
    {
        Position Pos,NewCell;
        List L;
        Pos=Find(key,H);
        if(Pos==NULL)
        {
            NewCell=malloc(sizeof(struct ListNode));
            if(NewCell==NULL)
            {
                FatalError("Out of space!!");
            }
            else
            {
                L=H->TheLists[Hash(key,H->TableSize)];
                NewCell->next=L->next;
                NewCell->Element=key;
                L->next=NewCell;
            }
        }
    }
    
    ElementType Retrieve(Position P)
    {
        if(P==NULL)
            return NULL;
        else
            return P->Element;
    }
    
    void Delete(ElementType key,HashTable H)
    {
        Position Pos,Pre;
        List L;
        int i;
        Pos=Find(key,H);
        if(Pos==NULL)
            return;
        else
        {
            L=H->TheLists[Hash(key,H->TableSize)];
            for(Pre=L;pre->next!=NUll;Pre=Pre->next)
            {
                if(Pre->next==Pos)
                {
                    free(Pos->Element);
                    free(Pos);
                    break;
                }
            }
        }
    }
  • 相关阅读:
    Shadow SSDT详解、WinDbg查看Shadow SSDT
    两种方法获取shadow ssdt
    r0遍历系统进程方法总结
    枚举PEB获取进程模块列表
    用户层获取TEB PEB结构地址 遍历进程模块.doc
    Process32First 返回FALSE的原因
    windows内核需要注意的
    hive中遇到的问题
    解读:计数器Counter
    hadoop System times on machines may be out of sync. Check system time and time zones.
  • 原文地址:https://www.cnblogs.com/wuchanming/p/3808597.html
Copyright © 2011-2022 走看看