Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include #include #include using namespace std;

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& adjList, const string& path) { ofstream file(path); for (int i = 0; i < adjList.size(); i++) { file << i << ":"; Node* currNode = adjList[i]; while (currNode != nullptr) { file << " " << currNode->val; currNode = currNode->next; } file << endl; } file.close(); }

void run(const string& src_path, const string& dst_path) { ifstream file(src_path); int numNodes; int numEdges; int maxDegree; file >> numNodes >> numEdges; vector adjList(numNodes, nullptr);

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:

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions