fixed A1 code

This commit is contained in:
K 2024-02-24 01:05:11 +05:30
parent 3c720a4074
commit 537edadc18
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F
2 changed files with 188 additions and 168 deletions

View File

@ -1,168 +0,0 @@
// WARNING: THIS CODE HAS NOT BEEN TESTED.
// IT WILL TAKE 1-2 MONTHS TO PERFECT THE CODE.
// IF YOU FACE ANY ERRORS, UNEXPECTED BEHAVIOUR OR HAVE ANY SUGGESTIONS,
// LET US KNOW BY CREATING AN ISSUE ON OUR KSKA GIT REPOSITORY.
/*
Problem Statement: Consider telephone book database of N clients. Make use of a hash table implementation to quickly look up client's telephone number. Make use of two collision handling techniques and compare them using number of comparisons required to find a set of telephone numbers.
Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
*/
// BEGINNING OF CODE
#include<iostream>
#include<cstring>
#define max 10
using namespace std;
struct client
{
long int iPhno;
char name[20];
};//end of structure
class hashtable
{
client ht[max];
public:
hashtable() //constructor
{
for(int i=0;i<max;i++)
{
ht[i].iPhno=0;
}
}
void insert();
void display();
int search(int);
int del(int);
int hash(long int);
};//end of class
void hashtable::insert()
{
client c;
int iPos;
char cAns;
do
{
cout<<"\n Enter Phone Number:";
cin>>c.iPhno;
iPos=hash(c.iPhno);
if(ht[iPos].iPhno==0)
{
ht[iPos]=c;
}
else
{
for(int i=iPos+1;i%max!=iPos;i++)
{
ht[i]=c;
break;
}
}
cout<<"\n Add More:";
cin>>cAns;
}while(cAns=='y' || cAns=='Y');
}//end of insert
int hashtable::hash(long int key)
{
return(key%max);
}//end of hash
void hashtable::display()
{
cout<<"------------------------------------";
cout<<"\nSrno\tPhone number\n";
cout<<"------------------------------------\n";
for(int i=0;i<max;i++)
{
cout<<i<<"\t"<<ht[i].iPhno<<endl;
}
cout<<"------------------------------------\n";
}//end of display
int hashtable::search(int x)
{
int iFlag=0;
cout<<"Enter Phone number to be searched:";
cin>>x;
for(int i=0;i<max;i++)
{
if(ht[i].iPhno==x)
{
cout<<"\n Phone Number Found at position "<<i;
iFlag=1;
}
}
if(iFlag==0)
cout<<"\n Phone Number Not Found";
}//end of search
int hashtable::del(int s)
{
int iF=0;
cout<<"\n Enter phone number to be deleted:";
cin>>s;
for(int i=0;i<max;i++)
{
if(ht[i].iPhno==s)
{
ht[i].iPhno=0;
cout<<"\n Phone number found and deleted";
iF=1;
}
}
if(iF==0)
cout<<"\n Phone number not found";
}//end of del
int main()
{
int y,s,iCh;
hashtable h;
do
{
cout<<"\n---------------LIST---------------\n";
cout<<"\n1.INSERT\n2.DISPLAY\n3.SEARCH\n4.DELETE\n5.EXIT\n\n";
cout<<"Enter your choice:";
cin>>iCh;
cout<<"\n";
switch(iCh)
{
case 1://insert
h.insert();
cout<<"\n";
break;
case 2://display
h.display();
cout<<"\n";
break;
case 3://search
h.search(y);
cout<<"\n";
break;
case 4://delete
h.del(s);
cout<<"\n";
break;
case 5://exit
break;
}//end of switch
}while(iCh!=5);//end of do
return 0;
}
// END OF CODE
// EXPERIMENTAL CODE

188
Codes/Practical-A1.py Normal file
View File

