Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <cstdlib>
- struct Link {
- Link(int value) {
- this->value = value;
- this->next = NULL;
- }
- int value;
- Link* next;
- };
- Link* hashTable[10000];
- int getHashTableIndex(int key) { return abs(key) % 9721; }
- bool existCmd(int key) {
- int index = getHashTableIndex(key);
- if (!hashTable[index]) {
- return false;
- }
- else {
- Link *curLink = hashTable[index];
- while (true) {
- if (curLink->value == key) return true;
- if (!curLink->next) return false;
- else curLink = curLink->next;
- }
- }
- }
- void insertCmd(int key) {
- if (existCmd(key)) return;
- int index = getHashTableIndex(key);
- if (!hashTable[index]) {
- hashTable[index] = new Link(key);
- }
- else {
- Link* curLink = hashTable[index];
- while (curLink->next) curLink = curLink->next;
- curLink->next = new Link(key);
- }
- }
- void deleteCmd(int key) {
- if (existCmd(key)) {
- int index = getHashTableIndex(key);
- Link *curLink = hashTable[index];
- if (curLink->value == key) {
- hashTable[index] = curLink->next;
- delete curLink;
- return;
- }
- while (true) {
- Link* nextLink = curLink->next;
- if (nextLink->value == key) {
- curLink->next = nextLink->next;
- delete nextLink;
- nextLink = NULL;
- return;
- }
- curLink = nextLink;
- }
- }
- }
- int main() {
- std::ios_base::sync_with_stdio(0);
- freopen("/Users/Alex/ClionProjects/cpp_binary/input.txt", "r", stdin);
- freopen("/Users/Alex/ClionProjects/cpp_binary/output.txt", "w", stdout);
- //freopen("set.in", "r", stdin);
- //freopen("set.out", "w", stdout);
- int x;
- std::string cmd;
- while (std::cin >> cmd) {
- std::cin >> x;
- switch (cmd[0]) {
- case 'i':
- insertCmd(x);
- break;
- case 'd':
- deleteCmd(x);
- break;
- case 'e':
- if (existCmd(x)) std::cout << "true\n";
- else std::cout << "fasle\n";
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement