邻接表(c语言)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MaxVertices 100
typedef char ElemType; //顶点类型 假定为char 

//边表结点 
typedef struct node{
	int adjvex;  //指向目标结点位置 
	struct node *next; //指向下一条边 
}ArcNode;

// 顶点表 
typedef struct{
	ElemType data; //顶点
	ArcNode * first; //指向边表 
}VerNode;

//邻接表
typedef struct{
	VerNode adjList[MaxVertices];//顶点表
	int n,e;//顶点总数 边数 
}AdjList; 

//返回顶点在顶点表的位置
int weizhi(char v,AdjList l){
	int i=0;
	for(i;i<l.n;i++){
		if(v==l.adjList[i].data){
			return i;
		}
	}
	return -1;
}

//生成邻接表
void CreateGraph(AdjList *L){
	int i,p,q;
	char vi,vj;
	ArcNode *s;
	printf("请输入总顶点数和总边数:");
	scanf("%d %d",&L->n,&L->e);
	getchar(); 
	printf("建立顶点表\n");
	for(i=0;i<L->n;i++){
		printf("请输入第%d个顶点:",i+1);
		scanf("%c",&L->adjList[i].data);
		getchar();
		L->adjList[i].first=NULL;//边表指针初始化为null 
	}

	//前插法建立边表
	printf("建立边表\n");
	for(i=0;i<L->e;i++){
		//有向表  printf("请依次输入起始节点和终点节点:") 	
		/*无向表 */	printf("请输入边的两个的顶点值:");
		scanf("%c %c",&vi,&vj);
		getchar();
		p=weizhi(vi,*L);//返回起点的下标 
		q=weizhi(vj,*L);//返回终点的下标 
		s=(ArcNode *)malloc(sizeof(ArcNode));//创建边表的结点 
		s->adjvex=q;
		s->next=L->adjList[p].first;
		L->adjList[p].first=s;
		/*无向表 则要双向添加s节点 
		s=(ArcNode *)malloc(sizeof(ArcNode));//创建边表的结点 
		s->adjvex=p;
		s->next=L->adjList[q].first;
		L->adjList[q].first=s; */
		
	}
	
} 

//打印
void DispGraph(AdjList L){
	int i;
	for(i=0;i<L.n;i++){
		printf("%c->",L.adjList[i].data);
		while(1){
			if(L.adjList[i].first==NULL){//该顶点的边表指针为空时 
				printf("^"); 
				break;
			}
			//该顶点的边表指针不为空时 
			printf("%d->",L.adjList[i].first->adjvex);//输出顶点所指向的边表结点的内容 
			L.adjList[i].first=L.adjList[i].first->next;//顶点的指针指向当前边节点的next,即指向下一个边节点 
		}
		printf("\n");//一行输出完了换行 
	}
}

int main(){
	AdjList L;
	CreateGraph(&L);
	DispGraph(L);
	return 1;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是邻接表转化为逆邻接表的 C 语言代码示例: ```c #include <stdio.h> #include <stdlib.h> #define MAX_VERTEX_NUM 100 typedef struct ArcNode { int adjvex; // 邻接点在顶点数组中的位置下标 struct ArcNode *nextarc; // 指向下一个邻接点 } ArcNode; typedef struct VNode { int data; // 顶点数据 ArcNode *firstarc; // 指向第一个邻接点 } VNode, AdjList[MAX_VERTEX_NUM]; typedef struct { AdjList vertices; // 存储图的顶点和邻接表 int vexnum, arcnum; // 顶点数和边数 } ALGraph; // 初始化图 void initGraph(ALGraph *G, int n, int m) { G->vexnum = n; G->arcnum = m; for (int i = 0; i < n; i++) { G->vertices[i].data = i; // 顶点编号从0开始 G->vertices[i].firstarc = NULL; } } // 添加边 void addEdge(ALGraph *G, int u, int v) { ArcNode *node = (ArcNode *) malloc(sizeof(ArcNode)); node->adjvex = u; // 注意这里是逆邻接表 node->nextarc = G->vertices[v].firstarc; G->vertices[v].firstarc = node; } // 打印邻接表 void printGraph(ALGraph G) { for (int i = 0; i < G.vexnum; i++) { printf("%d: ", G.vertices[i].data); ArcNode *p = G.vertices[i].firstarc; while (p != NULL) { printf("%d ", p->adjvex); p = p->nextarc; } printf("\n"); } } int main() { int n = 4, m = 5; ALGraph G; initGraph(&G, n, m); addEdge(&G, 0, 1); addEdge(&G, 1, 0); addEdge(&G, 1, 2); addEdge(&G, 2, 0); addEdge(&G, 3, 1); printGraph(G); return 0; } ``` 在上面的代码中,我们通过 `addEdge` 函数向逆邻接表中添加边。最后通过 `printGraph` 函数打印出逆邻接表。注意逆邻接表中的每个节点都存储了一个该节点指向的顶点的下标,这和邻接表中存储的是该节点的邻接点的下标是相反的。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值