@ -0,0 +1,188 @@
'''
Problem Statement: Consider telephone book database of N clients. Make use of a hash table implementation to quickly look up client's telephone number. Make use of two collision handling techniques and compare them using number of comparisons required to find a set of telephone numbers.
Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/
'''
# BEGINNING OF CODE
class HashEntry:
# Blueprint for entry in hash table
def __init__(self):
# Initializing empty values
self.name = None
self.num = -1
def insert(self, name, num):
# Insert value
self.name = name
self.num = num
def collision(self):
# Handle collision
return self.name is not None
def hashOne(size, val):
# Calculate first hashing function
return val % size
def hashTwo(val):
# Calculate second hashing function
return 7 - (val % 7)
def finalHash(val, size, i):
# Determine final position of value in hash table (after first and second hash is caluclated)
return (hashOne(size, val) + i * hashTwo(val)) % size
def stringToInt(strn):
# ASCII value of each character
sum = 0
for i in strn:
sum += ord(i)
return sum
class LinearProbing:
# Function to handle linear probing
def __init__(self, size):
self.size = size
self.HashTable = []
for _ in range(size):
self.HashTable.append(HashEntry())
def insert(self):
inputStr = input("Enter telephone NUMBER and NAME of client (separated by space):\t")
inputVal = inputStr.split()
if len(inputVal) != 2:
print("\n==========\nPlease enter both telephone number and name.\n==========")
return
num, name = inputVal
name2 = stringToInt(name)
pos = hashOne(self.size, name2)
i = 1
while self.HashTable[pos].collision():
pos = (pos + i) % self.size
i += 1
self.HashTable[pos].insert(name, num)
print("\n==========\nInserted\n==========")
def search(self):
name = input("Enter name of the client:\t")
name2 = stringToInt(name)
pos = hashOne(self.size, name2)
i = 1
while self.HashTable[pos].name != name:
pos = (pos + i) % self.size
i += 1
if i == self.size + 1:
break
else:
print("\n==========\nTelephone number of the client", name, "is", self.HashTable[pos].num, "\n==========")
return
print("\n==========\nClient not found.\n==========")
def display(self):
j = 0
print("Pos", "Name", "Value", sep="\t|\t")
print("-----", "-----", "-----", sep="\t+\t")
for i in self.HashTable:
print(j, i.name, i.num, sep="\t|\t")
j += 1
class DoubleHashing:
# Function to handle double hashing
def __init__(self, size):
self.size = size
self.HashTable = []
for _ in range(size):
self.HashTable.append(HashEntry())
def insert(self):
inputStr = input("Enter telephone NUMBER and NAME of client (separated by space):\t")
inputVal = inputStr.split()
if len(inputVal) != 2:
print("\n==========\nPlease enter both telephone number and name.\n==========")
return
num, name = inputVal
name2 = stringToInt(name)
i = 0
while True:
pos = finalHash(name2, self.size, i)
if self.HashTable[pos].collision():
i += 1
else:
break
self.HashTable[pos].insert(name, num)
print("\n==========\nInserted\n==========")
def search(self):
name = input("Enter name of the client:\t")
name2 = stringToInt(name)
i = 0
while True:
pos = finalHash(name2, self.size, i)
if self.HashTable[pos].name != name:
i += 1
else:
break
if i == self.size:
break
print("\n==========\nTelephone number of client", name, "is", self.HashTable[pos].num, "\n==========")
def display(self):
j = 0
print("Pos", "Name", "Value", sep="\t|\t")
print("-----", "-----", "-----", sep="\t+\t")
for i in self.HashTable:
print(j, i.name, i.num, sep="\t|\t")
j += 1
def main():
# Main function with options
tableSize = int(input("Enter size of hash table:\t"))
method = None
while True:
print("----- MAIN MENU -----")
print("1 -> Linear Probing")
print("2 -> Double Hashing")
print("3 -> Exit")
optn = int(input("Choose an option (1-3):\t"))
if optn == 1:
method = LinearProbing(tableSize)
elif optn == 2:
method = DoubleHashing(tableSize)
elif optn == 3:
print("\n\n## END OF CODE\n")
exit(1)
else:
print("Please choose a valid option (1-3).")
continue
while True:
print("\n----- CHOOSE OPERATION (HASH TABLE) -----")
print("1 -> Insert")
print("2 -> Search")
print("3 -> Display")
print("4 -> Return to previous menu")
optn = int(input("Choose an option (1-4):\t"))
if optn == 1:
method.insert()
elif optn == 2:
method.search()
elif optn == 3:
method.display()
elif optn == 4:
break
else:
print("Please choose a valid option (1-4).")
continue
# Calling main function
main()
# END OF CODE