邻接表(c语言)

这篇博客介绍了如何使用C语言构建邻接表数据结构来表示图,并提供了创建和打印邻接表的函数。文章详细展示了邻接表的定义、顶点表和边表的节点结构,以及如何通过前插法建立边表。最后,还给出了完整的代码示例,包括输入顶点和边,以及显示图的邻接表结构。
摘要由CSDN通过智能技术生成
#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;
}

广度优先搜索(Breadth-First Search, BFS)是一种用于遍历或搜索树形和图形数据结构的算法。在C语言中,邻接表是一种常用的数据结构来表示图,它将每个节点的邻接节点存储在一个链表中,这使得BFS特别高效。 以下是使用C语言实现BFS邻接表的基本步骤: 1. 定义图和邻接表结构: ```c typedef struct Node { int data; struct List *adjacent; // 邻接列表 } Node; typedef struct List { Node *head; } List; ``` 2. 初始化邻接表: ```c void initializeAdjacencyList(Node *node, List *list) { node->adjacent = (List*) malloc(sizeof(List)); list->head = NULL; } ``` 3. 添加边(连接两个节点): ```c void addEdge(Node *source, int dest, List **adjacent) { Node *newNode = (Node*) malloc(sizeof(Node)); newNode->data = dest; newNode->adjacent = NULL; if (*adjacent == NULL) { *adjacent = newNode; } else { Node *current = *adjacent; while (current->adjacent != NULL) { current = current->adjacent; } current->adjacent = newNode; } } ``` 4. 广度优先搜索函数(用队列实现): ```c void bfs(Node *root, List **adjacent) { Queue *queue = createQueue(); // 创建队列 Node *current = root; queue_enqueue(queue, current); while (!queue_isEmpty(queue)) { current = queue_dequeue(queue); printf("%d ", current->data); // 输出当前节点 List *temp = current->adjacent; while (temp != NULL) { Node *neighbor = temp->head; queue_enqueue(queue, neighbor); temp = temp->adjacent; } } destroyQueue(queue); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值