Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion lesson4/src/RelatedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public boolean equals(Object o) {
}

private Node<T> head;
private Node<T> nextItem;

public RelatedList() {
head = null;
Expand All @@ -36,6 +37,17 @@ public void insert(T c) {
head = n;
}

public T find(T t) {
Node<T> n = new Node<>(t);
Node<T> current = head;
while (!current.equals(n)) {
if (current.next == null)
return null;
current = current.next;
}
return current.c;
}

public T remove() {
if (isEmpty())
return null;
Expand All @@ -55,7 +67,26 @@ public boolean contains(T c) {
}
return true;
}


public T delete(T t) {
Node<T> current = head;
Node<T> previous = head;
while (!current.c.equals(t)) {
if (current.next == null)
return null;
else {
previous = current;
current = current.next;
}
}
if (current == head)
head = head.next;
else
previous.next = current.next;

return current.c;
}

public T delete(String name) {
Node<T> current = head;
Node<T> previous = head;
Expand Down Expand Up @@ -87,4 +118,21 @@ public String toString() {
}
return sb.toString();
}

public boolean hasNext() {
if (head == null)
return false;
if (nextItem == null)
return true;
return nextItem.next != null;
}

public T next () {
if (nextItem == null) {
nextItem = head;
return nextItem.c;
}
nextItem = nextItem.next;
return nextItem.c;
}
}
32 changes: 31 additions & 1 deletion lesson8/src/HashTable.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
public class HashTable {
private Item[] hashArr;
private RelatedList<Item>[] chainedHashTable;
private int arrSize;
private Item nullItem;

public HashTable(int arrSize) {
this.arrSize = arrSize;
hashArr = new Item[arrSize];
nullItem = new Item(-1);
chainedHashTable = new RelatedList[arrSize];
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arrSize; i++) {
sb.append((hashArr[i] != null) ? hashArr[i].getKey() : "*");
sb.append((chainedHashTable[i] != null) ? chainedHashTable[i] : "*");
if (i < arrSize - 1) {
sb.append(", ");
}
Expand All @@ -25,6 +27,14 @@ private int hashFunc(int key) {
return key % arrSize;
}

public void chainedInsert(Item item) {
int key = item.getKey();
int hash = hashFunc(key);
if (chainedHashTable[hash] == null)
chainedHashTable[hash] = new RelatedList<>();
chainedHashTable[hash].insert(item);
}

public void insert(Item item) {
int key = item.getKey();
int hashVal = hashFunc(key);
Expand All @@ -43,6 +53,16 @@ public void insert(Item item) {
hashArr[hashVal] = item;
}

public Item chainedFind(int key) {
int hash = hashFunc(key);
while (chainedHashTable[hash].hasNext()) {
Item item = chainedHashTable[hash].next();
if (item.getKey() == key)
return item;
}
return null;
}

public Item find(int key) {
int hashVal = hashFunc(key);
// int step = 0;
Expand All @@ -64,6 +84,16 @@ int dblHashFunc(int key) {
return 19 - (key % 19);
}

public Item chainedDelete(int key) {
int hash = hashFunc(key);
while (chainedHashTable[hash].hasNext()) {
Item item = chainedHashTable[hash].next();
if (item.getKey() == key)
return chainedHashTable[hash].delete(item);
}
return null;
}

public Item delete(int key) {
int hashVal = hashFunc(key);
// int step = 0;
Expand Down
5 changes: 5 additions & 0 deletions lesson8/src/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(data);
}

@Override
public String toString() {
return Integer.toString(data);
}
}
6 changes: 3 additions & 3 deletions lesson8/src/Online.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ public class Online {
public static void main(String[] args) {
HashTable ht = new HashTable(25);
for (int i = 0; i < 11; i++) {
ht.insert(new Item(i * 5));
ht.chainedInsert(new Item(i * 5));
}
System.out.println(ht);
ht.delete(25);
ht.chainedDelete(25);
System.out.println(ht);
ht.insert(new Item(75));
ht.chainedInsert(new Item(75));
System.out.println(ht);
}
}