Question: C + + PLEASE 1 Write a file called h . node, which defines the following class: class Node { private: std::string name; std::vector neighbors;
C PLEASE
Write a file called hnode, which defines the following class:
class Node
private:
std::string name;
std::vector neighbors;
public:
Nodeconst std::string& input;
void addNeighborNode pneighbor;
const std::vector& getNeighbors;
bool nameIsconst std::string& input;
void print;
;
The name variable holds the username. The neighbors variable holds pointers to its friends.
It is allowed to declare additional functions and variables inside and outside the class, according to the requirements of your solution to the exercise.
This is also true in the following questions.
Write a file called cppnode, which defines the functions declared in hnode.
The class constructor copies the contents of the input parameter to the name member of the class.
The addNeighbor function adds a friend to the list of friends stored in the neighbors variable. It does not allocate space in memory to a new network user, but adds a pointer to a member for whom space has already been allocated.
The allocation of space for a new user will be done in the Graph class later in the exercise.
The getNeighbors function returns the list of friends, that is it returns the neighbors variable.
The nameIs function returns true if the input parameter is the same as name, and false if not.
The print function prints the name variable.
Write a file called hgraph, which defines the following class:
class Graph
private:
std::vector nodes;
Node findNodeconst std::string& name;
bool areConnectedNode psrc Node pdest,
std::vector& currentpath,
std::vector& minpath;
public:
~Graph;
void addNodeconst std::string& name;
void addEdgeconst std::string& first, const std::string& second;
void printPathconst std::string& src const std::string& dest;
The nodes variable holds pointers to all users of the social network.
Write a file called cppgraph, which defines the functions declared in hgraph.
The addNode function dynamically allocates memory for a new network user, and adds a pointer to this user to the nodes variable. The new username will be name. If this name already exists in the network, the function will print
Name already exists.
In that case the function will not allocate memory and will not add the user to the network.
The class destructor frees all dynamically allocated memory for network users.
The addEdge function adds a friendship relationship between two existing users in the network. Friendship bond is always
Reciprocal: If A is a friend of B then B is a friend of A
If one or both of the usernames does not exist on the network, the function will print
Users not found.
And don't add the friendship link.
If the function receives two users who are already members, it will print
Already friends.
And you won't do anything.
If the function receives the same name in both parameters, it will print
Adam karov etzel atzmo.
And you won't do anything.
The printPath function accepts two usernames, and prints the shortest path between them. For example, suppose the social network looks like this: pictured
If the function receives "Bob" and "David" as parameters, it will print
Bob Carol David because it is the shortest path between them.
If one or both of the usernames does not exist on the network, the function will print
Users not found.
If the function receives two users that are not related through other people, it will print No path.
If the function receives the same name in both parameters, it will print Adam karov etzel atzmo.
If there are several paths that are the same length, the function can print any of them. For example, if the function accepts "Alice" and "Fred" as parameters, it can print either Alice Bob Fred or Alice Elisa Fred
The findNode function accepts a name, and returns the pointer to the user with the same name. If there is no user with that name, the function will return nullptr. This is a private function so you don't have to implement it I wrote it down here as a recommendation for solving the exercise.
The function areConnected is a recursive function, which receives pointers to two users, a path that has just been checked pathcurrent and the minimum path found so far. It returns true if there is a path between the users that does not pass through a user already in pathcurrent, and false if there is none. This is a private function and therefore You don't have to implement it I wrote it here as a recommendation for solving the exercise: I think the function printPath should call it
I recommend implementing the recursion like this:
currentpath to psrc you add
If srcp is the same as destp we have reached our destination. If the length of pathcurrent is less than the length of pathmin or there is still no pathmin we found a shorter path than we had until now. In such a case, we copy pathcurrent to pathmin.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
