開放定址散列表

?

? ? ? ?再散列之后散列函數要重新計算。

// kaifangliaobiao.cpp : 定義控制臺應用程序的入口點。
//使用平方探測解決沖突問題時,散列表至少空一半時,總能插入一個新的元素#include "stdafx.h"
#include<iostream>
using namespace std;#ifndef HashQuad
typedef unsigned int Index;
typedef Index Position;struct HashTbl;
typedef struct HashTbl *HashTable;HashTable InitializeTable(int TableSize);
void DestroyTable(HashTable H);
Position Find(int key, HashTable H);
void Insert(int key, HashTable H);
int Retrieve(Position P, HashTable H);
HashTable Rehash(HashTable H);
#endif // !HashQuad
#define MinTableSize 10enum KindOfEntry{Legitimate,Empty,Delete};struct HashEntry
{int key;enum KindOfEntry Info;
};typedef struct HashEntry Cell;struct HashTbl
{int TableSize;Cell *TheCell;
};int Hash(int key, int tableSize)
{return key%tableSize;
}int NextPrime(int n)
{if (n % 2 == 0) n++;               //1.排除掉偶數for (;; n += 2){bool isPrime = 1;              //2.標志位for (int i = 3; i*i <= n; i += 2)if (n%i == 0) {isPrime = 0;break;}if (isPrime)return n;}
}HashTable InitializeTable(int TableSize)   //初始化函數
{HashTable H;int i;if (TableSize < MinTableSize){cout << "Table is too small";return NULL;}H = (HashTable)malloc(sizeof(HashTbl));                //1.初始化散列表地址if (H == NULL)cout << "out of space";H->TableSize = NextPrime(TableSize);                 //2.用素數初始化散列表大小H->TheCell = (Cell *)malloc(sizeof(Cell)*H->TableSize);  //3.申請一個表頭if (H->TheCell == NULL)cout << "out of space";for (i = 0; i < H->TableSize; i++)H->TheCell[i].Info = Empty;                       //4.為每一個表項賦狀態空return H;
}Position Find(int key, HashTable H)                 //用平方探測散列法查找
{Position CurrentPos;                            //1.要返回的地址int CollisionNum;                               //2.偏移的位置量CollisionNum = 0;CurrentPos = Hash(key, H->TableSize);while (H->TheCell[CurrentPos].Info!=Empty&&H->TheCell[CurrentPos].key!=key)  //3.檢測表項狀態{CurrentPos += 2 * ++CollisionNum - 1;                                //4.偏移if (CurrentPos >= H->TableSize)                                     //5.滿則折返CurrentPos -= H->TableSize;}return CurrentPos;
}void Insert(int key, HashTable H)
{Position Pos;Pos = Find(key, H);if (H->TheCell[Pos].Info != Legitimate){H->TheCell[Pos].Info = Legitimate;H->TheCell->key = key;}
}HashTable Rehash(HashTable H)            //再散列
{int i, oldSize;Cell *OldCells;OldCells = H->TheCell;                  //1.記錄舊散列表的信息oldSize = H->TableSize;H = InitializeTable(2 * oldSize);       //2.創建兩倍大小的新散列表for (i = 0; i < oldSize; i++) {         //3.循環復制信息if (OldCells[i].Info == Legitimate)Insert(OldCells[i].key, H);}free(OldCells);return H;
}int main()
{return 0;
}

  

?

轉載于:https://www.cnblogs.com/linear/p/6636876.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541685.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541685.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541685.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

合并兩個鏈表數據結構c語言,合并兩個鏈表.

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓#include #define N1 10#define N2 10struct list{int date ;struct list *next;};main(){struct list *p1,*p2,*p3,*p4,*head,*head1,*head2,*p;int n0;head1head2NULL;p1p2(struct list *)malloc(sizeof(struct list));p1->da…

c ++查找字符串_C ++結構| 查找輸出程序| 套裝2

c 查找字符串Program 1: 程序1&#xff1a; #include <iostream>using namespace std;int main(){typedef struct{int A;char* STR;} S;S ob { 10, "india" };S* ptr;ptr &ob;cout << ptr->A << " " << ptr->STR[2];…

連接fiddler后手機無法顯示無網絡

升級了fiddler到4.6版本&#xff0c;手機設置代理后提示無網絡&#xff0c;試試以下解決方法&#xff1a; 1.fiddler升級后對應的.net framework也要升級&#xff0c;安裝最新的.net framework 4.6&#xff0c;升級安裝后&#xff0c;可以正確抓包啦 2.如果上述方法無效&#x…

android 人臉解鎖 鎖屏動畫,人臉保護鎖(人臉識別鎖屏)

這是一款十分炫酷的鎖屏工具&#xff0c;還記得電影中的特工所用的人臉識別鎖嗎&#xff1f;這款應用也能讓你過過癮&#xff01;人臉識別鎖屏安卓版是一款用人臉做密碼來打開手機屏保鎖的一個APP。不僅可以作屏保鎖&#xff0c;也可以單獨保護某些重要程序不被偷窺,例如查看短…

dbms_排名前50位的DBMS面試問答

dbms1) What are the drawbacks of the file system which is overcome on the database management system? 1)在數據庫管理系統上克服的文件系統有哪些缺點&#xff1f; Ans: Data redundancy & isolation, difficulty in accessing data, data isolation, and integri…

linux時間

