Kindly answer the following C programming question based on the following code! #include #include #define MAX_VERTEX_NUM 20 //MAX number of vertices   typedef struct edgenode { char vertex; int weight; struct edgenode *nextEdge; } EdgeNode, *EdgeNodePtr;   typedef struct vertexnode { char vertex; EdgeNodePtr firstEdge; } VertexNode, *VertexNodePtr;   typedef struct { int size; char vertices[MAX_VERTEX_NUM]; VertexNode AdjList[MAX_VERTEX_NUM]; } AdjListGraph, *Graph;   //Create a graph of n vertices, use dynamic memory //- given the vertices and the size, return the graph Graph createGraph(char vertices[], int size) { Graph graph = (Graph)malloc(sizeof(AdjListGraph)); graph->size = size; for (int i = 0; i < size; i++) { graph->vertices[i] = vertices[i]; graph->AdjList[i].vertex = vertices[i]; graph->AdjList[i].firstEdge = NULL; } return graph; }   //Print the graph as an adjacency matrix void printGraph(Graph graph) { if (graph == NULL) { puts("Graph is empty"); return; } for (int i = 0; i < graph->size; i++) { printf("|%c|->", graph->vertices[i]); EdgeNodePtr curr = graph->AdjList[i].firstEdge; while(curr != NULL) { printf("|%c|%d|->", curr->vertex, curr->weight); curr = curr->nextEdge; } puts("NULL"); } }   //Delete the graph, i.e. free the memory Graph deleteGraph(Graph graph) { if (graph == NULL) { puts("Graph is empty"); return NULL; } for (int i = 0; i < graph->size; i++) { EdgeNodePtr pre = NULL; EdgeNodePtr curr = graph->AdjList[i].firstEdge; while(curr != NULL) { pre = curr; curr = curr->nextEdge; free(pre); } } free(graph); return NULL; }   //Do NOT MODIFY ANY CODE ABOVE THIS LINE --------------------------------------   void addEdge(Graph graph, int u, int v, int w) { //TODO   }   //Do NOT MODIFY ANY CODE BELOW THIS LINE --------------------------------------   int main() { //Create the graph with 5 vertices char vertices[MAX_VERTEX_NUM]; for (int i = 0; i < MAX_VERTEX_NUM; i++) vertices[i] = 'A' + i; Graph g1 = createGraph(vertices, 5);   //Add edges int edges[] = {0,1,3, 1,2,2, 1,3,4, 2,4,5, 3,2,5, 4,3,2}; for (int i = 0; i < (int)(sizeof(edges)/sizeof(int)); i += 3) addEdge(g1, edges[i], edges[i+1], edges[i+2]); printGraph(g1);   //Delete the graph g1 = deleteGraph(g1); } //end of main

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Kindly answer the following C programming question based on the following code!

#include <stdio.h>

#include <stdlib.h>

#define MAX_VERTEX_NUM 20 //MAX number of vertices

 

typedef struct edgenode {

char vertex;

int weight;

struct edgenode *nextEdge;

} EdgeNode, *EdgeNodePtr;

 

typedef struct vertexnode {

char vertex;

EdgeNodePtr firstEdge;

} VertexNode, *VertexNodePtr;

 

typedef struct {

int size;

char vertices[MAX_VERTEX_NUM];

VertexNode AdjList[MAX_VERTEX_NUM];

} AdjListGraph, *Graph;

 

//Create a graph of n vertices, use dynamic memory

//- given the vertices and the size, return the graph

Graph createGraph(char vertices[], int size) {

Graph graph = (Graph)malloc(sizeof(AdjListGraph));

graph->size = size;

for (int i = 0; i < size; i++) {

graph->vertices[i] = vertices[i];

graph->AdjList[i].vertex = vertices[i];

graph->AdjList[i].firstEdge = NULL;

}

return graph;

}

 

//Print the graph as an adjacency matrix

void printGraph(Graph graph) {

if (graph == NULL) {

puts("Graph is empty");

return;

}

for (int i = 0; i < graph->size; i++) {

printf("|%c|->", graph->vertices[i]);

EdgeNodePtr curr = graph->AdjList[i].firstEdge;

while(curr != NULL) {

printf("|%c|%d|->", curr->vertex, curr->weight);

curr = curr->nextEdge;

}

puts("NULL");

}

}

 

//Delete the graph, i.e. free the memory

