-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchaning.java
More file actions
91 lines (82 loc) · 2.24 KB
/
Copy pathchaning.java
File metadata and controls
91 lines (82 loc) · 2.24 KB
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
/*
* 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.
*/
package javaapplication5;
import java.util.*;
class HashNode{
int key;
int value;
HashNode(int k, int v){
this.key = k;
this.value = v;
}
};
class Table{
int tableSize = 10;
LinkedList<HashNode>[] t;
Table(){
t = new LinkedList[tableSize];
for(int i=0; i<tableSize; i++){
if (t[i] == null) {
t[i] = new LinkedList<HashNode>();
}
}
}
int hashFunction(int key){
return key % tableSize;
}
void insert(int key, int value){
int hash = hashFunction(key);
HashNode node = new HashNode(key,value);
t[hash].add(node);
}
void search(int key){
int hash = hashFunction(key);
for(int i=0; i<t[hash].size(); i++){
HashNode temp = t[hash].get(i);
if(temp.key == key){
System.out.println("found ");
}
else if(i == t[hash].size()-1 && temp.key != key){
System.out.println("not found ");
}
}
}
void delete(int key){
int hash = hashFunction(key);
for(int i=0; i<t[hash].size(); i++){
HashNode temp = t[hash].get(i);
if(temp.key == key){
t[hash].remove(i);
}
else if(i == t[hash].size()-1 && temp.key != key){
System.out.println("not found ");
}
}
}
void display(){
for(int i=0; i<tableSize; i++){
System.out.print(i+"-> ");
if(t[i] == null){
System.out.println(i+" empty");
}else{
for(int j=0; j<t[i].size(); j++){
HashNode temp = t[i].get(j);
System.out.print(" "+temp.value);
}
}
System.out.print("\n");
}
}
};
public class chaning {
public static void main(String args[]){
Table t = new Table();
t.insert(10, 60);
t.insert(12, 70);
t.delete(12);
t.display();
}
}