using
System;
class
HashNode
{
public
int
Key {
get
;
set
; }
public
int
Value {
get
;
set
; }
public
HashNode(
int
key,
int
value)
{
Key = key;
Value = value;
}
}
class
HashMap
{
private
const
int
Capacity = 20;
private
int
size = 0;
private
HashNode[] arr =
new
HashNode[Capacity];
private
readonly
HashNode dummy =
new
HashNode(-1, -1);
public
void
Insert(
int
key,
int
value)
{
HashNode temp =
new
HashNode(key, value);
int
hashIndex = key % Capacity;
while
(arr[hashIndex] !=
null
&& arr[hashIndex].Key != key && arr[hashIndex].Key != -1)
{
hashIndex++;
hashIndex %= Capacity;
}
if
(arr[hashIndex] ==
null
|| arr[hashIndex].Key == -1)
{
size++;
}
arr[hashIndex] = temp;
}
public
bool
DeleteKey(
int
key)
{
int
hashIndex = key % Capacity;
while
(arr[hashIndex] !=
null
)
{
if
(arr[hashIndex].Key == key)
{
arr[hashIndex] = dummy;
size--;
return
true
;
}
hashIndex++;
hashIndex %= Capacity;
}
return
false
;
}
public
int
Find(
int
key)
{
int
hashIndex = key % Capacity;
int
counter = 0;
while
(arr[hashIndex] !=
null
)
{
if
(counter > Capacity)
{
break
;
}
if
(arr[hashIndex].Key == key)
{
return
arr[hashIndex].Value;
}
hashIndex++;
hashIndex %= Capacity;
counter++;
}
return
-1;
}
}
class
Program
{
static
void
Main()
{
HashMap hashMap =
new
HashMap();
hashMap.Insert(1, 5);
hashMap.Insert(2, 15);
hashMap.Insert(3, 20);
hashMap.Insert(4, 7);
if
(hashMap.Find(4) != -1)
{
Console.WriteLine(
"Value of Key 4 = "
+ hashMap.Find(4));
}
else
{
Console.WriteLine(
"Key 4 does not exist"
);
}
if
(hashMap.DeleteKey(4))
{
Console.WriteLine(
"Node value of key 4 is deleted successfully"
);
}
else
{
Console.WriteLine(
"Key does not exist"
);
}
if
(hashMap.Find(4) != -1)
{
Console.WriteLine(
"Value of Key 4 = "
+ hashMap.Find(4));
}
else
{
Console.WriteLine(
"Key 4 does not exist"
);
}
}
}