-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathHashtable.java
79 lines (71 loc) · 1.49 KB
/
Hashtable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Scanner;
class Student{
int key;
String date;
String name;
Student next;
}
class Hashtable{
Scanner s=new Scanner(System.in);
Student node=new Student();
static Hashtable n=new Hashtable();
Student arr[]=new Student[10];
void Hash(){
System.out.print("enter input elements");
for(int x=0;x<5;x++) {
Student node=new Student();
node.key = s.nextInt();
node.date=s.next();
node.name=s.next();
int v= n.calculate(node.key);
if (arr[v]==null)
{
node.next=null;
arr[v]=node;
}
else
{
Student ptr;
ptr=arr[v];
while(ptr.next!=null){
ptr=ptr.next;
}
ptr.next=node;
node.next=null;
}
}
}
int calculate(int value)
{
int index =value%10;
return index;
}
void display()
{
for(int i=0;i<10;i++){
if(arr[i]==null)
{
System.out.println(arr[i]);
}
else
{
Student ptr=arr[i];
while(ptr!=null)
{
System.out.print("->"+ptr.key);
ptr=ptr.next;
}
System.out.println();
}
}}
public static void main(String args[])
{
n.Hash();
n.display();
}
}