-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
95 lines (83 loc) · 2.42 KB
/
test.cpp
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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
struct Entry {
string key;
int value;
Entry* next;
Entry(string key, int value) : key(key), value(value), next(nullptr) {}
};
class EntryList {
public:
Entry* head;
EntryList() : head(nullptr) {}
static Entry* find(EntryList* entry, string key) {
Entry* current = entry->head;
while (current) {
if (current->key == key) {
return current;
}
current = current->next;
}
return nullptr;
}
static void show(EntryList* entry, string key) {
Entry* element = find(entry, key);
if (element) {
cout << "Key: " << element->key << ", Value: " << element->value << endl;
}
else {
cout << "Element o podanym keyu nie istnieje." << endl;
}
}
static void insert(EntryList* entry, string key, int value) {
if (find(entry, key)) {
cout << "Element o podanym keyu już istnieje." << endl;
return;
}
Entry* new_element = new Entry(key, value);
if (!entry->head) {
entry->head = new_element;
}
else {
Entry* current = entry->head;
while (current->next) {
current = current->next;
}
current->next = new_element;
}
}
static void delete_entry(EntryList* entry, string key) {
Entry* current = entry->head;
Entry* prev = nullptr;
while (current) {
if (current->key == key) {
if (prev) {
prev->next = current->next;
}
else {
entry->head = current->next;
}
delete current;
return;
}
prev = current;
current = current->next;
}
cout << "Element o podanym keyu nie istnieje." << endl;
}
};
string create_key(const int len) {
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
string tmp_s;
tmp_s.reserve(len);
for (int i = 0; i < len; ++i) {
tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)];
}
return tmp_s;
}