Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit a4255bd

Browse files
committed
lab 27-28 complete
1 parent 5cdb699 commit a4255bd

File tree

6 files changed

+158
-0
lines changed

6 files changed

+158
-0
lines changed

students/23K0351/23K0351-p27/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>23K0351</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0351-p27</artifactId>
12+
<description>/</description>
13+
</project>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
import java.util.LinkedList;
4+
5+
class HashTab {
6+
private LinkedList<Entry>[] table;
7+
private int size;
8+
9+
10+
private class Entry {
11+
String key;
12+
String value;
13+
14+
Entry(String key, String value) {
15+
this.key = key;
16+
this.value = value;
17+
}
18+
}
19+
20+
21+
public void hashtabInit(int size) {
22+
this.size = size;
23+
table = new LinkedList[size];
24+
for (int i = 0; i < size; i++) {
25+
table[i] = new LinkedList<>();
26+
}
27+
}
28+
29+
30+
private int hashtabHash(String key) {
31+
return Math.abs(key.hashCode()) % size;
32+
}
33+
34+
35+
public void hashtabAdd(String key, String value) {
36+
int index = hashtabHash(key);
37+
for (Entry entry : table[index]) {
38+
if (entry.key.equals(key)) {
39+
entry.value = value;
40+
return;
41+
}
42+
}
43+
table[index].add(new Entry(key, value));
44+
}
45+
46+
47+
public String hashtabLookup(String key) {
48+
int index = hashtabHash(key);
49+
for (Entry entry : table[index]) {
50+
if (entry.key.equals(key)) {
51+
return entry.value;
52+
}
53+
}
54+
return null;
55+
}
56+
57+
58+
public void hashtabDelete(String key) {
59+
int index = hashtabHash(key);
60+
table[index].removeIf(entry -> entry.key.equals(key));
61+
}
62+
63+
64+
public void display() {
65+
for (int i = 0; i < size; i++) {
66+
if (!table[i].isEmpty()) {
67+
for (Entry entry : table[i]) {
68+
System.out.println("Ключ: " + entry.key + ", Значение: " + entry.value);
69+
}
70+
}
71+
}
72+
}
73+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
4+
5+
6+
public abstract class Main {
7+
public static void main(String[] args) {
8+
HashTab hashTab = new HashTab();
9+
hashTab.hashtabInit(10);
10+
11+
12+
hashTab.hashtabAdd("ключ1", "значение1");
13+
hashTab.hashtabAdd("ключ2", "значение2");
14+
hashTab.hashtabAdd("ключ3", "значение3");
15+
hashTab.hashtabAdd("ключ4", "значение4");
16+
hashTab.hashtabAdd("ключ5", "значение5");
17+
hashTab.hashtabAdd("ключ6", "значение6");
18+
hashTab.hashtabAdd("ключ7", "значение7");
19+
hashTab.hashtabAdd("ключ8", "значение8");
20+
hashTab.hashtabAdd("ключ9", "значение9");
21+
hashTab.hashtabAdd("ключ10", "значение10");
22+
23+
24+
System.out.println("Поиск ключа 'ключ5': " + hashTab.hashtabLookup("ключ5"));
25+
System.out.println("Поиск ключа 'ключ11': " + hashTab.hashtabLookup("ключ11"));
26+
27+
28+
hashTab.hashtabDelete("ключ3");
29+
System.out.println("После удаления ключа 'ключ3':");
30+
hashTab.display();
31+
}
32+
}

students/23K0351/23K0351-p28/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>23K0351</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0351-p28</artifactId>
12+
<description>/</description>
13+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
import java.util.HashSet;
4+
import java.util.TreeSet;
5+
6+
public abstract class Tree {
7+
public static void main(String[] args) {
8+
9+
HashSet<String> hashSet = new HashSet<>();
10+
hashSet.add("banana");
11+
hashSet.add("apple");
12+
hashSet.add("orange");
13+
hashSet.add("grape");
14+
hashSet.add("kiwi");
15+
16+
17+
System.out.println("HashSet: " + hashSet);
18+
19+
20+
TreeSet<String> treeSet = new TreeSet<>(hashSet);
21+
22+
23+
System.out.println("TreeSet: " + treeSet);
24+
}
25+
}

students/23K0351/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
<module>23K0351-p24</module>
3434
<module>23K0351-p25</module>
3535
<module>23K0351-p26</module>
36+
<module>23K0351-p27</module>
37+
<module>23K0351-p28</module>
3638

3739

3840

0 commit comments

Comments
 (0)