Está en la página 1de 4

Problem Statement:

Implement a data structure for graphs that allows modification (insertion,


deletion). It should be possible to store values at edges and nodes. It might be
easiest to use a dictionary of (node, edgelist) to do this.
Solution:
/* Dictionary */

#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct node
{
int num ;
struct node * link ;
} ;

struct node * dic [ 26 ] ;

void addnode(char adjmat[7][7]), int numv, int vertex);


void addedge(char adjmat[7][7]), int numv, int v1, int v2);
void delnode(int vertex);
void deledge( char adjmat[7][7], int numv ,int v1, int v2)
void dispadmat(char adjmat[7][7]), int numv);

void main( )
{
int numv , ch ;
int i ,x , y ;

clrscr( ) ;

while ( 1 )
{
clrscr( ) ;
printf ( "\n\t\tDictionary\n" ) ;
printf ( "\n\t\t1.Add Node to the graph.\n" ) ;
printf ( "\n\t\t2.Add Edge to the graph.\n" ) ;
printf ( "\n\t\t3.Delete Node from the graph.\n" ) ;
printf ( "\n\t\t4.Delete Edge from the graph.\n" ) ;
printf ( "\t\t5.Show Dictionary.\n" ) ;
printf ( "\t\t0.Exit." ) ;
printf ( "\n\n\t\tYour Choice ") ;
scanf ( "%d", &ch ) ;

switch ( ch )
{
case 1 :

printf ( "\nEnter any parent node to be inserted to Graph :


%d" ) ;
scanf("%d",&x);
add (char adjmat[7][7], int numv ,int vertex ) ;
break ;

case 2 :

printf ( "\nEnter new child to be inserted to parent node :


" ) ;
scanf("%d",&y);
addedge(char adjmat[7][7]), int numv, int v1, int v2);
break ;

case 3 :
printf ( "\nEnter parent node to be deleted from Graph :
%d" ) ;
scanf("%d",&p);
delvertex(int vertex);
break ;

case 4 :
printf ( "\nEnter child node to be deleted from Graph : %d"
) ;
scanf("%d",&q);
deledge( char adjmat[7][7], int numv ,int v1, int v2)
break ;

case 5 :

show( ) ;
getch( ) ;
break ;

default :

printf ( "\nWrong Choice" ) ;


}
}
}

/* The function for addition of nodes of undirected graph */


void addnode( char adjmat[7][7], int numv ,int vertex )
{
int i;
struct node * r, * temp = dic [ j ], * q
adjmat[numv][0]= veretex;
adjmat[0][numv]= veretex;
for(i= 1; i<= numv; i++)
{
adjmat[numv][i]= '0';
adjmat[i][numv]= '0';
}

if ( dic [ j ] == NULL || strcmp ( dic [ j ] -> data, numv ) > 0 )


{
r = dic [ j ] ;
dic [ j ] = q ;
q -> link = r ;
return ;
}

else
{
while ( temp != NULL )
{
if ( ( strcmp ( temp -> data, numv ) < 0 ) && ( ( strcmp ( temp
-> link -> data, numv ) > 0 ) ||
temp -> link ==
NULL ) )
{
q -> link = temp -> link ;
temp -> link = q ;
return ;
}
temp = temp -> link ;
}
}

/* The function for addition of edges of undirected graph */


void addedge( char adjmat[7][7], int numv ,int v1, int v2)
{
int i,j,k;
i=0;
for(j-1; j<=numv; j++)
{
if( adjmat[i][j] == v1)
{
for(k=0; k<=numv; k++)
{
if( adjmat[k][0] == v2)
{
adjmat[k][j] = '1';
adjmat[j][k] = '1'; break;
}
}
}

/* The function for deletion of a node of undirected graph*/


void delnode(int vertex)
{ int i;

for(i= 1; i<= numv; i++)


{
if(adjmat[i][0] == v1)
{
adjmat[numv][i]= '0';
adjmat[i][numv]= '0'; break;
}

}
}

/* The function for deletion of edge of undirected graph*/


void deledge( char adjmat[7][7], int numv ,int v1, int v2)
{
int i,j,k;
j=0;
for(i=1; i<=numv; i++)
{
if( adjmat[i][j] == v1)
{
for(k=1; k<=numv; k++)
{
if( adjmat[i][k] == v2)
{

adjmat[i][k] = '0'; break;


}
}
}
}
}

void dispadmat(char adjmat[7][7], int numv)


{
int i,j;
printf("\nThe adj Mat is--\n");
char adjmat[7][7], int numv
for(i=0; i<=numv; i++)
{
for(j=0; j<=numv; j++)
{
printf("%c", adjmat[i][j]);
}
printf("\n");
}
printf("\n\n Enter any key to continue");
getch();
}

/* The function for display of Graph*/


void show( )
{
struct node *n ;
int i, j ;

for ( i = 0 ; i <= 25 ; i++ )


{
n = dic [ i ] ;
while ( n != NULL )
{
printf ( "\n%s\t\t%s", n -> data, n -> m [ 0 ] ) ;
for ( j = 1 ; j < n -> mcount ; j++ )
printf ( "\n\t\t%s", n -> m [ j ] ) ;
n = n -> link ;
}
}
}

También podría gustarte