-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linear_Hash_Table.py
92 lines (83 loc) · 2.11 KB
/
Linear_Hash_Table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class Linear_Hash_Table:
def __init__(self, size):
self.table = [0 for i in range(size)]
self.size = size
def hash_function(self, key):
index = key % self.size
return index
def insert(self, key):
index = self.hash_function(key)
temp = index
if (self.table[index] == 0):
self.table[index] = key
print("Insertion Successful")
else:
while(self.table[temp]!=0):
temp += 1
if (temp==self.size):
print("Hash Table is Full, insertion unsucessfull")
break
if (temp!=self.size):
self.table[temp] = key
print("Insertion Successful")
def search(self, key):
index = self.hash_function(key)
temp = index
if (self.table[index] == key):
print("Search Successfull")
return index
else:
while(self.table[temp]!=key):
temp += 1
if (temp==self.size):
print("Search unsucessfull")
break
if (temp!=self.size and self.table[temp]==key):
print("Search Successfull")
return temp
def delete(self, key):
index = self.hash_function(key)
temp = index
if (self.table[index] == key):
self.table[index] = 0
print("Deletion Successful")
else:
while(self.table[temp]!=key):
temp += 1
if (temp==self.size):
print("Deletion unsucessfull, key not found in Hash Table")
break
if (temp!=self.size and self.table[temp]==key):
self.table[temp] = 0
print("Deletion Successful")
def traverse(self):
print(self.table)
def is_Full(self):
index = 0
while (index<self.size and self.table[index]!=0):
index += 1
if (index==self.size):
return True
else:
return False
# Driver Code
Table = Linear_Hash_Table(10)
Table.insert(5)
Table.traverse()
Table.insert(2)
Table.traverse()
Table.insert(35)
Table.traverse()
Table.insert(22)
Table.traverse()
Table.insert(222)
Table.traverse()
Table.insert(2222)
Table.traverse()
Table.delete(222)
Table.traverse()
Table.delete(22)
Table.traverse()
Table.is_Full()
Table.search(222)
Table.search(5)