8_2_3.C
上传用户:wyn840322
上传日期:2007-01-13
资源大小:294k
文件大小:5k
源码类别:

数据结构

开发平台:

C/C++

  1. /* ======================================== */
  2. /*    程式实例: 8_2_3.c                     */
  3. /*    图形的多重邻接链表表示法              */
  4. /* ======================================== */
  5. #include <stdlib.h>
  6. struct edge                       /* 图形边线结构宣告     */
  7. {
  8.    int vertex1;                   /* 顶点1资料            */
  9.    int vertex2;                   /* 顶点2资料            */
  10.    struct edge *edge1;            /* 顶点1下一边线        */
  11.    struct edge *edge2;            /* 顶点2下一边线        */
  12. };
  13. typedef struct edge *nextedge;    /* 图形的边线新型态     */
  14. struct node                       /* 图形顶点结构宣告     */
  15. {
  16.    int vertex;                    /* 顶点资料             */
  17.    struct edge *edge;             /* 顶点下一边线         */
  18. };
  19. typedef struct node *graph;       /* 图形的结构新型态     */
  20. struct node head[6];              /* 图形顶点结构数组     */
  21. /* ---------------------------------------- */
  22. /*  建立图形                                */
  23. /* ---------------------------------------- */
  24. void creategraph(int *node,int num)
  25. {
  26.    nextedge newnode;              /* 新边线指标           */
  27.    nextedge previous;             /* 前一边线指标         */
  28.    nextedge ptr;                  /* 目前边线指标         */
  29.    int from;                      /* 边线的起点           */
  30.    int to;                        /* 边线的终点           */
  31.    int i;
  32.    for ( i = 0; i < num; i++ )    /* 读取边线的回路       */
  33.    {
  34.       from = node[i*2];           /* 边线的起点           */
  35.       to = node[i*2+1];           /* 边线的终点           */
  36.       /* 建立新边线记忆体 */
  37.       newnode = ( nextedge ) malloc(sizeof(struct edge));
  38.       newnode->vertex1 = from;    /* 建立顶点内容         */
  39.       newnode->vertex2 = to;      /* 建立顶点内容         */
  40.       newnode->edge1 = NULL;      /* 设定指标初值         */
  41.       newnode->edge2 = NULL;      /* 设定指标初值         */
  42.       previous = NULL;            /* 前一边线指标         */
  43.       ptr = head[from].edge;      /* 目前边线指标         */
  44.       while ( ptr != NULL )       /* 遍历到最后边线       */
  45.       {
  46.          previous = ptr;          /* 保留前一边线         */
  47.          if ( ptr->vertex1 == from )  /* 决定走的边线     */
  48.             ptr = ptr->edge1;     /* 下一边线             */
  49.          else
  50.             ptr = ptr->edge2;     /* 下一边线             */
  51.       }
  52.       if ( previous == NULL )
  53.          head[from].edge = newnode;   /* 设定顶点边线指标 */
  54.       else
  55.          if ( previous->vertex1 == from ) /* 决定走的边线 */
  56.             previous->edge1 = newnode;    /* 连接下一边线 */
  57.          else
  58.             previous->edge2 = newnode;    /* 连接下一边线 */
  59.       previous = NULL;                    /* 前一边线指标 */
  60.       ptr = head[to].edge;                /* 目前边线指标 */
  61.       while ( ptr != NULL )       /* 遍历到最后边线       */
  62.       {
  63.          previous = ptr;          /* 保留前一边线         */
  64.          if ( ptr->vertex1 == to )  /* 决定走的边线       */
  65.             ptr = ptr->edge1;     /* 下一边线             */
  66.          else
  67.             ptr = ptr->edge2;     /* 下一边线             */
  68.       }
  69.       if ( previous == NULL )
  70.          head[to].edge = newnode; /* 设定顶点边线指标     */
  71.       else
  72.          if ( previous->vertex1 == to ) /* 决定走的边线   */
  73.             previous->edge1 = newnode;  /* 连接下一边线   */
  74.          else
  75.             previous->edge2 = newnode;  /* 连接下一边线   */
  76.    }
  77. }
  78. /* ---------------------------------------- */
  79. /*  主程式: 建立图形后,将边线列印出.        */
  80. /* ---------------------------------------- */
  81. void main()
  82. {
  83.    nextedge ptr;
  84.    int node[6][2] = {  {1, 2},    /* 边线数组             */
  85.                        {1, 3},
  86.                        {2, 3},
  87.                        {2, 4},
  88.                        {3, 5},
  89.                        {4, 5}, };
  90.    int i;
  91.    for ( i = 1; i <= 5; i++ )
  92.    {
  93.       head[i].vertex = i;         /* 设定顶点值           */
  94.       head[i].edge = NULL;        /* 清除图形指标         */
  95.    }
  96.    creategraph(node,6);           /* 建立图形             */
  97.    printf("图形的多重邻接链表内容:n");
  98.    for ( i = 1; i <= 5; i++ )
  99.    {
  100.       printf("顶点%d =>",head[i].vertex); /* 顶点值       */
  101.       ptr = head[i].edge;                 /* 边线位置     */
  102.       while ( ptr != NULL )       /* 遍历至链表尾         */
  103.       {
  104.          /* 印出边线 */
  105.          printf("(%d,%d)",ptr->vertex1,ptr->vertex2);
  106.          /* 决定下一边线指标 */
  107.          if ( head[i].vertex == ptr->vertex1 )
  108.             ptr = ptr->edge1;     /* 下一个边线           */
  109.          else
  110.             ptr = ptr->edge2;     /* 下一个边线           */
  111.       }
  112.       printf("n");               /* 换行                 */
  113.    }
  114. }