zoukankan      html  css  js  c++  java
  • 哈希,链接法解决冲突

    #include<iostream>
    using namespace std;

    struct ListNode;
    typedef struct ListNode *Position;
    struct Hash_table;
    typedef Hash_table *Hashtab;
    typedef Position List;

    #define Min_table_size 10

    struct ListNode
    {
     int Emelent;
     Position Next;
    };

    struct Hash_table
    {
     int Table_size;
     List *Thelist;//指向链表的链表
    };

    //////////////相关函数声明//////////////////////
    Hashtab Inittable(int Table_size);     //初始化一个散列
    Position Find(int x, Hashtab H);      //查找元素x,返回对应的位置
    int Hash(int x);  //散列函数
    void Insert(int Key, Hashtab H);   //在散列中插入元素Key
    void Delete(int Key, Hashtab H);   //在散列中删除元素Key

    ///////////////相关函数定义////////////////////
    Hashtab Inittable(int table_size)
    {
     Hashtab H;
     if (table_size < Min_table_size)
     {
      cout << "Table size is too small" << endl;
      return NULL;
     }

     H = (Hashtab)malloc(sizeof(Hash_table));
     if (H == NULL)  cout << "out of space" << endl;
     H->Table_size = table_size;
     H->Thelist = (List*)malloc(sizeof(Position) * H->Table_size);//不知道Position里头有多少个元素也可以分配内存吗
     if (H->Thelist == NULL) cout << "out of space" << endl;
     for (int i = 0; i != H->Table_size; ++i)
     {
      H->Thelist[i] = (Position)malloc(sizeof(ListNode));
      if (H->Thelist[i] == NULL) cout << "out of space" << endl;
      else
      {
       H->Thelist[i]->Emelent = i;
       H->Thelist[i]->Next = NULL;
      }
     }
     return H;
    }

    int Hash(int x)   //对10取余数
    {
     return x % 10;
    }

    Position Find(int x, Hashtab H)
    {
     Position P;
     List L;

     L = H->Thelist[Hash(x)];  //指向含有那个元素的表头
     P = L->Next;
     while (P != NULL && P->Emelent != x)
      P = P->Next;
     return P;
    }

    void Insert(int Key, Hashtab H)
    {
     Position Pos, Newcell;
     List L;
     Pos = Find(Key, H);  //先找找看,有没有Key,有就算了
     if (Pos == NULL)
     {
      Newcell = (Position)malloc(sizeof(ListNode));
      if (Newcell == NULL)  cout << "out of space" << endl;
      else    //插入到槽后面的第一个位置
      {
       L = H->Thelist[Hash(Key)];
       Newcell->Next = L->Next;
       Newcell->Emelent = Key;
       L->Next = Newcell;
      }
     }
    }

    void Delete(int Key, Hashtab H)
    {
     Position p, Tmpcell;
     List L;
     p = Find(Key, H);
     if (p == NULL)
      cout << "not find the " << Key << endl;
     else
     {
      L = H->Thelist[Hash(Key)];
      p = L;
      while (p->Next != NULL && p->Next->Emelent != Key)   //寻找Key的前驱节点
      {
       p = p->Next;
      }
      //
      Tmpcell = p->Next;
      p->Next = Tmpcell->Next;
      free(Tmpcell);
     }

    }
    int main()
    {
     Hashtab H = Inittable(11);
     cout << H->Thelist[9]->Emelent << endl;
     Insert(1, H);
     Insert(4, H);
     Insert(9, H);
     Insert(16, H);
     Insert(25, H);
     Insert(19, H);
     Insert(29, H);
     Delete(19, H);
     
     cout << H->Thelist[9]->Next->Next->Emelent << endl;
     return 0;
    }

  • 相关阅读:
    dotnet 控制台读写 Sqlite 提示 no such table 找不到文件
    dotnet 控制台读写 Sqlite 提示 no such table 找不到文件
    dotnet 控制台 Hangfire 后台定时任务
    dotnet 控制台 Hangfire 后台定时任务
    dotnet 获取指定进程的输入命令行
    dotnet 获取指定进程的输入命令行
    PHP sqrt() 函数
    PHP sinh() 函数
    PHP sin() 函数
    PHP round() 函数
  • 原文地址:https://www.cnblogs.com/Rakint/p/9795428.html
Copyright © 2011-2022 走看看