[C++] 图的建立 →→→→→进入此内容的聊天室

来自 , 2021-03-13, 写在 C++, 查看 115 次.
URL http://www.code666.cn/view/d0fb963f
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. //表结点
  5. typedef struct ArcNode{
  6. int adjvex;
  7. struct ArcNode *nextarc;
  8. }ArcNode;
  9.  
  10. //头结点
  11. typedef struct Vnode
  12. {
  13.   char data;
  14.   ArcNode *firstarc;
  15. }Vnode;
  16. bool visit[20];
  17.  
  18. //图结构
  19. typedef struct Graph
  20. {
  21.         int point;
  22.         int link;
  23.         Vnode *VeArray;
  24.         int flag;
  25. }AlGraph;
  26.  
  27. //初始化图
  28. void InitGraph(AlGraph *p,int ve,int arc)
  29. {
  30.  p->point=ve;
  31.  p->link=arc;
  32.  p->flag=0;
  33.  p->VeArray=(Vnode *)malloc(sizeof(Vnode)*ve);
  34. }
  35. //用邻接表存储图
  36. void CreateGraph(AlGraph *p)
  37. {
  38.         int i,j;
  39.         char index;
  40.         ArcNode *Q,*S;
  41.         printf("请依次输入各顶点:\n");
  42.         for(i=0;i<p->point;i++)
  43.         {
  44.             getchar();
  45.                 scanf("%c",&index);
  46.                 p->VeArray[i].data=index;
  47.                 p->VeArray[i].firstarc=NULL;
  48.         }
  49.         for(i=0;i<p->point;i++)
  50.         {
  51.             getchar();
  52.                 S=(ArcNode *)malloc(sizeof(ArcNode));
  53.                 printf("依次输入与顶点 %c  相邻的顶点并以#结束:",p->VeArray[i].data);
  54.                 scanf("%c",&index);
  55.                 if(index)
  56.                 {
  57.                 j=0;
  58.             while(index!=p->VeArray[j].data)
  59.                         j++;
  60.                  S->adjvex=j;
  61.          p->VeArray[i].firstarc=S;
  62.                  Q=S;
  63.         }
  64.                 else
  65.                 continue;
  66.                 scanf("%c",&index);
  67.                 while(index!='#')
  68.                 {
  69.                    S=(ArcNode *)malloc(sizeof(ArcNode));
  70.                    j=0;
  71.                    while(index!=p->VeArray[j].data)
  72.                            j++;
  73.                     S->adjvex=j;
  74.                     Q->nextarc=S;
  75.                     Q=S;
  76.                         scanf("%c",&index);
  77.                 }
  78.                 Q->nextarc=NULL;
  79.         }
  80. }
  81. //输出邻接表
  82. void  showGraph(AlGraph *p)
  83. {
  84.         int i;
  85.         ArcNode *temp;
  86.         printf("顶点位置\t顶点名称\t邻接顶点的位置\n");
  87.         for(i=0;i<p->point;i++)
  88.         {
  89.                 printf("%4d\t\t %c  ->\t\t",i,p->VeArray[i].data);
  90.                 temp=p->VeArray[i].firstarc;
  91.                 while(temp)
  92.                 {
  93.                         printf("%d->\t",temp->adjvex);
  94.                         temp=temp->nextarc;
  95.                 }
  96.                 printf("\n");
  97.         }
  98. }
  99.  
  100. void DFS(AlGraph &p,int i)
  101. {
  102.    visit[i]=true;
  103.    printf("%c->",p.VeArray[i].data);
  104.    ArcNode *nextNode=p.VeArray[i].firstarc;
  105.    while(nextNode)
  106. {
  107.     if(!visit[nextNode->adjvex])
  108.         DFS(p,nextNode->adjvex);
  109.         nextNode=nextNode->nextarc;
  110. }
  111. }
  112. void DFStraverse(AlGraph &p)
  113. {
  114.     for(int i=0;i<p.point;i++)
  115.        visit[i]=false;
  116.    for(int i=0;i<p.point;i++)
  117.    {
  118.         if(!visit[i])
  119.           DFS(p,i);
  120.    }
  121. }
  122. int main ()
  123. {
  124.         int ve,arc;
  125.         AlGraph p;
  126.         printf("请输入无向图的顶点数和边数:\n");
  127.         scanf("%d%d",&ve,&arc);
  128.     InitGraph(&p,ve,arc);
  129.         CreateGraph(&p);
  130.         printf("\n无向图的邻接表存储结构如下:\n");
  131.         showGraph(&p);
  132.         printf("深度优先搜索无向图序列如下:\n");
  133.         DFStraverse(p);
  134. }
  135.  

回复 "图的建立"

这儿你可以回复上面这条便签

captcha