Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Linear Probing method for handling collisions
- def insert(hasht, n):
- key = int(input("Enter the value to be inserted: "))
- loc = key % n
- if (hasht[loc] == -1):
- hasht[loc] = key
- else:
- while (hasht[loc] != -1):
- loc = (loc + 1) % n
- hasht[loc] = key
- def display(hasht):
- print("\nHashtable: ", hasht)
- def search(hasht, size):
- key = int(input("Enter the element to be searched: "))
- start = key % size
- i = start
- flag = 0
- found = -1
- while (True):
- if (hasht[i] == key):
- flag = 1
- found = i
- break
- else:
- flag = 0
- i = (i + 1) % size
- if (i == start):
- break
- if (flag == 0):
- print("\nKey not found")
- if (flag == 1):
- print("\nKey found at position: ", found)
- def main():
- size = int(input("Enter the size of the Hash Table: "))
- hasht = []
- for i in range(0, size):
- hasht.append(-1)
- count = 0
- while (True):
- print("\n----- MENU -----")
- print("1. Insert")
- print("2. Display")
- print("3. Search")
- print("4. Exit")
- ch = input("Enter your choice: ")
- if ch == "1":
- if (count >= size):
- print("\nCannot Insert! The Hash Table is full")
- else:
- insert(hasht, size)
- display(hasht)
- count += 1
- elif ch == "2":
- display(hasht)
- elif ch == "3":
- search(hasht, size)
- elif ch == "4":
- return
- else:
- print("Invalid Input")
- main()
Advertisement
Add Comment
Please, Sign In to add comment