Question: I need the code in C++ using a singly linked list. typedef std::string KeyType; typedef double ValueType; class Map { public: Map(); // Create an
I need the code in C++ using a singly linked list.
typedef std::string KeyType; typedef double ValueType; class Map { public: Map(); // Create an empty map (i.e., one with no key/value pairs) bool empty() const; // Return true if the map is empty, otherwise false. int size() const; // Return the number of key/value pairs in the map. bool insert(const KeyType& key, const ValueType& value); // If key is not equal to any key currently in the map, and if the // key/value pair can be added to the map, then do so and return true. // Otherwise, make no change to the map and return false (indicating // that either the key is already in the map). bool update(const KeyType& key, const ValueType& value); // If key is equal to a key currently in the map, then make that key no // longer map to the value it currently maps to, but instead map to // the value of the second parameter; return true in this case. // Otherwise, make no change to the map and return false. bool insertOrUpdate(const KeyType& key, const ValueType& value); // If key is equal to a key currently in the map, then make that key no // longer map to the value it currently maps to, but instead map to // the value of the second parameter; return true in this case. // If key is not equal to any key currently in the map then add it and // return true. In fact this function always returns true. bool erase(const KeyType& key); // If key is equal to a key currently in the map, remove the key/value // pair with that key from the map and return true. Otherwise, make // no change to the map and return false. bool contains(const KeyType& key) const; // Return true if key is equal to a key currently in the map, otherwise // false. bool get(const KeyType& key, ValueType& value) const; // If key is equal to a key currently in the map, set value to the // value in the map that that key maps to, and return true. Otherwise, // make no change to the value parameter of this function and return // false. bool get(int i, KeyType& key, ValueType& value) const; // If 0 <= i < size(), copy into the key and value parameters the // key and value of one of the key/value pairs in the map and return // true. Otherwise, leave the key and value parameters unchanged and // return false. (See below for details about this function.) void swap(Map& other); // Exchange the contents of this map with the other one. }; Below are the test cases to test whether the functions work correctly. You can just post the code for the functions you know. Thank you.
Map m; m.insert("A", 10); m.insert("B", 44); m.insert("C", 10); string all; double total = 0; for (int n = 0; n < m.size(); n++) { string k; double v; m.get(n, k, v); all += k; total += v; } cout << all << total;
Map gpas; gpas.insert("Fred", 2.956); gpas.insert("Ethel", 3.538); double v; string k1; assert(gpas.get(1,k1,v) && (k1 == "Fred" || k1 == "Ethel")); string k2; assert(gpas.get(1,k2,v) && k2 == k1);
Map gpas; gpas.insert("Fred", 2.956); assert(!gpas.contains("")); gpas.insert("Ethel", 3.538); gpas.insert("", 4.000); gpas.insert("Lucy", 2.956); assert(gpas.contains("")); gpas.erase("Fred"); assert(gpas.size() == 3 && gpas.contains("Lucy") && gpas.contains("Ethel") && gpas.contains(""));
Map m1; m1.insert("Fred", 2.956); Map m2; m2.insert("Ethel", 3.538); m2.insert("Lucy", 2.956); m1.swap(m2); assert(m1.size() == 2 && m1.contains("Ethel") && m1.contains("Lucy") && m2.size() == 1 && m2.contains("Fred"));
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
