ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
printf("\nThe vertices in the graph are : ");
for(element = list_head(list); element != NULL; element = list_next(element))
printf("%d ", list_data(element));
}
int main()
{
int choice, num, sour, dest, flag = 0;
Graph *graph;
printf("\n\n\n*****IMPLEMENTATION OF GRAPHS***\n");
start: printf("\n1. Initialize a graph");
printf("\n2. Insert a vertex in the graph");
printf("\n3. Insert an edge in the graph");
printf("\n4. Delete a vertex from the graph");
printf("\n5. Delete an edge from the graph");
printf("\n6. Display the graph");
printf("\n7. Destroy the graph");
printf("\n8. Exit");
printf("\n Enter your choice : ");
choice: scanf("%d", &choice);
if(choice == 8)
return -1;
if (choice > 8)
{
printf("No such choice. Enter choice again : ");
goto choice;
}
if(choice != 1 && flag == 0)
{
printf("Unless a graph is created, this operation is invalid.\n");
goto start;
}
switch(choice)
{
case 1:
if (flag == 1)
{
printf("\nA graph is in use. Destroy it before initialing a new graph.\n");
goto start;
}
printf("\nEnter the vertex to be inserted in the graph : ");
scanf("%d", &num);
if (graph_ins_vertex(graph, num) == 1)
printf("The vertex already exists.\n");
else
printf("The new vertex is inserted in the graph.\n");
goto start;
case 3:
printf("\nEnter the source vertex : ");
scanf("%d", &sour);
printf("Enter the terminating vertex : ");
scanf("%d", &dest);
if (graph_ins_edge(graph, sour, dest) == -1)
printf("Either one of the vertices may be wrong or the edge already exists.\n");
else
printf("The new edge is inserted in the graph.\n");
goto start;
case 4:
printf("\nEnter the vertex to be removed : ");
scanf("%d", &num);
if (graph_rem_vertex(graph, &num) == 0)
printf("The vertex is removed.\n");
else
printf("Either edges incident to/from the vertex may not be deleted or it is not present.\n");
if (graph->vcount == 0)
{
flag = 0;
printf("The graph is empty. The graph is destroyed.\n");
graph_destroy(graph);
}
goto start;
case 5:
printf("\nEnter the source vertex of the edge to be removed : ");
scanf("%d", &sour);
printf("Enter the terminating vertex of the edge to be removed : ");
scanf("%d", &dest);
if (graph_rem_edge(graph, sour, &dest) == 0)
printf("The edge is removed.\n");
else
printf("Either one of the vertices may be wrong or the edge does not exist.\n");
goto start;
case 6:
printf("\nThe graph is decribed as follows :");
list_display(&graph_adjlists(graph));
goto start;
case 7:
graph_destroy(graph);
printf("\nThe graph is destroyed.\n");
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.