Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- typedef struct Node
- {
- string key;
- int value;
- Node *next;
- }Node;
- int const n = 16;
- Node *NodeTab[n];
- int index(string key, int n)
- {
- int d = key.size();
- int sum = 0;
- for (int i = 0; i < d; i++)
- {
- sum = sum + key[i];
- }
- return sum % n;
- }
- void add(string key, int value)
- {
- int indeks = index(key, n);
- if (NodeTab[indeks]->value == -1)
- {
- NodeTab[indeks]->key = key;
- NodeTab[indeks]->value = value;
- }
- else
- {
- Node *tempNode = NodeTab[indeks];
- while (tempNode->next != NULL)
- {
- tempNode = tempNode->next;
- }
- Node *currentNode = new Node;
- currentNode->value = value;
- currentNode->key = key;
- currentNode->next = NULL;
- tempNode->next = currentNode;
- }
- }
- void show(string key)
- {
- Node *tempNode = NodeTab[0];
- int i = 0;
- while (i < n)
- {
- if (key == tempNode->key)
- {
- cout << tempNode->value;
- break;
- }
- else if (tempNode->next != NULL)
- {
- tempNode = tempNode->next;
- }
- else
- {
- tempNode = NodeTab[++i];
- }
- }
- }
- int ile(int indeks)
- {
- Node *tempNode = NodeTab[indeks];
- int suma = 0;
- if (tempNode->value != -1)
- {
- suma = suma+1;
- while (tempNode->next != NULL)
- {
- tempNode = tempNode->next;
- suma++;
- }
- }
- return suma;
- }
- int main()
- {
- for (int i = 0; i < n; i++)
- {
- NodeTab[i] = new Node;
- NodeTab[i]->key = "-1";
- NodeTab[i]->value = -1;
- NodeTab[i]->next = NULL;
- }
- int operacja;
- cin >> operacja;
- string key;
- int value;
- int index;
- while (operacja != 5)
- {
- if (operacja == 1)
- {
- cin >> key >> value;
- add(key, value);
- }
- else if (operacja == 2)
- {
- cin >> key;
- show(key);
- }
- else if (operacja == 3)
- {
- cin >> index;
- cout << ile(index);
- }
- cin >> operacja;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement