Question: //trendtracker.h #ifndef TRENDTRACKER_H #define TRENDTRACKER_H #include #include using namespace std; class Trendtracker { public: Trendtracker(); void insert(string ht); int size(); void tweeted(string ht); int popularity(string
//trendtracker.h #ifndef TRENDTRACKER_H #define TRENDTRACKER_H
#include
using namespace std;
class Trendtracker {
public: Trendtracker();
void insert(string ht);
int size();
void tweeted(string ht);
int popularity(string name);
string top_trend();
void top_three_trends(vector
void top_k_trends(vector
private: class Entry { public: string hashtag; int pop; };
vector
#endif
//trendtracker.cpp
#include"trendtracker.h" using namespace std;
// Creates a new Trendtracker tracking no hashtags. // // Must run in O(1) time. Trendtracker::Trendtracker(){
}
// Inserts a hashtag (tweeted 0 times) into the Trendtracker. // If the hashtag already is in Trendtracker, does nothing. // // Must run in O(n) time. void Trendtracker::insert(string ht){ Entry t; int n = E.size(); t.hashtag = ht; t.pop = 0; for (int i = 0; i < n; i++) { if (ht == E[i].hashtag) { break; } } }
// Return the number of hashtags in the Trendtracker. // // Must run in O(1) time. int Trendtracker::size(){ return E.size(); }
// Adds 1 to the total number times a hashtag has been tweeted. // If the hashtag does not exist in TrendTracker, does nothing. // // Must run in O(n) time. void Trendtracker::tweeted(string ht) { int n = E.size(); for (int i = 0; i < n; i++) { if (E[i].hashtag == ht) { E[i].pop++; } } }
// Returns the number of times a hashtag has been tweeted. // If the hashtag does not exist in Trendtracker, returns -1. // // Must run in O(n) time. int Trendtracker::popularity(string name){ int i = 0; int n = E.size(); bool check = 0;
for (int i = 0; i < n; i++) { if (name == E[i].hashtag) { check = 1; } }
if (check == 1) { return E[i].pop; } else { return -1; } }
// Returns a most-tweeted hashtag. // If the Trendtracker has no hashtags, returns "". // // Must run in O(n) time. string Trendtracker::top_trend(){ if (E.size() == 0) { return ""; } int popular; for (int i = 0, n = E.size(), popular = 0; i < n; i++) { if (E[i].pop > E[popular].pop) { popular = i; } } return E[popular].hashtag;
}
// Fills the provided vector with the 3 most-tweeted hashtags, // in order from most-tweeted to least-tweeted. // // If there are fewer than 3 hashtags, then the vector is filled // with all hashtags (in most-tweeted to least-tweeted order). // // Must run in O(n) time. void Trendtracker::top_three_trends(vector
T.push_back(E[f].hashtag); if (s != n && s != f) T.push_back(E[s].hashtag); if (t != n && t != s) T.push_back(E[t].hashtag); }
// Fills the provided vector with the k most-tweeted hashtags, // in order from most-tweeted to least-tweeted. // // If there are fewer than k hashtags, then the vector is filled // with all hashtags (in most-tweeted to least-tweeted order). // // Must run in O(nk) time. void Trendtracker::top_k_trends(vector
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
