Skip to content

Commit a5452d7

Browse files
Open Addressing in Java
0 parents  commit a5452d7

File tree

6 files changed

+462
-0
lines changed

6 files changed

+462
-0
lines changed

Diff for: hashing/OpenAddressing/Demo.java

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package OpenAddressing;
7+
8+
import java.util.Scanner;
9+
10+
public class Demo
11+
{
12+
public static void main(String[] args)
13+
{
14+
int id,choice;
15+
String name;
16+
17+
Scanner scan = new Scanner(System.in);
18+
19+
System.out.print("Enter initial size of table : ");
20+
int size = scan.nextInt();
21+
22+
HashTable table = new HashTable(size);
23+
24+
while(true)
25+
{
26+
System.out.println("1.Insert a record");
27+
System.out.println("2.Search a record");
28+
System.out.println("3.Delete a record");
29+
System.out.println("4.Display table");
30+
System.out.println("5.Exit");
31+
32+
System.out.print("Enter your choice : ");
33+
choice = scan.nextInt();
34+
35+
if (choice == 5)
36+
break;
37+
38+
switch(choice)
39+
{
40+
case 1 :
41+
System.out.print("Enter student id : ");
42+
id = scan.nextInt();
43+
System.out.print("Enter student name : ");
44+
name = scan.next();
45+
46+
studentRecord aRecord = new studentRecord(id,name);
47+
48+
table.Insert(aRecord);
49+
break;
50+
case 2 :
51+
System.out.print("Enter a key to be searched : ");
52+
id = scan.nextInt();
53+
aRecord = table.Search(id);
54+
55+
if( aRecord==null )
56+
System.out.println("Key not found");
57+
else
58+
System.out.println(aRecord);
59+
60+
break;
61+
case 3:
62+
System.out.print("Enter a key to be deleted : ");
63+
id = scan.nextInt();
64+
table.Delete(id);
65+
break;
66+
case 4:
67+
table.DisplayTable();
68+
break;
69+
70+
}
71+
}
72+
scan.close();
73+
}
74+
}

Diff for: hashing/OpenAddressing/HashTable.java

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package OpenAddressing;
7+
8+
public class HashTable
9+
{
10+
private studentRecord[] array;
11+
private int m; //size of the array
12+
int n; //number of records
13+
14+
public HashTable()
15+
{
16+
this(11);
17+
}
18+
19+
public HashTable(int tableSize)
20+
{
21+
m = tableSize;
22+
array = new studentRecord[m];
23+
n = 0;
24+
}
25+
26+
int hash(int key)
27+
{
28+
return(key%m);
29+
}
30+
31+
public void Insert(studentRecord newRecord)
32+
{
33+
int key = newRecord.getstudentId();
34+
int h = hash(key);
35+
36+
int location = h;
37+
38+
for( int i=1; i<m; i++)
39+
{
40+
if( array[location]==null || array[location].getstudentId()==-1 )
41+
{
42+
array[location]=newRecord;
43+
n++;
44+
return;
45+
}
46+
47+
if(array[location].getstudentId() == key)
48+
throw new UnsupportedOperationException("Duplicate key");
49+
50+
location = (h+i)%m;
51+
}
52+
System.out.println("Table is full : Record can't be inserted ");
53+
}
54+
55+
public studentRecord Search(int key)
56+
{
57+
int h = hash(key);
58+
int location=h;
59+
60+
for(int i=1; i<m ; i++)
61+
{
62+
if( array[location]==null )
63+
return null;
64+
if(array[location].getstudentId() == key)
65+
return array[location];
66+
location = (h+i)%m;
67+
}
68+
return null;
69+
}
70+
71+
public void DisplayTable()
72+
{
73+
for(int i=0; i<m; i++)
74+
{
75+
System.out.print( "[" + i + "] --> " );
76+
77+
if(array[i]!=null && array[i].getstudentId()!=-1)
78+
System.out.println( array[i] );
79+
else
80+
System.out.println("___");
81+
}
82+
}
83+
84+
85+
public studentRecord Delete(int key)
86+
{
87+
int h = hash(key);
88+
int location=h;
89+
90+
for(int i=1; i<m ; i++)
91+
{
92+
if( array[location]==null)
93+
return null;
94+
if(array[location].getstudentId() == key)
95+
{
96+
studentRecord temp = array[location];
97+
array[location].setstudentId(-1);
98+
n--;
99+
return temp;
100+
}
101+
location = (h+i)%m;
102+
}
103+
return null;
104+
}
105+
}
106+

Diff for: hashing/OpenAddressing/studentRecord.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package OpenAddressing;
7+
8+
public class studentRecord
9+
{
10+
private int studentId;
11+
private String studentName;
12+
13+
public studentRecord(int i, String name)
14+
{
15+
studentId=i;
16+
studentName = name;
17+
}
18+
public int getstudentId()
19+
{
20+
return studentId;
21+
}
22+
public void setstudentId(int i)
23+
{
24+
studentId = i;
25+
}
26+
public String toString()
27+
{
28+
return studentId + " " + studentName + " ";
29+
}
30+
}

Diff for: hashing/OpenAddressing1/Demo.java

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package OpenAddressing1;
7+
8+
import java.util.Scanner;
9+
10+
public class Demo
11+
{
12+
public static void main(String[] args)
13+
{
14+
int id,choice;
15+
String name;
16+
17+
Scanner scan = new Scanner(System.in);
18+
19+
System.out.print("Enter initial size of table : ");
20+
int size = scan.nextInt();
21+
22+
HashTable table = new HashTable(size);
23+
24+
while(true)
25+
{
26+
System.out.println("1.Insert a record");
27+
System.out.println("2.Search a record");
28+
System.out.println("3.Delete a record");
29+
System.out.println("4.Display table");
30+
System.out.println("5.Exit");
31+
32+
System.out.print("Enter your choice : ");
33+
choice = scan.nextInt();
34+
35+
if (choice == 5)
36+
break;
37+
38+
switch(choice)
39+
{
40+
case 1 :
41+
System.out.print("Enter student id : ");
42+
id = scan.nextInt();
43+
System.out.print("Enter student name : ");
44+
name = scan.next();
45+
46+
studentRecord aRecord = new studentRecord(id,name);
47+
48+
table.Insert1(aRecord);
49+
break;
50+
case 2 :
51+
System.out.print("Enter a key to be searched : ");
52+
id = scan.nextInt();
53+
aRecord = table.Search(id);
54+
55+
56+
if( aRecord==null )
57+
System.out.println("Key not found");
58+
else
59+
System.out.println(aRecord);
60+
61+
break;
62+
case 3:
63+
System.out.print("Enter a key to be deleted : ");
64+
id = scan.nextInt();
65+
table.Delete(id);
66+
break;
67+
case 4:
68+
table.DisplayTable();
69+
break;
70+
71+
}
72+
}
73+
scan.close();
74+
}
75+
}

0 commit comments

Comments
 (0)