[英]How to delete a node in HashTable in Java?
我是編程新手。 我目前正在學習 Java 編程語言。 我的問題是,我正在嘗試刪除包含 HashTable 中的值的特定節點,但我無法弄清楚 go 哪里出錯了。 有人可以向我解釋如何刪除這個特定節點嗎? 先感謝您。 對不起,我的英語不好。
我想瞄准的Output:應該刪除“Albert”和“Timmy” 。
這是我的代碼:
Class: HashEntry
public class HashEntry {
private int key;
private String value;
private HashEntry next;
public HashEntry() {
this.next = null;
}
public HashEntry(int key, String value) {
this.key = key;
this.value = value;
this.next = null;
}
public int getKey() {
return key;
}
public String getValue() {
return value;
}
public void setNext(HashEntry next) {
this.next = next;
}
public HashEntry getNext() {
return this.next;
}
}
Class:哈希表數組
public class HashTableArray {
HashEntry[] arrayHash;
int size;
public HashTableArray(int size) {
this.size = size;
arrayHash = new HashEntry[size];
}
public int getHash(int key) {
return key % size;
}
public void insert(int key, String value){
int hashInd = getHash(key);
HashEntry newVal = new HashEntry(key, value);
if (arrayHash[hashInd] == null) {
arrayHash[hashInd] = newVal;
}
else {
HashEntry arrayValue = arrayHash[hashInd];
while (arrayValue.getNext() != null) {
arrayValue = arrayValue.getNext();
}
arrayValue.setNext(newVal);
}
}
public void displayTable() {
System.out.println("Hash Table:");
for (int i=0; i<arrayHash.length; i++) {
if(arrayHash[i] != null) {
HashEntry temp = arrayHash[i];
while(temp.getNext() != null) {
System.out.println(temp.getKey() + ", " + temp.getValue());
temp = temp.getNext();
}
System.out.println(temp.getKey() + ", " + temp.getValue());
}
}
}
public void delete (int key, String value) {
int hashInd = getHash(key);
HashEntry head = arrayHash[hashInd];
if (arrayHash[hashInd] != null) {
HashEntry temp = head, prev = null;
if (temp.getNext() == null && head.getValue().equalsIgnoreCase(value)) {
head = null;
}
else if (temp.getNext() != null && temp.getValue().equalsIgnoreCase(value)) {
head = temp.getNext();
}
else {
while(!temp.getValue().equalsIgnoreCase(value)) {
prev = temp;
temp = temp.getNext();
}
if (temp == null) {
prev.setNext(null);
}
else {
prev.setNext(temp.getNext());
}
}
}
}
}
Class:哈希表程序
public class HashTableProgram {
public static void main(String[] args) {
HashTableArray h = new HashTableArray (10);
h.insert(12, "Albert");
h.insert(26, "Johnson");
h.insert(5, "Timmy");
h.insert(12, "George");
h.displayTable();
h.delete(12, "Albert");
h.delete(5, "Timmy");
System.out.println("\nAfter deleted...");
h.displayTable();
}
}
經過快速測試,問題出在你在HashTableArray中的delete方法上,你應該改
head = null;
head = temp.getNext();
至
arrayHash[hashInd] = null;
arrayHash[hashInd] = temp.getNext();
最終代碼將如下所示:
public void delete(int key, String value) {
int hashInd = getHash(key);
HashEntry head = arrayHash[hashInd];
if (arrayHash[hashInd] != null) {
HashEntry temp = head, prev = null;
if (temp.getNext() == null && head.getValue().equalsIgnoreCase(value)) {
arrayHash[hashInd] = null;
} else if (head.getNext() != null && head.getValue().equalsIgnoreCase(value)) {
arrayHash[hashInd] = temp.getNext();
} else {
while (!temp.getValue().equalsIgnoreCase(value)) {
prev = temp;
temp = temp.getNext();
}
if (temp == null) {
prev.setNext(null);
} else {
prev.setNext(temp.getNext());
}
}
}
}
問題是,當您像這樣將arrayHash[hashInd]
分配給head
時:
HashEntry head = arrayHash[hashInd];
您正在創建一個引用(或指針)變量head
,它指向堆上的實際 HashEntry 數據。 如果您嘗試將head
設置為null ,那么您只是更改了head
指向的內容,現在它指向了null 。 您沒有更改arrayHash
中的實際 HashEntry 數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.