FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaSTUDY1/HT/HashFunction3.java at master · Slyrich/JavaSTUDY1 · GitHub
Slyrich
/
JavaSTUDY1
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaSTUDY1
/
HT
/
HashFunction3.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
185 lines (138 loc) · 4.46 KB
Breadcrumbs
JavaSTUDY1
/
HT
/
HashFunction3.java
Copy path
File metadata and controls
185 lines (138 loc) · 4.46 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
183
184
185
package
ht3
;
import
java
.
util
.
Scanner
;
public
class
HashFunction3
{
WordList
[]
theArray
;
int
arraySize
;
int
itemsInArray
=
0
;
public
String
[][]
elementsToAdd
= {
{
"ace"
,
"Very good"
},
{
"act"
,
"Take action"
},
{
"add"
,
"Join (something) to something else"
},
{
"age"
,
"Grow old"
},
{
"ago"
,
"Before the present"
},
{
"aid"
,
"Help, assist, or support"
},
{
"aim"
,
"Point or direct"
},
{
"air"
,
"Invisible gaseous substance"
},
{
"all"
,
"Used to refer to the whole quantity"
},
{
"amp"
,
"Unit of measure for the strength of an electrical current"
},
{
"and"
,
"Used to connect words"
}, {
"ant"
,
"A small insect"
},
{
"any"
,
"Used to refer to one or some of a thing"
},
{
"ape"
,
"A large primate"
},
{
"apt"
,
"Appropriate or suitable in the circumstances"
},
{
"arc"
,
"A part of the circumference of a curve"
},
{
"are"
,
"Unit of measure, equal to 100 square meters"
},
{
"ark"
,
"The ship built by Noah"
},
{
"arm"
,
"Two upper limbs of the human body"
},
{
"art"
,
"Expression or application of human creative skill"
},
{
"ash"
,
"Powdery residue left after the burning"
},
{
"ask"
,
"Say something in order to obtain information"
},
{
"asp"
,
"Small southern European viper"
},
{
"ass"
,
"Hoofed mammal"
},
{
"ate"
,
"To put (food) into the mouth and swallow it"
},
{
"atm"
,
"Unit of pressure"
},
{
"awe"
,
"A feeling of reverential respect"
},
{
"axe"
,
"Edge tool with a heavy bladed head"
},
{
"aye"
,
"An affirmative answer"
} };
public
int
hashString
(
String
sourceWord
){
int
hashKeyValue
=
0
;
for
(
int
i
=
0
;
i
<
sourceWord
.
length
();
i
++){
int
charCode
=
sourceWord
.
charAt
(
i
) -
96
;
int
temp
=
hashKeyValue
;
hashKeyValue
= (
hashKeyValue
*
27
+
charCode
) %
arraySize
;
System
.
out
.
println
(
"Hash Key Value "
+
temp
+
" * 27 + CharCode "
+
charCode
+
" % arraySize "
+
arraySize
+
"= "
+
hashKeyValue
);
}
System
.
out
.
println
();
return
hashKeyValue
;
}
HashFunction3
(
int
size
){
arraySize
=
size
;
theArray
=
new
WordList
[
size
];
for
(
int
i
=
0
;
i
<
arraySize
;
i
++){
theArray
[
i
] =
new
WordList
();
}
addTheArray
(
elementsToAdd
);
}
public
void
insert
(
Word
newWord
){
String
sourceWord
=
newWord
.
theWord
;
int
hashKey
=
hashString
(
sourceWord
);
theArray
[
hashKey
].
insert
(
newWord
,
hashKey
);
}
public
void
addTheArray
(
String
[][]
elementsToAdd
){
for
(
int
i
=
0
;
i
<
elementsToAdd
.
length
;
i
++){
String
word
=
elementsToAdd
[
i
][
0
];
String
definition
=
elementsToAdd
[
i
][
1
];
Word
newWord
=
new
Word
(
word
,
definition
);
insert
(
newWord
);
}
}
public
Word
find
(
String
wordToFind
){
int
hashKey
=
hashString
(
wordToFind
);
Word
theWord
=
theArray
[
hashKey
].
find
(
hashKey
,
wordToFind
);
return
theWord
;
}
public
void
displayTheArray
(){
for
(
int
i
=
0
;
i
<
arraySize
;
i
++){
System
.
out
.
println
(
"theArray Index: "
+
i
);
theArray
[
i
].
displayWordList
();
}
}
}
class
Word
{
public
String
theWord
;
public
String
definition
;
public
int
Key
;
public
Word
next
;
public
Word
(
String
theWord
,
String
definition
){
this
.
theWord
=
theWord
;
this
.
definition
=
definition
;
}
public
String
toString
(){
return
theWord
+
" : "
+
definition
;
}
}
class
WordList
{
public
Word
firstWord
=
null
;
public
void
insert
(
Word
newWord
,
int
hashKey
){
Word
previous
=
null
;
Word
current
=
firstWord
;
newWord
.
Key
=
hashKey
;
while
(
current
!=
null
&&
newWord
.
Key
>
current
.
Key
){
previous
=
current
;
current
=
current
.
next
;
}
if
(
previous
==
null
){
firstWord
=
newWord
;
}
else
previous
.
next
=
newWord
;
newWord
.
next
=
current
;
}
public
void
displayWordList
(){
Word
current
=
firstWord
;
while
(
current
!=
null
){
System
.
out
.
println
(
current
);
current
=
current
.
next
;
}
}
public
Word
find
(
int
hashKey
,
String
wordToFind
){
Word
current
=
firstWord
;
while
(
current
!=
null
&&
current
.
Key
<=
hashKey
){
if
(
current
.
theWord
.
equals
(
wordToFind
)){
return
current
;
}
current
=
current
.
next
;
}
return
null
;
}
public
static
void
main
(
String
[]
args
){
Scanner
input
=
new
Scanner
(
System
.
in
);
HashFunction3
wordHashTable
=
new
HashFunction3
(
11
);
String
wordLookUp
=
"a"
;
while
(!
wordLookUp
.
equalsIgnoreCase
(
"x"
)){
System
.
out
.
println
(
": "
);
wordLookUp
=
input
.
nextLine
();
System
.
out
.
println
(
wordHashTable
.
find
(
wordLookUp
));
}
wordHashTable
.
displayTheArray
();
}
}
Back
|
FazBrowse Home
|
New Git URL