Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- struct row
- {
- string key;
- string value;
- row* next;
- };
- class table
- {
- private:
- row* begin;
- public:
- table()
- {
- begin = nullptr;
- }
- void addRow(string k, string val)
- {
- row* elemToAdd = new row;
- elemToAdd->key = k;
- elemToAdd->value = val;
- elemToAdd->next = begin;
- begin = elemToAdd;
- }
- void changeRow(string k, string val)
- {
- row* rowToCheck = begin;
- while (rowToCheck != nullptr)
- {
- if (rowToCheck->key == k)
- {
- rowToCheck->value = val;
- break;
- }
- rowToCheck = rowToCheck->next;
- }
- }
- void deleteRow(string k)
- {
- if (begin == nullptr)
- {
- cout << "No elements!\n";
- return;
- }
- row* rowToDelete = begin;
- if (rowToDelete->key == k)
- {
- begin = begin->next;
- delete rowToDelete;
- return;
- }
- while (rowToDelete->next != nullptr)
- {
- if (rowToDelete->next->key == k)
- {
- rowToDelete->next = rowToDelete->next->next;
- delete rowToDelete->next;
- return;
- }
- rowToDelete = rowToDelete->next;
- }
- cout << "No such an element!\n";
- }
- string findRow(string k)
- {
- row* rowForCheck = begin;
- while (rowForCheck != nullptr)
- {
- if (rowForCheck->key == k)
- {
- return rowForCheck->value;
- }
- rowForCheck = rowForCheck->next;
- }
- return "Row wasn't found!\n";
- }
- void doIteration(void(*func)(row*))
- {
- row* el = begin;
- while (el != nullptr)
- {
- (*func)(el);
- el = el->next;
- }
- }
- ~table()
- {
- if (begin == nullptr)
- {
- return;
- }
- row* t = begin;
- while (t != nullptr)
- {
- begin = begin->next;
- delete t;
- t = begin;
- }
- }
- };
- void iterate(row* r)
- {
- r->value += "val";
- }
- bool testIterate()
- {
- bool testResult = true;
- table tab;
- tab.addRow("key1", "value1");
- tab.addRow("key2", "value2");
- tab.addRow("key3", "value3");
- tab.doIteration(iterate);
- testResult &= (tab.findRow("key1") == "value1val");
- testResult &= (tab.findRow("key2") == "value2val");
- testResult &= (tab.findRow("key3") == "value3val");
- return testResult;
- }
- bool testAddFind()
- {
- table tab;
- bool testResult = true;
- tab.addRow("first key", "first value");
- tab.addRow("second key", "second value");
- testResult &= (tab.findRow("first key") == "first value");
- testResult &= (tab.findRow("second key") == "second value");
- testResult &= (tab.findRow("third key") == "Row wasn't found!\n");
- return testResult;
- }
- bool testChange()
- {
- table tab;
- bool testResult = true;
- tab.addRow("first key", "first value");
- tab.addRow("second key", "second value");
- tab.changeRow("first key", "not a first value");
- testResult &= (tab.findRow("first key") == "not a first value");
- return testResult;
- }
- bool testDelete()
- {
- table tab;
- bool testResult = true;
- tab.addRow("first key", "first value");
- tab.addRow("second key", "second value");
- tab.deleteRow("first key");
- tab.deleteRow("second key");
- testResult &= (tab.findRow("first key") == "Row wasn't found!\n");
- testResult &= (tab.findRow("second key") == "Row wasn't found!\n");
- return testResult;
- }
- int main()
- {
- cout << "Table is adding and finding: " << boolalpha << testAddFind() << endl;
- cout << "Table is changing: " << testChange() << endl;
- cout << "Table is deleting: " << testDelete() << endl;
- cout << "Iterating... " << testIterate() << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement