Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zhhf96
C-Plus-Plus-TheAlgorithms
提交
d89b2df2
C
C-Plus-Plus-TheAlgorithms
项目概览
zhhf96
/
C-Plus-Plus-TheAlgorithms
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
C-Plus-Plus-TheAlgorithms
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
d89b2df2
编写于
7月 29, 2020
作者:
K
Krishna Vedala
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
replace using std::cin, etc
上级
61732da0
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
102 addition
and
112 deletion
+102
-112
hashing/linear_probing_hash_table.cpp
hashing/linear_probing_hash_table.cpp
+49
-54
hashing/quadratic_probing_hash_table.cpp
hashing/quadratic_probing_hash_table.cpp
+53
-58
未找到文件。
hashing/linear_probing_hash_table.cpp
浏览文件 @
d89b2df2
...
...
@@ -7,14 +7,8 @@
* @note The implementation can be optimized by using OOP style.
*/
#include <iostream>
#include <string>
#include <vector>
using
std
::
cin
;
using
std
::
cout
;
using
std
::
endl
;
using
std
::
string
;
namespace
{
// keep the code local to this file by assigning them to an unnamed
// namespace
...
...
@@ -65,25 +59,26 @@ int linearProbe(int key, bool searching) {
return
notPresent
;
}
if
(
searchingProber
(
entry
,
key
))
{
cout
<<
"Found key!"
<<
endl
;
std
::
cout
<<
"Found key!"
<<
std
::
endl
;
return
index
;
}
cout
<<
"Found tombstone or equal hash, checking next"
<<
endl
;
std
::
cout
<<
"Found tombstone or equal hash, checking next"
<<
std
::
endl
;
i
++
;
}
else
{
if
(
putProber
(
entry
,
key
))
{
if
(
!
rehashing
)
{
cout
<<
"Spot found!"
<<
endl
;
std
::
cout
<<
"Spot found!"
<<
std
::
endl
;
}
return
index
;
}
if
(
!
rehashing
)
{
cout
<<
"Spot taken, looking at next"
<<
endl
;
std
::
cout
<<
"Spot taken, looking at next"
<<
std
::
endl
;
}
i
++
;
}
if
(
i
==
totalSize
)
{
cout
<<
"Linear probe failed"
<<
endl
;
std
::
cout
<<
"Linear probe failed"
<<
std
::
endl
;
return
notPresent
;
}
}
while
(
entry
.
key
!=
notPresent
);
...
...
@@ -120,16 +115,16 @@ bool searchingProber(const Entry& entry, int key) {
void
display
()
{
for
(
int
i
=
0
;
i
<
totalSize
;
i
++
)
{
if
(
table
[
i
].
key
==
notPresent
)
{
cout
<<
" Empty "
;
std
::
cout
<<
" Empty "
;
}
else
if
(
table
[
i
].
key
==
tomb
)
{
cout
<<
" Tomb "
;
std
::
cout
<<
" Tomb "
;
}
else
{
cout
<<
" "
;
cout
<<
table
[
i
].
key
;
cout
<<
" "
;
std
::
cout
<<
" "
;
std
::
cout
<<
table
[
i
].
key
;
std
::
cout
<<
" "
;
}
}
cout
<<
endl
;
std
::
cout
<<
std
::
endl
;
}
/** Rehashes the table into a bigger table
...
...
@@ -152,7 +147,7 @@ void rehash() {
}
// delete[] oldTable;
rehashing
=
false
;
cout
<<
"Table was rehashed, new size is: "
<<
totalSize
<<
endl
;
std
::
cout
<<
"Table was rehashed, new size is: "
<<
totalSize
<<
std
::
endl
;
}
/** Adds entry using linear probing. Checks for load factor here
...
...
@@ -173,9 +168,9 @@ void add(int key) {
void
remove
(
int
key
)
{
int
index
=
linearProbe
(
key
,
true
);
if
(
index
==
notPresent
)
{
cout
<<
"key not found"
<<
endl
;
std
::
cout
<<
"key not found"
<<
std
::
endl
;
}
cout
<<
"Removal Successful, leaving tomb"
<<
endl
;
std
::
cout
<<
"Removal Successful, leaving tomb"
<<
std
::
endl
;
table
[
index
].
key
=
tomb
;
size
--
;
}
...
...
@@ -184,14 +179,14 @@ void remove(int key) {
* @param key key value to hash and add
*/
void
addInfo
(
int
key
)
{
cout
<<
"Initial table: "
;
std
::
cout
<<
"Initial table: "
;
display
();
cout
<<
endl
;
cout
<<
"hash of "
<<
key
<<
" is "
<<
hashFxn
(
key
)
<<
" % "
<<
totalSize
<<
" == "
<<
hashFxn
(
key
)
%
totalSize
;
cout
<<
endl
;
std
::
cout
<<
std
::
endl
;
std
::
cout
<<
"hash of "
<<
key
<<
" is "
<<
hashFxn
(
key
)
<<
" % "
<<
totalSize
<<
" == "
<<
hashFxn
(
key
)
%
totalSize
;
std
::
cout
<<
std
::
endl
;
add
(
key
);
cout
<<
"New table: "
;
std
::
cout
<<
"New table: "
;
display
();
}
...
...
@@ -199,14 +194,14 @@ void addInfo(int key) {
* @param key key value to hash and remove
*/
void
removalInfo
(
int
key
)
{
cout
<<
"Initial table: "
;
std
::
cout
<<
"Initial table: "
;
display
();
cout
<<
endl
;
cout
<<
"hash of "
<<
key
<<
" is "
<<
hashFxn
(
key
)
<<
" % "
<<
totalSize
<<
" == "
<<
hashFxn
(
key
)
%
totalSize
;
cout
<<
endl
;
std
::
cout
<<
std
::
endl
;
std
::
cout
<<
"hash of "
<<
key
<<
" is "
<<
hashFxn
(
key
)
<<
" % "
<<
totalSize
<<
" == "
<<
hashFxn
(
key
)
%
totalSize
;
std
::
cout
<<
std
::
endl
;
remove
(
key
);
cout
<<
"New table: "
;
std
::
cout
<<
"New table: "
;
display
();
}
}
// namespace
...
...
@@ -216,45 +211,45 @@ void removalInfo(int key) {
*/
int
main
()
{
int
cmd
=
0
,
hash
=
0
,
key
=
0
;
cout
<<
"Enter the initial size of Hash Table. = "
;
cin
>>
totalSize
;
std
::
cout
<<
"Enter the initial size of Hash Table. = "
;
std
::
cin
>>
totalSize
;
table
=
std
::
vector
<
Entry
>
(
totalSize
);
bool
loop
=
true
;
while
(
loop
)
{
system
(
"pause"
);
cout
<<
endl
;
cout
<<
"PLEASE CHOOSE -"
<<
endl
;
cout
<<
"1. Add key. (Numeric only)"
<<
endl
;
cout
<<
"2. Remove key."
<<
endl
;
cout
<<
"3. Find key."
<<
endl
;
cout
<<
"4. Generate Hash. (Numeric only)"
<<
endl
;
cout
<<
"5. Display Hash table."
<<
endl
;
cout
<<
"6. Exit."
<<
endl
;
cin
>>
cmd
;
std
::
cout
<<
std
::
endl
;
std
::
cout
<<
"PLEASE CHOOSE -"
<<
std
::
endl
;
std
::
cout
<<
"1. Add key. (Numeric only)"
<<
std
::
endl
;
std
::
cout
<<
"2. Remove key."
<<
std
::
endl
;
std
::
cout
<<
"3. Find key."
<<
std
::
endl
;
std
::
cout
<<
"4. Generate Hash. (Numeric only)"
<<
std
::
endl
;
std
::
cout
<<
"5. Display Hash table."
<<
std
::
endl
;
std
::
cout
<<
"6. Exit."
<<
std
::
endl
;
std
::
cin
>>
cmd
;
switch
(
cmd
)
{
case
1
:
cout
<<
"Enter key to add = "
;
cin
>>
key
;
std
::
cout
<<
"Enter key to add = "
;
std
::
cin
>>
key
;
addInfo
(
key
);
break
;
case
2
:
cout
<<
"Enter key to remove = "
;
cin
>>
key
;
std
::
cout
<<
"Enter key to remove = "
;
std
::
cin
>>
key
;
removalInfo
(
key
);
break
;
case
3
:
{
cout
<<
"Enter key to search = "
;
cin
>>
key
;
std
::
cout
<<
"Enter key to search = "
;
std
::
cin
>>
key
;
Entry
entry
=
table
[
linearProbe
(
key
,
true
)];
if
(
entry
.
key
==
notPresent
)
{
cout
<<
"Key not present"
;
std
::
cout
<<
"Key not present"
;
}
break
;
}
case
4
:
cout
<<
"Enter element to generate hash = "
;
cin
>>
key
;
cout
<<
"Hash of "
<<
key
<<
" is = "
<<
hashFxn
(
key
);
std
::
cout
<<
"Enter element to generate hash = "
;
std
::
cin
>>
key
;
std
::
cout
<<
"Hash of "
<<
key
<<
" is = "
<<
hashFxn
(
key
);
break
;
case
5
:
display
();
...
...
@@ -264,7 +259,7 @@ int main() {
break
;
// delete[] table;
}
cout
<<
endl
;
std
::
cout
<<
std
::
endl
;
}
return
0
;
}
hashing/quadratic_probing_hash_table.cpp
浏览文件 @
d89b2df2
...
...
@@ -8,14 +8,8 @@
*/
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using
std
::
cin
;
using
std
::
cout
;
using
std
::
endl
;
using
std
::
string
;
namespace
{
// keep the code local to this file by assigning them to an unnamed
// fwd declarations
...
...
@@ -59,29 +53,30 @@ int quadraticProbe(int key, bool searching) {
return
notPresent
;
}
if
(
searchingProber
(
entry
,
key
))
{
cout
<<
"Found key!"
<<
endl
;
std
::
cout
<<
"Found key!"
<<
std
::
endl
;
return
index
;
}
cout
<<
"Found tombstone or equal hash, checking next"
<<
endl
;
std
::
cout
<<
"Found tombstone or equal hash, checking next"
<<
std
::
endl
;
i
++
;
}
else
{
if
(
putProber
(
entry
,
key
))
{
if
(
!
rehashing
)
{
cout
<<
"Spot found!"
<<
endl
;
std
::
cout
<<
"Spot found!"
<<
std
::
endl
;
}
return
index
;
}
if
(
!
rehashing
)
{
cout
<<
"Spot taken, looking at next (next index = "
<<
(
hash
+
static_cast
<
size_t
>
(
std
::
round
(
std
::
pow
(
i
+
1
,
2
))))
%
totalSize
<<
endl
;
std
::
cout
<<
"Spot taken, looking at next (next index = "
<<
(
hash
+
static_cast
<
size_t
>
(
std
::
round
(
std
::
pow
(
i
+
1
,
2
))))
%
totalSize
<<
std
::
endl
;
}
i
++
;
}
if
(
i
==
totalSize
*
100
)
{
cout
<<
"Quadratic probe failed (infinite loop)"
<<
endl
;
std
::
cout
<<
"Quadratic probe failed (infinite loop)"
<<
std
::
endl
;
return
notPresent
;
}
}
while
(
entry
.
key
!=
notPresent
);
...
...
@@ -117,16 +112,16 @@ Entry find(int key) {
void
display
()
{
for
(
int
i
=
0
;
i
<
totalSize
;
i
++
)
{
if
(
table
[
i
].
key
==
notPresent
)
{
cout
<<
" Empty "
;
std
::
cout
<<
" Empty "
;
}
else
if
(
table
[
i
].
key
==
tomb
)
{
cout
<<
" Tomb "
;
std
::
cout
<<
" Tomb "
;
}
else
{
cout
<<
" "
;
cout
<<
table
[
i
].
key
;
cout
<<
" "
;
std
::
cout
<<
" "
;
std
::
cout
<<
table
[
i
].
key
;
std
::
cout
<<
" "
;
}
}
cout
<<
endl
;
std
::
cout
<<
std
::
endl
;
}
// Rehashes the table into a bigger table
...
...
@@ -146,7 +141,7 @@ void rehash() {
}
// delete[] oldTable;
rehashing
=
false
;
cout
<<
"Table was rehashed, new size is: "
<<
totalSize
<<
endl
;
std
::
cout
<<
"Table was rehashed, new size is: "
<<
totalSize
<<
std
::
endl
;
}
// Checks for load factor here
...
...
@@ -163,36 +158,36 @@ void add(int key) {
void
remove
(
int
key
)
{
int
index
=
quadraticProbe
(
key
,
true
);
if
(
index
==
notPresent
)
{
cout
<<
"key not found"
<<
endl
;
std
::
cout
<<
"key not found"
<<
std
::
endl
;
}
table
[
index
].
key
=
tomb
;
cout
<<
"Removal successful, leaving tombstone"
<<
endl
;
std
::
cout
<<
"Removal successful, leaving tombstone"
<<
std
::
endl
;
size
--
;
}
// Information about the adding process
void
addInfo
(
int
key
)
{
cout
<<
"Initial table: "
;
std
::
cout
<<
"Initial table: "
;
display
();
cout
<<
endl
;
cout
<<
"hash of "
<<
key
<<
" is "
<<
hashFxn
(
key
)
<<
" % "
<<
totalSize
<<
" == "
<<
hashFxn
(
key
)
%
totalSize
;
cout
<<
endl
;
std
::
cout
<<
std
::
endl
;
std
::
cout
<<
"hash of "
<<
key
<<
" is "
<<
hashFxn
(
key
)
<<
" % "
<<
totalSize
<<
" == "
<<
hashFxn
(
key
)
%
totalSize
;
std
::
cout
<<
std
::
endl
;
add
(
key
);
cout
<<
"New table: "
;
std
::
cout
<<
"New table: "
;
display
();
}
// Information about removal process
void
removalInfo
(
int
key
)
{
cout
<<
"Initial table: "
;
std
::
cout
<<
"Initial table: "
;
display
();
cout
<<
endl
;
cout
<<
"hash of "
<<
key
<<
" is "
<<
hashFxn
(
key
)
<<
" % "
<<
totalSize
<<
" == "
<<
hashFxn
(
key
)
%
totalSize
;
cout
<<
endl
;
std
::
cout
<<
std
::
endl
;
std
::
cout
<<
"hash of "
<<
key
<<
" is "
<<
hashFxn
(
key
)
<<
" % "
<<
totalSize
<<
" == "
<<
hashFxn
(
key
)
%
totalSize
;
std
::
cout
<<
std
::
endl
;
remove
(
key
);
cout
<<
"New table: "
;
std
::
cout
<<
"New table: "
;
display
();
}
}
// namespace
...
...
@@ -200,45 +195,45 @@ void removalInfo(int key) {
// I/O
int
main
()
{
int
cmd
=
0
,
hash
=
0
,
key
=
0
;
cout
<<
"Enter the initial size of Hash Table. = "
;
cin
>>
totalSize
;
std
::
cout
<<
"Enter the initial size of Hash Table. = "
;
std
::
cin
>>
totalSize
;
table
=
std
::
vector
<
Entry
>
(
totalSize
);
bool
loop
=
true
;
while
(
loop
)
{
system
(
"pause"
);
cout
<<
endl
;
cout
<<
"PLEASE CHOOSE -"
<<
endl
;
cout
<<
"1. Add key. (Numeric only)"
<<
endl
;
cout
<<
"2. Remove key."
<<
endl
;
cout
<<
"3. Find key."
<<
endl
;
cout
<<
"4. Generate Hash. (Numeric only)"
<<
endl
;
cout
<<
"5. Display Hash table."
<<
endl
;
cout
<<
"6. Exit."
<<
endl
;
cin
>>
cmd
;
std
::
cout
<<
std
::
endl
;
std
::
cout
<<
"PLEASE CHOOSE -"
<<
std
::
endl
;
std
::
cout
<<
"1. Add key. (Numeric only)"
<<
std
::
endl
;
std
::
cout
<<
"2. Remove key."
<<
std
::
endl
;
std
::
cout
<<
"3. Find key."
<<
std
::
endl
;
std
::
cout
<<
"4. Generate Hash. (Numeric only)"
<<
std
::
endl
;
std
::
cout
<<
"5. Display Hash table."
<<
std
::
endl
;
std
::
cout
<<
"6. Exit."
<<
std
::
endl
;
std
::
cin
>>
cmd
;
switch
(
cmd
)
{
case
1
:
cout
<<
"Enter key to add = "
;
cin
>>
key
;
std
::
cout
<<
"Enter key to add = "
;
std
::
cin
>>
key
;
addInfo
(
key
);
break
;
case
2
:
cout
<<
"Enter key to remove = "
;
cin
>>
key
;
std
::
cout
<<
"Enter key to remove = "
;
std
::
cin
>>
key
;
removalInfo
(
key
);
break
;
case
3
:
{
cout
<<
"Enter key to search = "
;
cin
>>
key
;
std
::
cout
<<
"Enter key to search = "
;
std
::
cin
>>
key
;
Entry
entry
=
table
[
quadraticProbe
(
key
,
true
)];
if
(
entry
.
key
==
notPresent
)
{
cout
<<
"Key not present"
;
std
::
cout
<<
"Key not present"
;
}
break
;
}
case
4
:
cout
<<
"Enter element to generate hash = "
;
cin
>>
key
;
cout
<<
"Hash of "
<<
key
<<
" is = "
<<
hashFxn
(
key
);
std
::
cout
<<
"Enter element to generate hash = "
;
std
::
cin
>>
key
;
std
::
cout
<<
"Hash of "
<<
key
<<
" is = "
<<
hashFxn
(
key
);
break
;
case
5
:
display
();
...
...
@@ -248,7 +243,7 @@ int main() {
break
;
// delete[] table;
}
cout
<<
endl
;
std
::
cout
<<
std
::
endl
;
}
return
0
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录