- /*
- * This is a small program to display the basic operations with adjacent_matrix including:
- * insert_vex,insert_arc,dele_vex,dele_arc and print_matrix.
- * n.comoon
- *
- * Apr.21th.2012
- */
- #include<stdio.h>
- #include<string.h>
- #define ARRAY_SIZE 50
- typedef struct node
- {
- char vex[ARRAY_SIZE];
- int matrix[ARRAY_SIZE][ARRAY_SIZE];
- int vex_num;
- }adj_matrix;
- void insert_vex(adj_matrix * my_matrix)
- {
- /*
- * insert a vex into the matrix
- */
- char ch;
- int i;
- my_matrix->vex_num++;
- printf("please input the char for the vex!\\n");
- getchar();
- scanf("%c",&ch);
- my_matrix->vex[my_matrix->vex_num-1]=ch;
- for(i=0;i<my_matrix->vex_num;i++)
- {
- /*
- * initiate the matrix of this vex
- */
- my_matrix->matrix[i][my_matrix->vex_num]=0;
- my_matrix->matrix[my_matrix->vex_num][i]=0;
- }
- /*
- * this vex is connected with itself
- */
- my_matrix->matrix[my_matrix->vex_num-1][my_matrix->vex_num-1]=1;
- }
- int is_vex_exist(adj_matrix * my_matrix,char ch)
- {
- /*
- * check whether the vex is exited or not
- */
- int i;
- for(i=0;i<strlen(my_matrix->vex);i++)
- {
- if(my_matrix->vex[i]==ch)
- {
- return i;
- }
- }
- return -1;
- }
- void insert_arc(adj_matrix * my_matrix)
- {
- /*
- * insert a arc into the matrix
- */
- char vex_first,vex_second;
- int first_index,second_index;
- printf("please input the first vex!\\n");
- getchar();
- scanf("%c",&vex_first);
- printf("please input the second vex!\\n");
- getchar();
- scanf("%c",&vex_second);
- first_index=is_vex_exist(my_matrix,vex_first);
- second_index=is_vex_exist(my_matrix,vex_second);
- if(first_index==-1 || second_index==-1)
- {
- printf("first or second vex is not existed!\\n");
- return;
- }
- else
- {
- my_matrix->matrix[first_index][second_index]=1;
- }
- }
- void dele_vex(adj_matrix * my_matrix)
- {
- /*
- * delete a vex from the matrix
- */
- int index;
- int i,j;
- char vex_to_del;
- printf("please input the vex you want to delete!\\n");
- getchar();
- scanf("%c",&vex_to_del);
- index=is_vex_exist(my_matrix,vex_to_del);
- if(index==-1)
- {
- printf("the vex you input is not existed!\\n");
- return;
- }
- else
- {
- for(i=index;i<strlen(my_matrix->vex);i++)
- {
- my_matrix->vex[i]=my_matrix->vex[i+1];
- for(j=0;j<index;j++)
- {
- my_matrix->matrix[i][j]=my_matrix->matrix[i+1][j];
- my_matrix->matrix[j][i]=my_matrix->matrix[j][i+1];
- }
- for(j=index;j<strlen(my_matrix->vex);j++)
- {
- my_matrix->matrix[i][j]=my_matrix->matrix[i+1][j+1];
- }
- }
- my_matrix->vex_num--;
- }
- }
- void dele_arc(adj_matrix * my_matrix)
- {
- /*
- * delete a arc from the matrix
- */
- char vex_first,vex_second;
- int first_index,second_index;
- printf("please input the first vex!\\n");
- getchar();
- scanf("%c",&vex_first);
- printf("please input the second vex!\\n");
- getchar();
- scanf("%c",&vex_second);
- first_index=is_vex_exist(my_matrix,vex_first);
- second_index=is_vex_exist(my_matrix,vex_second);
- if(first_index==-1 || second_index==-1)
- {
- printf("first or second vex is not existed!\\n");
- return;
- }
- else
- {
- my_matrix->matrix[first_index][second_index]=0;
- }
- }
- void print_matrix(adj_matrix * my_matrix)
- {
- int i,j;
- for(i=0;i<my_matrix->vex_num;i++)
- {
- printf("%c :",my_matrix->vex[i]);
- for(j=0;j<my_matrix->vex_num;j++)
- {
- if(my_matrix->matrix[i][j]==1)
- {
- printf("->%c ",my_matrix->vex[j]);
- }
- }
- printf("\\n");
- }
- }
- int show_menu()
- {
- int choice;
- printf("1.InsertVex\\n");
- printf("2.InsertArc\\n");
- printf("3.DeleteVex\\n");
- printf("4.DeleteArc\\n");
- printf("5.Show Adjacent Matrix\\n");
- printf("6.Exit\\n");
- scanf("%d",&choice);
- if(choice>0 && choice<7)
- {
- return choice;
- }
- else
- {
- printf("choice error!\\n");
- return;
- }
- }
- int main()
- {
- adj_matrix * my_matrix;
- my_matrix=(adj_matrix *)malloc(sizeof(adj_matrix));
- my_matrix->vex_num=0;
- while(1)
- {
- switch(show_menu())
- {
- case 1:
- insert_vex(my_matrix);
- break;
- case 2:
- insert_arc(my_matrix);
- break;
- case 3:
- dele_vex(my_matrix);
- break;
- case 4:
- dele_arc(my_matrix);
- break;
- case 5:
- print_matrix(my_matrix);
- break;
- case 6:
- exit(1);
- break;
- }
- }
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/260620134293.html
来源: http://www.codesnippet.cn/detail/260620134293.html