CST代表中國標準時間rtc實時時鐘linux主要有兩種時間硬件時鐘 clock系統時鐘 date修改時間 date 03300924必須是兩位或者 date -s 2017:03:30將系統時間同步到硬件時間 hwclock -w將硬件時間同步到系統時間 hwclock -s轉載于:https://blog.51cto.com/12372297/1911608

查找Python中給定字符串的所有排列

Python itertools Module Python itertools模塊 "itertools" are an inbuilt module in Python which is a collection of tools for handling iterators. It is the most useful module of Python. Here, a string is provided by the user and we have to print a…

android 圖片疊加xml,Android實現圖片疊加效果的兩種方法

本文實例講述了Android實現圖片疊加效果的兩種方法。&#xff0c;具體如下&#xff1a;效果圖&#xff1a;第一種&#xff1a;第二種&#xff1a;第一種是通過canvas畫出來的效果:public void first(View v) {// 防止出現Immutable bitmap passed to Canvas constructor錯誤Bit…

Win10系列:VC++ 定時器

計時器機制俗稱"心跳"&#xff0c;表示以特定的頻率持續觸發特定事件和執行特定程序的機制。在開發Windows應用商店應用的過程中&#xff0c;可以使用定義在Windows::UI::Xaml命名空間中的DispatcherTimer類來創建計時器。DispatcherTimer類包含了如下的成員&#xf…

dbms系統 rdbms_DBMS與傳統文件系統之間的區別

dbms系統 rdbmsIntroduction 介紹 DBMS and Traditional file system have some advantages, disadvantages, applications, functions, features, components and uses. So, in this article, we will discuss these differences, advantages, disadvantages and many other …

android 百度地圖api密鑰,Android百度地圖開發獲取秘鑰之SHA1

最近在做一個關于百度地圖的開發。不過在正式開發之前還必須要在百度地圖API官網里先申請秘鑰&#xff0c;而在申請秘鑰的過程中&#xff0c;就需要獲取一個所謂的SHA1值。如上所示&#xff0c;但是由于不是正式開發&#xff0c;所以以上的發布版和開發版的SHA1可以先填寫相同。…

單位矩陣的逆| 使用Python的線性代數

Prerequisites: 先決條件&#xff1a; Defining a Matrix 定義矩陣 Identity Matrix 身份矩陣 There are matrices whose inverse is the same as the matrices and one of those matrices is the identity matrix. 有些矩陣的逆與矩陣相同&#xff0c;并且這些矩陣之一是單位…

華為榮耀七能升級鴻蒙系統嗎,華為鴻蒙系統來了,你知道哪些華為手機榮耀手機可以升級嗎?...

從鴻蒙系統第一次開始登場&#xff0c;到現在慢慢有許多鴻蒙系統設備出現&#xff0c;手機市場的格局似乎又要升級變化了。科技樹兒了解到&#xff0c;在某數碼博主經過和相關人員的溝通核實之后&#xff0c;目前暫定的是搭載華為麒麟710芯片以上的機型&#xff0c;無論華為或榮…

day5-shutil模塊

一、簡述 我們在日常處理文件時&#xff0c;經常用到os模塊&#xff0c;但是有的時候你會發現&#xff0c;像拷貝、刪除、打包、壓縮等文件操作&#xff0c;在os模塊中沒有對應的函數去操作&#xff0c;下面我們就來講講高級的 文件、文件夾、壓縮包 處理模塊&#xff1a;shuti…

matlab中now函數_now()方法以及JavaScript中的示例

matlab中now函數JavaScript now()方法 (JavaScript now() method) now() method is a Date class method, it is used to current time in milliseconds, it returns the total number of milliseconds since 01st January 1970, 00:00:00 UTC. now()方法是Date類的一種方法&am…

android 集成x5內核時 本地沒有,騰訊瀏覽服務-接入文檔

三、SDK集成步驟1. 第一步下載 SDK jar 包放到工程的libs目錄下&#xff0c;將源碼和XML里的系統包和類替換為SDK里的包和類&#xff0c;具體對應如下&#xff1a;系統內核SDK內核android.webkit.ConsoleMessagecom.tencent.smtt.export.external.interfaces.ConsoleMessageand…

java vector_Java Vector sureCapacity()方法與示例

java vector向量類別sureCapacity()方法 (Vector Class ensureCapacity() method) ensureCapacity() method is available in java.util package. sureCapacity()方法在java.util包中可用。 ensureCapacity() method is used to ensure the capacity of this Vector when requi…

Tcl與Design Compiler (十二)——綜合后處理

本文如果有錯&#xff0c;歡迎留言更正&#xff1b;此外&#xff0c;轉載請標明出處 http://www.cnblogs.com/IClearner/ &#xff0c;作者&#xff1a;IC_learner 概述 前面也講了一些綜合后的需要進行的一些工作&#xff0c;這里就集中講一下DC完成綜合了&#xff0c;產生了…

Java短類的compareTo()方法和示例

簡短的類compareTo()方法 (Short class compareTo() method) compareTo() method is available in java.lang package. compareTo()方法在java.lang包中可用。 compareTo() method is used to check equality or inequality for this Short object against the given Short obj…

四則運算網頁版

一.設計思想&#xff1a; 1&#xff09;寫出一個菜單界面&#xff0c;有兩個選項一個是分數&#xff0c;一個是整數。 2&#xff09;而這兩個標簽后面則是轉向其更詳細的菜單&#xff0c;題目數量&#xff0c;有無括號&#xff0c;運算的項數等等詳細功能&#xff0c;再點擊這兩…