Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

2차 조사로 해시 테이블을 구현하는 C++ 프로그램

<시간/>

해시 테이블은 키-값 쌍을 저장하는 데 사용되는 데이터 구조입니다. 해시 함수는 해시 테이블에서 요소를 삽입하거나 검색할 배열의 인덱스를 계산하는 데 사용됩니다. 2차 프로빙은 Open Addressed Hash 테이블의 충돌 해결 기술입니다. 원래 해시 인덱스를 가져 와서 열린 슬롯을 찾을 때까지 임의의 이차 다항식의 연속 값을 추가하여 작동합니다.

이차 조사로 해시 테이블을 구현하는 C++ 프로그램입니다.

알고리즘

키 값 검색:

시작 선언 함수 SearchKey(int k, HashTable *ht) int pos =HashFunc(k, ht->s) 충돌 초기화 =0 동안 (ht->t[pos].info !=Emp 및 ht->t) [pos].e !=k) pos =pos + 2 * ++collisions -1 if (pos>=ht->s) pos =pos - ht->s return posEnd.

삽입용:

Begin Declare function Insert(int k, HashTable *ht) int pos =SearchKey(k, ht) if (ht->t[pos].info !=Legi) ht->t[pos].info =Legi ht->t[pos].e =kEnd.

디스플레이용:

begin Declare function display(HashTable *ht) for (int i =0; i s; i++) int value =ht->t[i].e if (!value) print"Position:" 현재 위치 인쇄 Print" 요소:Null" else print"Position:" 현재 위치 인쇄 Print" 요소:" 요소 인쇄.End.

재해시 기능의 경우:

Begin Declare function Rehash(HashTable *ht) int s =ht->s HashTableEntry *t=ht->t ht=initialTable(2 * s) for (int i =0; i  

예시 코드

#include #include #define T_S 10 네임스페이스 사용 std;enum EntryType { Legi, Emp, Del}; 구조체 HashTableEntry { 정수 e; 열거형 항목 유형 정보; }; 구조체 해시 테이블 { int s; 해시 테이블 항목 *t; }; bool isPrime (int n) { if (n ==2 || n ==3) true를 반환합니다. if (n ==1 || n % 2 ==0) false를 반환합니다. for (int i =3; i * i <=n; i +=2) if (n % i ==0)은 false를 반환합니다. return true;}int nextPrime(int n) { if (n <=0) n ==3; if (n % 2 ==0) n++; (; !isPrime( n ); n +=2); return n;}int HashFunc(int k, int s) { return k % s;}HashTable *initiateTable(int s) { HashTable *ht; if (s s =nextPrime(들); ht->t =새로운 HashTableEntry [ht->s]; if (ht->t ==NULL) { cout<<"테이블 크기가 너무 작습니다."<s; i++) { ht->t[i].info =Emp; ht->t[i].e =NULL; } return ht;}int SearchKey(int k, HashTable *ht) { int pos =HashFunc(k, ht->s); int 충돌 =0; while (ht->t[pos].info !=Emp &&ht->t[pos].e !=k) { pos =pos + 2 * ++충돌 -1; if (pos>=ht->s) pos =pos - ht->s; } return pos;}void Insert(int k, HashTable *ht) { int pos =SearchKey(k, ht); if (ht->t[pos].info !=Legi) { ht->t[pos].info =Legi; ht->t[pos].e =k; }}HashTable *Rehash(HashTable *ht) { int s =ht->s; HashTableEntry *t=ht->t; ht=시작 테이블(2 * s); for (int i =0; i s; i++) { int value =ht->t[i].e; if(!값) cout<<"위치:"<>c; switch(c) { 경우 1:cout<<"해시 테이블의 크기를 입력하십시오:"; 신>>s; ht =시작 테이블(들); cout<<"해시 테이블의 크기:"< ht->s) { cout<<"테이블이 꽉 차면 테이블을 다시 해시합니다."<>v; 삽입(v, ht); 나는 ++; 부서지다; 사례 3:디스플레이(ht); 부서지다; 사례 4:ht =Rehash(ht); 부서지다; 사례 5:exit(1); 기본값:cout<<"\n올바른 옵션을 입력하세요\n"; } } 반환 0;}

출력

1.테이블의 크기를 초기화2.테이블에 요소를 삽입3.디스플레이 해시 테이블4.Rehash The Table5.ExitEnter your choice:1Enter size of the Hash Table:4Table Size is too small Hash Table의 Size:51.Initialize size of table2.Insert 요소를 table3.Display Hash Table4.Rehash The Table5.ExitEnter your choice:1Enter size of the Hash Table:10Size of the Hash Table:111.Initialize size of the table2.Insert element into the table3.Display Hash Table4 .Rehash The Table5.ExitEnter 선택:2삽입할 요소 입력:11.테이블의 크기 초기화2.테이블에 요소 삽입3.Display Hash Table4.Rehash The Table5.ExitEnter 선택:2삽입할 요소 입력:21.Initialize table2.Insert 요소의 table3.Display Hash Table4.Rehash The Table5.ExitEnter 당신의 선택:2삽입할 요소:31.Initialize size of the table2.Insert element into the table3.Display Hash Table4.Rehash The Table5 .Exit선택 사항을 입력하세요:2입력할 요소를 입력하세요 erted:41.Initialize size of the table2.Insert element into the table3.Display Hash Table4.Rehash The Table5.ExitEnter your choice:2Enter element to insert:51.Initialize size of the table2.Insert element into the table3.Display Hash Table4.Rehash Table5.ExitEnter 선택 항목:2삽입할 요소를 입력하세요. table2.Insert 요소의 크기를 table3.Display Hash Table4.Rehash The Table5.ExitEnter 선택:2삽입할 요소를 입력하십시오. 81.테이블의 크기를 초기화합니다.2.요소를 table3에 삽입합니다. Table5.Exit선택 사항을 입력하십시오:2삽입할 요소를 입력하십시오. table2.Insert 요소에 table3.Display Hash Table4.Rehash The Table5.ExitEnter 당신의 선택:2삽입할 요소를 입력:111.테이블의 크기를 초기화2.테이블에 요소를 삽입3.Display Hash Table4.Rehash The Table5.ExitEnter 당신의 선택:2Table is Full, table1.Initialize size of the table2.Insert element into the table3.Display Hash Table4.Rehash The Table5.ExitEnter your choice:3Position:1 Element:11Position:2 Element:1Position:3 Element:2Position:4 Element:3Position:5 요소:4Position:6 요소:5Position:7 요소:6Position:8 요소:7Position:9 요소:8Position:10 요소:9Position:11 요소:101. Table2.Insert 요소의 크기를 table3에 초기화합니다. .Rehash The Table5.ExitEnter your choice:41.Initialize size of the table2.Insert element into the table3.Display Hash Table4.Rehash The Table5.ExitEnter your choice:3Position:1 Element:NullPosition:2 Element:1Position:3 Element:2위치:4 요소:3위치:5 요소:4위치 :6 요소:5위치:7 요소:6위치:8 요소:7위치:9 요소:8위치:10 요소:9위치:11 요소:10위치:12 요소:11위치:13 요소:NullPosition:1 :16 요소:NullPosition:17 요소:NullPosition:18 요소:NullPosition:19 요소:NullPosition:20 요소:NullPosition:21 요소:NullPosition:22 요소:NullPosition:23 요소:Null1.Init의 요소 크기 the table3.Display Hash Table4.Rehash The Table5.ExitEnter 선택:2삽입할 요소:201.테이블의 크기 초기화2.Insert element into the table3.Display Hash Table4.Rehash The Table5.ExitEnter 선택:5