summaryrefslogtreecommitdiffstats
path: root/02-data_structures/03-hash_tables/01-phone_book/phone_book.cpp
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2016-11-13 22:22:51 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2016-11-13 22:22:51 +0100
commitb524eca827b182b0a51952557e36c945c779f848 (patch)
treebf14635a2c502826b198d07138ba038e489068e0 /02-data_structures/03-hash_tables/01-phone_book/phone_book.cpp
parente6b725d69c07d4a582d0aadc915def99d872dfde (diff)
downloadcoursera-b524eca827b182b0a51952557e36c945c779f848.zip
coursera-b524eca827b182b0a51952557e36c945c779f848.tar.gz
Algorithms : complete 02-data_structures 03-hash_tables
Diffstat (limited to '02-data_structures/03-hash_tables/01-phone_book/phone_book.cpp')
-rw-r--r--02-data_structures/03-hash_tables/01-phone_book/phone_book.cpp39
1 files changed, 12 insertions, 27 deletions
diff --git a/02-data_structures/03-hash_tables/01-phone_book/phone_book.cpp b/02-data_structures/03-hash_tables/01-phone_book/phone_book.cpp
index 182c36f..6f5e9af 100644
--- a/02-data_structures/03-hash_tables/01-phone_book/phone_book.cpp
+++ b/02-data_structures/03-hash_tables/01-phone_book/phone_book.cpp
@@ -32,37 +32,22 @@ void write_responses(const vector<string>& result) {
vector<string> process_queries(const vector<Query>& queries) {
vector<string> result;
- // Keep list of all existing (i.e. not deleted yet) contacts.
- vector<Query> contacts;
- for (size_t i = 0; i < queries.size(); ++i)
+
+ vector<string> contacts(10000000);
+
+ for (size_t i = 0; i < queries.size(); ++i) {
+ int j = queries[i].number;
if (queries[i].type == "add") {
- bool was_founded = false;
- // if we already have contact with such number,
- // we should rewrite contact's name
- for (size_t j = 0; j < contacts.size(); ++j)
- if (contacts[j].number == queries[i].number) {
- contacts[j].name = queries[i].name;
- was_founded = true;
- break;
- }
- // otherwise, just add it
- if (!was_founded)
- contacts.push_back(queries[i]);
+ contacts[j] = queries[i].name;
} else if (queries[i].type == "del") {
- for (size_t j = 0; j < contacts.size(); ++j)
- if (contacts[j].number == queries[i].number) {
- contacts.erase(contacts.begin() + j);
- break;
- }
+ contacts[j] = "";
} else {
- string response = "not found";
- for (size_t j = 0; j < contacts.size(); ++j)
- if (contacts[j].number == queries[i].number) {
- response = contacts[j].name;
- break;
- }
- result.push_back(response);
+ if (contacts[j].empty())
+ result.push_back("not found");
+ else
+ result.push_back(contacts[j]);
}
+ }
return result;
}