⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 5.1.c

📁 自己写的数据结构课程程序
💻 C
字号:
#include<stdio.h>
#define MAX_VERTEX_NUM 20
#define NULL 0
enum {FALSE,TRUE}VisitType;
struct ArcNode
{int adjvex;
 struct ArcNode* nextarc;
};
struct VNode
{char data;
 struct ArcNode* firstarc;
};
struct ALGraph
{struct VNode vertices[MAX_VERTEX_NUM+1];
 int vexnum,arcnum;
 int kind;
};
struct ALGraph G;
enum VisitType *visited;
void CreatALGraph()
{int i,v;
 struct ArcNode *p,**q;
 printf("please input the number of vertexs:\n");
 scanf("%d",&G.vexnum);
 getchar();
 visited=(enum VisitType*)malloc(G.vexnum*sizeof(enum VisitType));
 for(i=1;i<=G.vexnum;i++)
   G.vertices[i].firstarc=NULL;
 printf("please input the data of every vertex first:\n");
 for(i=1;i<=G.vexnum;i++)
   scanf("%c",&G.vertices[i].data);
 getchar();
 printf("now input the neighbors:\n");
 for(i=1;i<=G.vexnum;i++)
   {printf("please input the neighbors of %d\n",i);
    q=&G.vertices[i].firstarc;
    while(1)
      {scanf("%d",&v);
       if(v==0)
         break;
       else
         {p=(struct ArcNode *)malloc(sizeof(struct ArcNode));
          p->adjvex=v;
          p->nextarc=NULL;
          *q=p;
          q=&p->nextarc;
         }
      }

   }
 printf("success!\n");
}
void Visit(int i)
{printf("%c",G.vertices[i].data);
}
int FirstAdjVex(int v)
{return G.vertices[v].firstarc->adjvex;
}
int NextAdjVex(int v,int w)
{int n;
 struct ArcNode *p;
 p=G.vertices[v].firstarc;
 n=p->adjvex;
 while(n!=w&&p!=NULL)
   {p=p->nextarc;
    n=p->adjvex;
   }
 if(p==NULL)
   return -1;
 else
   if(p->nextarc==NULL)
     return -1;
   else
     return p->nextarc->adjvex;
}
void DFS(int v)
{int w;
 visited[v]=TRUE;
 Visit(v);
 for(w=FirstAdjVex(v);w>=1;w=NextAdjVex(v,w))
   if(!visited[w])
     DFS(w);
}
void DFSTraverse()
{int v;
 for(v=1;v<=G.vexnum;++v)
   visited[v]=FALSE;
 for(v=1;v<=G.vexnum;++v)
   if(!visited[v])
     DFS(v);
}
void main(void)
{CreatALGraph();
 DFSTraverse();
 getchar();
 getchar();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -