图的建立,输出,深度优先遍历,广度优先遍历,度的输出

#include <stdio.h>
#include <malloc.h>
#include <string.h>
using namespace std;
#define maxven 20
bool visited[100]={0};
int in[100]={0},out[100]={0},total[100];
typedef struct ArcNode
{	int adjvex;
	struct ArcNode *next;
} ArcNode;
typedef struct VNode
{	int data;
	ArcNode *firstarc;
} VNode;
typedef struct
{	int n,e;
	VNode adjlist[maxven];
}Graph;
void Dispgra(Graph G)
{
	int i;
	ArcNode *p;
	for (i=0;i<G.n;i++)
	{
	    printf("%d:",G.adjlist[i].data);
		p=G.adjlist[i].firstarc;
		while (p!=NULL)
		{	printf("%d ",p->adjvex);
			p=p->next;
		}
		printf("\n");
	}
}
void DFS(Graph G,int x)
{
    int i,j;
    printf("v%d ",x+1);
    visited[x]=1;
    ArcNode *p;
    p=G.adjlist[x].firstarc;
    while(p)
    {
        if(!visited[p->adjvex-1])
            DFS(G,p->adjvex-1);
        p=p->next;
    }
}
void BFS(Graph G,int x)
{
    int bf[maxven],i,v;
    int front=0,rear=0;
    printf("v%d ",x+1);
    rear=(rear+1)%maxven;
    bf[rear]=x;visited[x]=1;
    ArcNode *p;
    while(rear!=front)
    {
        front=(front+1)%maxven;
        v=bf[front];
        p=G.adjlist[v].firstarc;
        while(p)
        {
            if(!visited[p->adjvex-1])
        {
             printf("v%d ",p->adjvex);
             visited[p->adjvex-1]=1;
             rear=(rear+1)%maxven;
             bf[rear]=p->adjvex-1;
        }
           p=p->next;
        }
    }
}
void buildgraph( Graph &G)
{
    int i,j,x,y;
    ArcNode *p,*q;
    scanf("%d%d",&G.n,&G.e);
    for (i=0;i<G.n;i++)
   {
        scanf("%d",&G.adjlist[i].data);
        G.adjlist[i].firstarc=NULL;
   }
   for(i=0;i<G.e;i++)
   {
       scanf("%d%d",&x,&y);
       out[x-1]++;in[y-1]++;
       p=(ArcNode *)malloc(sizeof(ArcNode));
       p->adjvex=y;
       p->next=G.adjlist[x-1].firstarc;
       G.adjlist[x-1].firstarc=p;
       q=(ArcNode *)malloc(sizeof(ArcNode));
       q->adjvex=x;
       q->next=G.adjlist[y-1].firstarc;
       G.adjlist[y-1].firstarc=q;
   }
   for(i=0;i<G.n;i++)
    total[i]=out[i]+in[i];
}
void outdu(int in[],int out[],int total[],int n)
{
    int i;
    for(i=0;i<n;i++)
    printf("%d:%d %d %d\n",i+1,in[i],out[i],total[i]);
}
int main()
{
    int i,j,x,y;
     Graph G;
    //buildgraph(G);
	//Dispgra(G);
	//DFS(G,0);
	//BFS(G,0);
	//outdu(in,out,total,G.n);
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值