Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please review following codes and change the code so that it satisfies the output file rule. / / code #include #include #include #include #define INF

Please review following codes and change the code so that it satisfies the output file rule.
//code
#include
#include
#include
#include
#define INF INT_MAX
typedef struct {
int node;
int cost;
} Edge;
typedef struct {
int from;
int to;
char msg[1000];
} Message;
typedef struct {
int node1;
int node2;
int cost;
} Change;
typedef struct {
Edge* edges;
int size;
int capacity;
} EdgeList;
void add_edge(EdgeList* list, int node, int cost);
void dijkstra(int src, EdgeList* graph, int n, int* dist, int* parent);
void print_routing_table(int n, int* parent, int* dist, FILE* out);
void process_topology(const char* filename, int* n, EdgeList** graph);
void process_messages(const char* filename, Message** messages, int* message_count);
void process_changes(const char* filename, Change** changes, int* change_count);
void update_graph(EdgeList* graph, Change change);
void add_edge(EdgeList* list, int node, int cost){
if (list->size == list->capacity){
list->capacity *=2;
list->edges =(Edge*)realloc(list->edges, list->capacity * sizeof(Edge));
if (!list->edges){
perror("Failed to realloc edges");
exit(EXIT_FAILURE);
}
}
list->edges[list->size].node = node;
list->edges[list->size].cost = cost;
list->size++;
}
void dijkstra(int src, EdgeList* graph, int n, int* dist, int* parent){
int* visited =(int*)malloc(n * sizeof(int));
if (!visited){
perror("Failed to malloc visited");
exit(EXIT_FAILURE);
}
for (int i =0; i < n; i++){
dist[i]= INF;
parent[i]=-1;
visited[i]=0;
}
dist[src]=0;
for (int i =0; i < n; i++){
int u =-1;
for (int j =0; j < n; j++){
if (!visited[j] && (u ==-1|| dist[j]< dist[u])){
u = j;
}
}
if (u ==-1|| dist[u]== INF) break;
visited[u]=1;
for (int j =0; j < graph[u].size; j++){
Edge edge = graph[u].edges[j];
int v = edge.node;
int weight = edge.cost;
if (dist[u]+ weight < dist[v]){
dist[v]= dist[u]+ weight;
parent[v]= u;
}
}
}
free(visited);
}
void print_routing_table(int n, int* parent, int* dist, FILE* out){
for (int i =0; i < n; i++){
if (dist[i]== INF) continue;
fprintf(out,"%d %d %d
", i, parent[i], dist[i]);
}
}
void process_topology(const char* filename, int* n, EdgeList** graph){
FILE* in = fopen(filename,"r");
if (!in){
perror("Failed to open topology file");
exit(EXIT_FAILURE);
}
if (fscanf(in,"%d", n)!=1){
perror("Failed to read the number of nodes");
fclose(in);
exit(EXIT_FAILURE);
}
*graph =(EdgeList*)malloc(*n * sizeof(EdgeList));
if (!*graph){
perror("Failed to malloc graph");
fclose(in);
exit(EXIT_FAILURE);
}
for (int i =0; i <*n; i++){
(*graph)[i].edges =(Edge*)malloc(10* sizeof(Edge));
if (!(*graph)[i].edges){
perror("Failed to malloc edges");
fclose(in);
exit(EXIT_FAILURE);
}
(*graph)[i].size =0;
(*graph)[i].capacity =10;
}
int u, v, cost;
while (fscanf(in,"%d %d %d", &u, &v, &cost)!= EOF){
add_edge(&(*graph)[u], v, cost);
add_edge(&(*graph)[v], u, cost);
}
fclose(in);
}
void process_messages(const char* filename, Message** messages, int* message_count){
FILE* in = fopen(filename,"r");
if (!in){
perror("Failed to open messages file");
exit(EXIT_FAILURE);
}
*message_count =0;
*messages =(Message*)malloc(100* sizeof(Message));
if (!*messages){
perror("Failed to malloc messages");
fclose(in);
exit(EXIT_FAILURE);
}
char line[1024];
while (fgets(line, sizeof(line), in)){
int u, v;
char msg[1000];
if (sscanf(line,"%d %d %[^
]", &u, &v, msg)!=3){
fprintf(stderr, "Error reading message: %s", line);
continue;
}
(*messages)[*message_count].from = u;
(*messages)[*message_count].to = v;
strcpy((*messages)[*message_count].msg, msg);
(*message_count)++;
}
fclose(in);
}
void process_changes(const char* filename, Change** changes, int* change_count){
FILE* in = fopen(filename,"r");
if (!in){
perror("Failed to open changes file");
exit(EXIT_FAILURE);
}
*change_count =0;
*changes =(Change*)malloc(100* sizeof(Change));
if (!*changes){
perror("Failed to malloc changes");
fclose(in);
exit(EXIT_FAILURE);
}

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

Step: 3

blur-text-image

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

Lab Manual For Database Development

Authors: Rachelle Reese

1st Custom Edition

1256741736, 978-1256741732

More Books

Students also viewed these Databases questions

Question

1. Define mass and mediated communication

Answered: 1 week ago