Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Dictionary
//g++ 5.4.0 #include <iostream> #include <string> class Entry { private: int _key; std::string _str; public: Entry() {} ~Entry() {} Entry(int key, std::string str){ this->_key = key; if (str.length() <= 30){ this->_str = str; } else { std::cout << "String is too large.\n"; } } int getKey() const { return _key; } std::string getString() const { return _str; } }; class Dictionary { public: Dictionary() { for (int i = 0; i < 5; i++){ arr[i] = nullptr; } } ~Dictionary() {} void add(int key, std::string s) { Entry e(key, s); Entry *p = &e; for (int i = 0; i < 5; i++) { if (arr[i] == nullptr) { arr[i] = p; } } } void remove(int key) { for (int i = 0; i < 5; i++){ if (arr[i]->getKey() == key){ if (i == 5){ delete arr[i]; break; } else{ for (int j = i; j < 5; j++){ arr[j] = arr[j+1]; } break; } } } } Entry* getEntry(int key){ for (int i = 0; i < 5; i++){ if (arr[i]->getKey() == key){ return arr[i]; } } std::cout << "Entry does not exist.\n"; return nullptr; } Entry* getEntryByIndex(int index){ return arr[index]; } Entry* searchFor(int key); private: Entry *arr[5]; }; int main() { Dictionary d; d.add(1, "Hello"); d.add(2, "World"); d.add(3, "Good"); d.add(4, "Morning"); Entry *test = d.getEntryByIndex(2); }
run
|
edit
|
history
|
help
0
fb_series
Updated Linked Lists - 5/10/2017 V3.0
Dar
ONP is working!
¡Arriba, Papalotes, Arriba! v2
12hours Clock c++
TwoANOVA
cotton farm 0.0.1.0
Kolokwium_2011_z12
logcOperator