Skip to content

Commit 3c619c4

Browse files
Separate Chaining in Python
1 parent 1e5d15c commit 3c619c4

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed

hashing/separate-chaining.py

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of DSA course available on CourseGalaxy.com
3+
4+
##################################################################
5+
6+
class studentRecord:
7+
def __init__(self,i,name):
8+
self.studentId = i
9+
self.studentName = name
10+
11+
def get_student_id(self):
12+
return self.studentId
13+
14+
def set_student_id(self,i):
15+
self.studentId = i
16+
17+
def __str__(self):
18+
return str(self.studentId) + " " + self.studentName
19+
20+
##################################################################
21+
22+
class Node:
23+
def __init__(self,value):
24+
self.info = value
25+
self.link = None
26+
27+
##################################################################
28+
29+
class SingleLinkedList:
30+
def __init__(self):
31+
self.start = None
32+
33+
def display_list(self):
34+
if self.start is None:
35+
print("___")
36+
return
37+
p = self.start
38+
while p is not None:
39+
print(p.info , " ", end='')
40+
p = p.link
41+
print()
42+
43+
def search(self,x):
44+
p = self.start
45+
while p is not None:
46+
if p.info.get_student_id() == x:
47+
return p.info
48+
p = p.link
49+
else:
50+
return None
51+
52+
def insert_in_beginning(self, data):
53+
temp = Node(data)
54+
temp.link = self.start
55+
self.start = temp
56+
57+
def delete_node(self,x):
58+
if self.start is None:
59+
print("List is empty")
60+
return
61+
62+
# Deletion of first node
63+
if self.start.info.get_student_id() == x:
64+
self.start = self.start.link
65+
return
66+
67+
# Deletion in between or at the end
68+
p = self.start
69+
while p.link is not None:
70+
if p.link.info.get_student_id() == x:
71+
break
72+
p = p.link
73+
74+
if p.link is None:
75+
print("Element ", x ,"not in list")
76+
else:
77+
p.link = p.link.link
78+
79+
##################################################################
80+
81+
82+
class HashTable:
83+
84+
def __init__(self,tableSize):
85+
self.m = tableSize
86+
self.array = [None] * self.m
87+
self.n = 0
88+
89+
def hash(self, key):
90+
return (key % self.m)
91+
92+
def display_table(self):
93+
for i in range(self.m):
94+
print( "[" , i , "] --> ", end ='' )
95+
if self.array[i]!= None:
96+
self.array[i].display_list()
97+
else:
98+
print("___")
99+
100+
101+
def search(self, key):
102+
h = self.hash(key)
103+
if self.array[h] != None:
104+
return self.array[h].search(key)
105+
return None
106+
107+
def insert(self, newRecord):
108+
key = newRecord.get_student_id()
109+
h = self.hash(key)
110+
111+
if self.array[h] == None:
112+
self.array[h] = SingleLinkedList()
113+
self.array[h].insert_in_beginning(newRecord)
114+
self.n+=1
115+
116+
117+
118+
def delete(self, key):
119+
h = self.hash(key)
120+
if self.array[h] != None:
121+
self.array[h].delete_node(key)
122+
self.n-=1
123+
else:
124+
print("Value " , key , " not present")
125+
126+
127+
128+
##################################################################
129+
130+
size = int(input("Enter size of table : "))
131+
table = HashTable(size)
132+
133+
while True:
134+
print("1.Insert a record")
135+
print("2.Search a record")
136+
print("3.Delete a record")
137+
print("4.Display table")
138+
print("5.Exit")
139+
140+
choice = int(input("Enter your choice : "))
141+
if choice == 1:
142+
id = int(input("Enter student id : "))
143+
name = input("Enter student name : ")
144+
aRecord = studentRecord(id,name)
145+
table.insert(aRecord)
146+
elif choice == 2:
147+
id = int(input("Enter a key to be searched : "))
148+
aRecord = table.search(id)
149+
if aRecord is None:
150+
print("Key not found")
151+
else:
152+
print(aRecord)
153+
elif choice == 3:
154+
id = int(input("Enter a key to be deleted :"))
155+
table.delete(id)
156+
elif choice == 4:
157+
table.display_table()
158+
elif choice == 5:
159+
break
160+
else:
161+
print("Wrong option")
162+
print()
163+
164+
165+
166+

0 commit comments

Comments
 (0)