-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTable.java
182 lines (168 loc) · 3.42 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package vicky8rk;
class Node implements Comparable{
String data;
Node next;
Node(String x){
this.data = x.toUpperCase();
this.next = null;
}
Node(String x, Node next){
this.data = x.toUpperCase();
this.next = next;
}
public int compareTo(Object O){
if(O.getClass()!= this.getClass())
return -1;
Node newer = (Node)O;
return data.compareToIgnoreCase(newer.data);
}
void addNext(Node x){
x.next = this.next;
this.next = x;
System.out.println("Added Node "+x.data+" after Node "+this.data);
}
}
class LinkedList{
Node root=null;
LinkedList( String initValue){
root = new Node(initValue);
}
LinkedList( ){
}
public String toString(){
if(root == null){
return "Empty";
}
Node current = root;
String ret = "";
while(current.next!=null){
ret = ret + current.data;
current = current.next;
if(current!=null)
ret+=",";
}
return ":"+ret+current.data;
}
void add(String x){
Node neww = new Node(x);
if(root == null){ // empty linked list
System.out.println("Adding Root "+x);
root = neww;
return;
}
else{
Node current = root;
while(current.next != null)
current = current.next;
current.next = neww;
}
}
void addInOrder(String x){
Node neww = new Node(x);
if(root == null){ // empty linked list
System.out.println("Adding Root "+x);
root = neww;
return;
}
else{
Node current = root;
Node prev = root;
while(current.compareTo(neww)<=0){
prev = current;
current = current.next;
if(current==null)
break;
}
if(current==root){
neww.next = root;
root=neww;
}
else
prev.addNext(neww);
}
}
/*void addSorted(String x){ // figure out cases
if(root == null){ // empty linked list
System.out.println("Adding Root "+x);
root = new Node(x);
return;
}
System.out.println("Adding "+x+" to "+this.toString()); //
Node neww = new Node(x);
if(root.next==null){
if(neww.compareTo(root)<0){
neww.addNext(root);
root=neww;
}
else
root.addNext(neww);
return;
}
if(root.compareTo(O))
Node current = root;
while(current.next!=null){
if(neww.compareTo(current.next)<0){
current.addNext(neww);
return;
}
current = current.next;
if(current.next==null){
current.addNext(neww);
return;
}
}
}*/
}
public class HashTable {
LinkedList[] list;
HashTable(int k){
list = new LinkedList[k];
}
void map(int k, String str){
if(list[k]==null)
list[k]=new LinkedList(str);
else
list[k].addInOrder(str);
}
String get(int k){
return list[k].toString();
}
String getFirst(int k){
return list[k].root.data;
}
public String toString(){
String ret="";
for(int i=0;i<list.length;i++){
if(list[i]!=null)
ret = ret+i+":>"+list[i].toString()+"\n";
else
ret = ret+i+":> >Null List<\n";
}
return ret;
}
int hash(String x){
int ret = Math.abs(x.hashCode())%list.length;
//System.out.println(x+" ka hashcode="+ret);
return ret;
}
void add(String x){
this.map(hash(x), x);
}
public static void main(String... ars){
// LinkedList l = new LinkedList("Katara");
// System.out.println(l);
// l.addInOrder("Vicky");
// System.out.println(l);
// l.addInOrder("Nisha");
// System.out.println(l);
// l.addInOrder("A");
// System.out.println(l);
HashTable h = new HashTable(4);
h.add("Vicky");
h.add("Nisha");
h.add("Katara");
h.add("Ankita");
System.out.println(h);
//System.out.println(h.list[2]);
}
}