Graph deleteGraph(Graph graph) {

if (graph == NULL) {

puts("Graph is empty");

return NULL;

}

for (int i = 0; i < graph->size; i++) {

EdgeNodePtr pre = NULL;

EdgeNodePtr curr = graph->AdjList[i].firstEdge;

while(curr != NULL) {

pre = curr;

curr = curr->nextEdge;

free(pre);

}

}

free(graph);

return NULL;

}

 

//Do NOT MODIFY ANY CODE ABOVE THIS LINE --------------------------------------

 

void addEdge(Graph graph, int u, int v, int w) {

//TODO

 

}

 

//Do NOT MODIFY ANY CODE BELOW THIS LINE --------------------------------------

 

int main() {

//Create the graph with 5 vertices

char vertices[MAX_VERTEX_NUM];

for (int i = 0; i < MAX_VERTEX_NUM; i++)

vertices[i] = 'A' + i;

Graph g1 = createGraph(vertices, 5);

 

//Add edges

int edges[] = {0,1,3, 1,2,2, 1,3,4, 2,4,5, 3,2,5, 4,3,2};

for (int i = 0; i < (int)(sizeof(edges)/sizeof(int)); i += 3)

addEdge(g1, edges[i], edges[i+1], edges[i+2]);

printGraph(g1);

 

//Delete the graph

g1 = deleteGraph(g1);

} //end of main

 
●
Write a function addEdge, based on provided code.
o
The function adds a new edge from u to v of weight w to the graph.
Suppose this is a directed graph, so only add (u, v), not (v, u)
o
o New edge node to be in heap and to be inserted to the end of the list
Use the provided function headers in the starter file. Finish the TODOS.
See below an example screenshot and the expected output.
NOTE: for this assignment you don't need to submit anything as you will write it in Quiz 4.
Online C Compiler - online editor x X
> C
19
onlinegdb.com/online c compiler
SPONSOR Mailchimp - Create stunning brand assets with the help of our Al-driven Creative Assistant. Get started today.
A ▶ Run Ⓒ Debug Stop Share Save Beautify +
main.c
10
113
71 //DO NOT MODIFY ANY CODE ABOVE THIS LINE
77
73./4
74 Assignment 4.4: Graph Representation
D'
- Read the provided code. Do NOT modify any of them.
76
Write a function addEdge, bused on provided code.
77
78
-
The function add a new edge from u to v of weight w to the graph
- Suppose this is a directed graph, so only add (u, v), not (v, u)
New edge node to be in heap and to be inserted to the end of the list
You must use the provided function headers below.
+
79
80
81 7/
82 void addEdge (Graph graph, int u, int v, int w) {
///(XX)
883
✔✔
-
|A|->|B|3|->NULL
|B|->|C|2|->|D|4|->NULL
|C|->|E|5|->NULL
|D|->|C|5|->NULL
E|-|D|2|->NULL
...Program finished with exil code 0
Press ENTER to exil console.
input
Language C
0
J ⠀
Transcribed Image Text:● Write a function addEdge, based on provided code. o The function adds a new edge from u to v of weight w to the graph. Suppose this is a directed graph, so only add (u, v), not (v, u) o o New edge node to be in heap and to be inserted to the end of the list Use the provided function headers in the starter file. Finish the TODOS. See below an example screenshot and the expected output. NOTE: for this assignment you don't need to submit anything as you will write it in Quiz 4. Online C Compiler - online editor x X > C 19 onlinegdb.com/online c compiler SPONSOR Mailchimp - Create stunning brand assets with the help of our Al-driven Creative Assistant. Get started today. A ▶ Run Ⓒ Debug Stop Share Save Beautify + main.c 10 113 71 //DO NOT MODIFY ANY CODE ABOVE THIS LINE 77 73./4 74 Assignment 4.4: Graph Representation D' - Read the provided code. Do NOT modify any of them. 76 Write a function addEdge, bused on provided code. 77 78 - The function add a new edge from u to v of weight w to the graph - Suppose this is a directed graph, so only add (u, v), not (v, u) New edge node to be in heap and to be inserted to the end of the list You must use the provided function headers below. + 79 80 81 7/ 82 void addEdge (Graph graph, int u, int v, int w) { ///(XX) 883 ✔✔ - |A|->|B|3|->NULL |B|->|C|2|->|D|4|->NULL |C|->|E|5|->NULL |D|->|C|5|->NULL E|-|D|2|->NULL ...Program finished with exil code 0 Press ENTER to exil console. input Language C 0 J ⠀
Expert Solution
steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education