📄 linjiebiaotu.cpp
字号:
// linjiebiaotu.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#define MAX 20
typedef struct node
{
int adjvertex;
struct node* adjnext;
}adjnode;
typedef struct vnode
{
int vertex;
adjnode* firstadj;
}vertexnode;
typedef vertexnode adjlist[MAX];
typedef struct
{
adjlist adj;
int n,e;
}algraph;
int visited[MAX];
void creategraph(algraph *graph)
{
adjnode* p;
int v1,v2;
printf("\n输入图的顶点:\n");
for (int i=0;i<graph->n;i++)
{
scanf("%d",&graph->adj[i].vertex);
graph->adj[i].firstadj=NULL;
}
printf("\n输入图的邻接边:\n");
for (i=0;i<graph->e;i++)
{
scanf("%d%d",&v1,&v2);
p=(adjnode *)malloc(sizeof(adjnode));
p->adjvertex=v2;
p->adjnext=graph->adj[v1].firstadj;
graph->adj[v1].firstadj=p;
p=(adjnode *)malloc(sizeof(adjnode));
p->adjvertex=v1;
p->adjnext=graph->adj[v2].firstadj;
graph->adj[v2].firstadj=p;
}
}
void shougraph(algraph *graph)
{
adjnode* p;
for (int i=0;i<graph->n;i++)
{
printf("\n顶点%d的值%d",i,graph->adj[i].vertex);
p=graph->adj[i].firstadj;
while(p!=NULL)
{
printf("==>%d",p->adjvertex);
p=p->adjnext;
}
}
}
void dfs(algraph *graph,int m,int visited[])
{
adjnode* p;
printf("%5d",graph->adj[m].vertex);
visited[m]=1;
p=graph->adj[m].firstadj;
while (p!=NULL)
{
if(visited[p->adjvertex]==0)dfs(graph,p->adjvertex,visited);
p=p->adjnext;
}
}
int main(int argc, char* argv[])
{ algraph g;
int m;
printf("\n输入图的接点数n和边数e:\n");
scanf("%d%d",&g.n,&g.e);
creategraph(&g);
shougraph(&g);
for (int i=0;i<MAX;i++)visited[i]=0;
printf("\n输入遍历开始的结点:");
scanf("%d",&m);
dfs(&g,m,visited);
printf("\n");
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -