Chinaunix首页 | 论坛 | 博客
  • 博客访问: 117832
  • 博文数量: 23
  • 博客积分: 495
  • 博客等级: 下士
  • 技术积分: 252
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-05 20:44
文章分类
文章存档

2011年(23)

分类: C/C++

2011-08-16 22:17:23

针对邻接矩阵的算法的实现:
GtoG2M.c:
  1. /*
  2. * filename : GtoG2M.c
  3. * author : Jesse
  4. * update : 2011/08/15
  5. */

  6. #include <stdio.h>

  7. void GtoG2(int G[5][5], int G2[5][5], int V)
  8. {
  9.     int r, c, c1;
  10.     for(r = 0; r < V; r++)
  11.     {
  12.         for(c = 0; c < V; c++)
  13.             if(G[r][c] == 1)
  14.             {
  15.                 for(c1 = 0; c1 < V; c1++)
  16.                     if(G[c][c1] == 1)    G2[r][c1] = 1;
  17.             }
  18.     }
  19. }

  20. int main(void)
  21. {
  22.     int G[5][5] = {{0, 1, 0, 0, 0}, {0, 0, 1, 1, 0}, {0, 1, 0, 0, 1}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}};
  23.     int G2[5][5] = {{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}};
  24.     int r, c;

  25.     for(r = 0; r < 5; r++)
  26.     {
  27.         for(c = 0; c < 5; c++)
  28.             printf("%d ", G[r][c]);
  29.         printf("\n");
  30.     }
  31.     printf("\n");

  32.     GtoG2(G, G2, 5);

  33.     for(r = 0; r < 5; r++)
  34.     {
  35.         for(c = 0; c < 5; c++)
  36.             printf("%d ", G2[r][c]);
  37.         printf("\n");
  38.     }
  39.     return(0);
  40. }


针对邻接表的算法的实现:
GtoG2L.c :
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef struct node
  4. {
  5.     int data;
  6.     struct node *next;
  7. }VNode;

  8. void GtoG2(VNode G[], VNode G2[], int V)
  9. {
  10.     int i;
  11.     VNode *srcline, *dstline, *dstp, *tmp;
  12.     for(i = 0; i < V; i++)
  13.     {
  14.         srcline = G[i].next;
  15.         while(srcline != NULL)
  16.         {
  17.             dstline = G[srcline->data - 1].next;
  18.             while(dstline != NULL)
  19.             {
  20.                 dstp = G2[i].next;
  21.                 while(dstp != NULL && dstp->data != dstline->data)
  22.                     dstp = dstp->next;
  23.                 if(dstp == NULL)
  24.                 {
  25.                     tmp = G2[i].next;
  26.                     G2[i].next = (VNode *)malloc(sizeof(VNode));
  27.                     G2[i].next->data = dstline->data;
  28.                     G2[i].next->next = tmp;
  29.                 }
  30.                 dstline = dstline->next;
  31.             }
  32.             srcline = srcline->next;
  33.         }
  34.     }
  35. }

  36. void INIT(VNode G[], int V)
  37. {
  38.     int i;    
  39.     for(i = 0; i < V; i++)
  40.         G[i].next = NULL;
  41. }

  42. void INPUT(VNode G[], int V)
  43. {
  44.     int i, data;
  45.     VNode *p;

  46.     printf("please input %d lines data(data > 0) and every line end with -1 :\n", V);
  47.     for(i = 0; i < V; i++)
  48.     {
  49.         scanf("%d", &data);
  50.         while(data != -1)
  51.         {
  52.             p = (VNode *)malloc(sizeof(VNode));
  53.             p->data = data;
  54.             p->next = G[i].next;
  55.             G[i].next = p;
  56.             scanf("%d", &data);
  57.         }
  58.     }
  59. }

  60. void PRINT(VNode G[], int V)
  61. {
  62.     int i;
  63.     VNode *p;
  64.     for(i = 0; i < V; i++)
  65.     {
  66.         printf("%d\t", i+1);
  67.         p = G[i].next;
  68.         while(p != NULL)
  69.         {
  70.             printf("%d ", p->data);
  71.             p = p->next;
  72.         }
  73.         printf("\n");
  74.     }
  75. }

  76. int main(void)
  77. {
  78.     int Vn = 4;
  79.     VNode G[Vn], G2[Vn];

  80.     INIT(G, Vn);
  81.     INIT(G2, Vn);

  82.     INPUT(G, Vn);
  83.     printf("before GtoG2: \n");
  84.     PRINT(G, Vn);

  85.     GtoG2(G, G2, Vn);

  86.     printf("after GtoG2: \n");
  87.     PRINT(G2, Vn);

  88.     return(0);
  89. }




阅读(1220) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~