关于插入和删除图的节点和边的问题 [英] Question on inserting and deleting nodes and edges of graph

查看:83
本文介绍了关于插入和删除图的节点和边的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友和导游我有另一个问题,即使用邻接矩阵在无向图中插入和删除节点和边的C程序。

我已经搞砸了这个我得到了一些有很多错误的程序。但我得到了一个比他们更好但仍然没有运行的。请通过纠正和纠正来帮助我。





Hi friends and guiders I have another problem which is a C program to insert and delete nodes and edges in an undirected graph using adjacency matrix.
I have goggled this i got some programs which had many errors. But i got following one which is better than them but still does not run. please help me by rectifying and correcting it.


#include<stdio.h>
#define max 20
int adj[ max ][ max ];
int n;
main()
{
    int choice;
    int node, origin, destin;
    create_graph();
    while ( 1 )
        {
            printf( "1.Insert a node\n" );
            printf( "2.Insert an edge\n" );
            printf( "3.Delete a node\n" );
            printf( "4.Delete an edge\n" );
            printf( "5.Dispaly\n" );
            printf( "6.Exit\n" );
            printf( "Enter your choice : " );
            scanf( "%d", &choice );
            switch ( choice )
                {
                case 1:
                    insert_node();
                    break;
                case 2:
                    printf( "Enter an edge to be inserted : " );
                    fflush( stdin );
                    scanf( "%d %d", &origin, &destin );
                    insert_edge( origin, destin );
                    break;
                case 3:
                    printf( "Enter a node to be deleted : " );
                    fflush( stdin );
                    scanf( "%d", &node );
                    delete_node( node );
                    break;
                case 4:
                    printf( "Enter an edge to be deleted : " );
                    fflush( stdin );
                    scanf( "%d %d", &origin, &destin );
                    del_edge( origin, destin );
                    break;
                case 5:
                    display();
                    break;
                case 6:
                    exit();
                default:
                    printf( "Wrong choice\n" );
                    break;
                } /*End of switch*/
        } /*End of while*/
} /*End of main()*/
create_graph()
{
    int i, max_edges, origin, destin;
    printf( "Enter number of nodes : " );
    scanf( "%d", &n );
    max_edges = n * ( n – 1 ); /* Taking directed graph */
    for ( i = 1;i <= max_edges;i++ )
        {
            printf( "Enter edge %d( 0 0 ) to quit : ", i );
            scanf( "%d %d", &origin, &destin );
            if ( ( origin == 0 ) && ( destin == 0 ) )
                break;
            if ( origin > n || destin > n || origin <= 0 || destin <= 0 )
                {
                    printf( "Invalid edge!\n" );
                    i–;
                }
            else
                adj[ origin ][ destin ] = 1;
        } /*End of for*/
} /*End of create_graph()*/
display()
{
    int i, j;
    for ( i = 1;i <= n;i++ )
        {
            for ( j = 1;j <= n;j++ )
                printf( "%4d", adj[ i ][ j ] );
            printf( "\n" );
        }
} /*End of display()*/
insert_node()
{
    int i;
    n++; /*Increase number of nodes in the graph*/
    printf( "The inserted node is %d \n", n );
    for ( i = 1;i <= n;i++ )
        {
            adj[ i ][ n ] = 0;
            adj[ n ][ i ] = 0;
        }
} /*End of insert_node()*/
delete_node( char u )
{
    int i, j;
    if ( n == 0 )
        {
            printf( "Graph is empty\n" );
            return ;
        }
    if ( u > n )
        {
            printf( "This node is not present in the graph\n" );
            return ;
        }
    for ( i = u;i <= n – 1;i++ )
        for ( j = 1;j <= n;j++ )
            {
                adj[ j ][ i ] = adj[ j ][ i + 1 ]; /* Shift columns left */
                adj[ i ][ j ] = adj[ i + 1 ][ j ]; /* Shift rows up */
            }
    n–; /*Decrease the number of nodes in the graph */
} /*End of delete_node*/
insert_edge( char u, char v )
{
    if ( u > n )
        {
            printf( "Source node does not exist\n" );
            return ;
        }
    if ( v > n )
        {
            printf( "Destination node does not exist\n" );
            return ;
        }
    adj[ u ][ v ] = 1;
} /*End of insert_edge()*/
del_edge( char u, char v )
{
    if ( u > n || v > n || adj[ u ][ v ] == 0 )
        {
            printf( "This edge does not exist\n" );
            return ;
        }
    adj[ u ][ v ] = 0;
} /*End of del_edge()*/

推荐答案

您好,在您的程序中,我可以看到更多基本错误,您也可以解决它。这是一个错误列表,请修复它。



1)请将返回类型提供给所有用户定义的函数。

2)声明函数在main之前或者把你的函数定义为主函数。

3)把adj [] []

的声明放入4)在delete_node函数中make n--而不是n-

5)在显示功能中,n是否是deaclare?

6)你没有包含exit()函数的头文件
Hi, In your Program i can see more basic bugs that you can also slove it. here is a bug list please fix it.

1) Please give return type to all your user defined function.
2) Declare function before the main or put your function defination befor the main function.
3) Put the declaration for adj[][]
4) In delete_node function make n-- instead of n-
5) In display function were is n is deaclare ?
6) You have not included header file for exit() function


这篇关于关于插入和删除图的节点和边的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