数据结构与算法—创建有向图的邻接表

#include <iostream>
using namespace std;
const int MaxVnum=100;  //顶点数最大值

typedef char VexType;  //顶点的数据类型为字符型
typedef struct AdjNode{  //定义邻接点类型
	int v;
	struct AdjNode *next;  //指向下一个邻接点 
}AdjNode;

typedef struct VexNode{  //定义定点类型 
	VexType data;  //VerType为顶点的数据类型,根据需要定义 
	AdjNode *first;  //指向第一个邻接点 
}VerNode;

typedef struct{  //定义邻接表类型
	VexNode Vex[MaxVnum];
	int vexnum,edgenum;  //顶点数,边数 
}ALGraph;

int locatevex(ALGraph G,VexType x)
{
	for(int i=0; i<G.vexnum; i++)
		if(x==G.Vex[i].data)
		return i;
	return -1;  //没找到 
}

void insertedge(ALGraph &G, int i, int j)  //头插法插入一条边 
{
	AdjNode *s;
	s=new AdjNode;
	s->v-j;
	s->next=G.Vex[i].first;
	G.Vex[i].first=s;
}

void print(ALGraph G)  //输出邻接表
{
	cout<<"------邻接表如下------"<<endl;
	for(int i=0; i<G.vexnum; i++)
	{
		AdjNode *t=G.Vex[i].first;
		cout<<cout<<G.Vex[i].data<<":";
		while(t!=NULL)
		{
			cout<<"["<<t->v<<"] ";
			t=t->next;
		}
		cout<<endl;
	} 
} 

void CreateALGraph(ALGraph &G)  //创建有向图邻接表
{
	int i,j;
	VexType u,v;
	cout<<"请输入顶点数和边数:"<<endl;
	cin>>G.vexnum>>G.edgenum;
	cout<<"请输入顶点信息:"<<endl;
	for(i=0; i<G.vexnum; i++)  //输入顶点信息,存入顶点信息数组
		cin>>G.Vex[i].data;
	for(i=0; i<G.vexnum; i++)
		G.Vex[i].first=NULL;
	cout<<"请依次输入每条边的两个顶点u,v"<<endl;
	while(G.edgenum--)
	{
		cin>>u>>v;
		i=locatevex(G,u);  //查找顶点u的存储下标 
		j=locatevex(G,v);  //查找顶点v的存储下标 
		if(i!=-1&&j!=-1)
			insertedge(G,i,j);
		else
		{
			cout<<"输入顶点信息错!请重新输入!"<<endl;
			G.edgenum++;//本次输入不算 
		}
	} 
} 

int main()
{
	ALGraph G;
	CreateALGraph(G);
	print(G);
	return 0;
}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
拓扑排序是对有向无环图(DAG)的所有节点进行排序,使得对于每一条有向边(u,v),在排序后的节点序列中,节点u都排在节点v的前面。下面是用C语言实现基于邻接表存储有向图的拓扑排序的代码: ```c #include <stdio.h> #include <stdlib.h> #define MAXV 1000 // 最大顶点数 typedef struct node{ int v; // 存储的顶点 struct node *next; // 指向下一个节点的指针 }node; node *G[MAXV]; // 存储图的邻接表 int inDegree[MAXV]; // 存储各个顶点的入度 int V, E; // 顶点数和边数 // 向邻接表中添加一条边 void addEdge(int u, int v){ node *newNode = (node*)malloc(sizeof(node)); newNode->v = v; newNode->next = G[u]; G[u] = newNode; } // 用拓扑排序对DAG进行排序 void topologicalSort(){ int i, j; int queue[MAXV], front = 0, rear = -1; // 队列实现 int cnt = 0; // 记录已经输出的顶点数 // 将入度为0的顶点入队 for(i = 0; i < V; i++){ if(inDegree[i] == 0){ queue[++rear] = i; } } while(front <= rear){ int u = queue[front++]; // 取出队首元素 printf("%d ", u); cnt++; // 将所有u指向的顶点的入度减1 for(node *p = G[u]; p != NULL; p = p->next){ int v = p->v; inDegree[v]--; // 如果v的入度为0,将v入队 if(inDegree[v] == 0){ queue[++rear] = v; } } } // 如果输出的顶点数小于V,说明图中有环 if(cnt < V){ printf("Error: Graph contains cycle!\n"); } } int main(){ int i, u, v; scanf("%d%d", &V, &E); // 输入顶点数和边数 // 初始化邻接表和入度数组 for(i = 0; i < V; i++){ G[i] = NULL; inDegree[i] = 0; } // 读入边,建立邻接表和入度数组 for(i = 0; i < E; i++){ scanf("%d%d", &u, &v); addEdge(u, v); inDegree[v]++; } topologicalSort(); // 拓扑排序 return 0; } ``` 在这个程序中,我们使用了邻接表存储图,并且记录了每个顶点的入度。在拓扑排序时,我们使用了队列实现。我们先将入度为0的顶点入队,然后不断从队列中取出元素,输出该元素,并将所有该元素指向的顶点的入度减1。如果减1后该顶点的入度变为0,就将该顶点入队。当队列为空时,如果输出的顶点数小于总顶点数,说明图中存在环。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值