Question
Hello I need help finishing the program the below pics have all the instructions for how the code works and there is also code that
Hello I need help finishing the program the below pics have all the instructions for how the code works and there is also code that I have which doesn't work so if someone could fix it that would be great I will also attach some example to test the code out with.
Here is my code:
#include
struct Node { int val; Node* next; };
void delete_linked_list(Node* head) { while (head != nullptr) { Node* nextNode = head->next; delete head; head = nextNode; } }
void writeFile(const vector
void run(const string& src_path, const string& dst_path) { ifstream file(src_path); int numNodes; int numEdges; int maxDegree; file >> numNodes >> numEdges; vector
for (int i = 0; i < numEdges; i++) { int temp1; int temp2; file >> temp1 >> temp2; for (int i = 0; i < numEdges; i++) { int temp1; int temp2; file >> temp1 >> temp2; Node* newNode1 = new Node{temp2, adjList[temp1]}; adjList[temp1] = newNode1; Node* newNode2 = new Node{temp1, adjList[temp2]}; adjList[temp2] = newNode2; } file.close(); }
writeFile(adjList, dst_path); int totalDegree = 0; for (int i = 0; i < adjList.size(); i++) { int degree = 0; Node* currNode = adjList[i]; while (currNode != nullptr) { degree++; currNode = currNode->next; } totalDegree += degree; maxDegree = max(maxDegree, degree); } int numEdgesCalculated = totalDegree / 2; cout << "Number of nodes: " << numNodes << endl; cout << "Number of edges: " << numEdgesCalculated << endl; cout << "Maximum degree: " << maxDegree << endl; for (int i = 0; i < adjList.size(); i++) { delete_linked_list(adjList[i]); } }
int main() { run("karate.txt", "last.txt"); }
Here is the example of an input file the code is tested on: