Array ( [0] => [1] => questions [2] => Popular-Algorithms [3] => Collision-Avoidance ) Popular Algorithms | Collision Avoidance | THE INQUISITIVE





Collision Avoidance

LEVEL:Beginner

Description

Perform collision Avoidance using separate chaining on the hash table ask input from the user to perform operations such as insert remove and get the max size of table print the table after every operations

Input Format

First line contains N representing max size of the hash table

Output Format

show the options to the user and print the output as per the user requirement.


Example 1:

Input
10
Output
1 for insert
2 for remove
3 to max size
1
2

Bucket 0:  
Bucket 1:  
Bucket 2:  2 
Bucket 3:  
Bucket 4:  
Bucket 5:  
Bucket 6:  
Bucket 7:  
Bucket 8:  
Bucket 9:  
Example 2:

Input
5
Output
1 for insert
2 for remove
3 to max size
1
5

Bucket 0:  5 
Bucket 1:  
Bucket 2:  
Bucket 3:  
Bucket 4:  
Do you want to continue y or n
y
1 for insert
2 for remove
3 to max size
1
5

Bucket 0:  5 5 
Bucket 1:  
Bucket 2:  
Bucket 3:  
Bucket 4:  
Do you want to continue y or n
n
Example 3:

Input
8
Output
1 for insert
2 for remove
3 to max size
1
4

Bucket 0:  
Bucket 1:  
Bucket 2:  
Bucket 3:  
Bucket 4:  4 
Bucket 5:  
Bucket 6:  
Bucket 7:  
Do you want to continue y or n
y
1 for insert
2 for remove
3 to max size
2
4

Bucket 0:  
Bucket 1:  
Bucket 2:  
Bucket 3:  
Bucket 4:  
Bucket 5:  
Bucket 6:  
Bucket 7:  
Do you want to continue y or n
y
1 for insert
2 for remove
3 to max size
3
8

Bucket 0:  
Bucket 1:  
Bucket 2:  
Bucket 3:  
Bucket 4:  
Bucket 5:  
Bucket 6:  
Bucket 7:  
Do you want to continue y or n
n

oops

Login to see Discussion




Approach


Step-i)create a class SLLNode which has next and data as instances
Step-ii)through constructor assign data part to x and next to null
Step-
Step-iii)create a class collision avoidance which contains size, max_size and array
Step-iv)allocate memory to table using constructor,initialize max size to size and size to 0
Step-v)create a method insert which takes val as argument
Step-vi)increment size by one value and assign result of myhash(val) to pos
Step-vii)create ptr of SLLNode type and assign val to it
Step-viii)if table[pos] equal to null assign ptr to table[pos]
Step-ix)else assign table[pos] to ptr.next and ptr to table[pos]
Step-x) create a method remove which takes val as arguement
Step-xi) assign value of myhash(val) to pos, table[pos] to start and start to end
Step-xii)if start.data is equal to val decrement size , assign start.next to table[pos] and return;
Step-xiii) if end.next equal to null print "element not found"
Step-xiv)decrement size by one value and if end.next.next equal to null assign null to end.next and return
Step-xv)assign end.next.next to next and start to table[pos]
Step-xvi) create myhash function which takes integer as argument
Step-xvii)assign value of X.hashcode to hashval and update hashval to hashval%table.length
Step-xviii)if hashval is zero add table length to it
Step-xix) return hashval
Step-xx)create method print table to print the contents of the hashtable
Step-xxi)in main method take required inputs such as array lenght and array elements provide input as per the users choice


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution