图的邻接表存储以及相关操作 C语言

/****************************************/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
/****************************************/
#define	MaxVex	255
#define	TRUE	1
#define	FALSE	0
typedef char	VertexType;		//顶点类型
//typedef int		EdgeType;	//权值类型
typedef int		Bool;
Bool visited[MaxVex];

typedef struct EdgeNode	{		//边表结点
	int				adjvex;		//该邻接点在顶点数组中的下标
//	EdgeType		weight;		//权值域
	struct EdgeNode	*next;		//链域 指向下一个邻接点
}EdgeNode;

typedef struct VertexNode {
	VertexType	data;
	EdgeNode	*firstedge;		//边表头指针
}VertexNode, AdjList[MaxVex];

typedef struct {
	AdjList		adjList;
	int	numVertexes, numEdges;		//图中当前的结点数以及边数
}GraphAdjList;
/****************************************/
//广度优先遍历需要的循环队列
typedef struct {
	int	data[MaxVex];
	int	front, rear;
}Queue;
/****************************************/
//队列的相关操作

//初始化
void InitQueue(Queue *Q)
{
	Q->front = Q->rear = 0;
}

//入队
void EnQueue(Queue *Q, int e)
{
	if ((Q->rear+1)%MaxVex == Q->front)
		return ;

	Q->data[Q->rear] = e;
	Q->rear = (Q->rear+1)%MaxVex;
}

//判空
Bool QueueEmpty(Queue *Q)
{
	if (Q->front == Q->rear)
		return TRUE;
	else
		return FALSE;
}

//出队
void DeQueue(Queue *Q, int *e)
{
	if (Q->front == Q->rear)
		return ;
	
	*e = Q->data[Q->front];
	Q->front = (Q->front+1)%MaxVex;
}
/****************************************/
//建立图的邻接表结构
void CreateALGraph(GraphAdjList *G)
{
	int i, j, k;
//	EdgType w;
	EdgeNode *p;
	printf("输入图的结点数以及边数:");
	scanf("%d%d", &G->numVertexes, &G->numEdges);
	fflush(stdin);
	
	printf("输入各个顶点:");
	for (i=0; i<G->numVertexes; ++i)
	{
		scanf("%c", &G->adjList[i].data);
		G->adjList[i].firstedge = NULL;
		fflush(stdin);
	}

	for (k=0; k<G->numEdges; ++k)
	{
		printf("输入(vi, vj)上的顶点序号:");
		scanf("%d%d", &i, &j);
		p = (EdgeNode *)malloc(sizeof(EdgeNode));
		p->adjvex = j;
		p->next = G->adjList[i].firstedge;
		G->adjList[i].firstedge = p;

		p = (EdgeNode *)malloc(sizeof(EdgeNode));
		p->adjvex = i;
		p->next = G->adjList[j].firstedge;
		G->adjList[j].firstedge = p;
	}
}
/****************************************/
//深度优先遍历
void DFS(GraphAdjList G, int i)
{
	EdgeNode *p;
	visited[i] = TRUE;
	printf("%c ", G.adjList[i].data);
	p = G.adjList[i].firstedge;

	while (p)
	{
		if (!visited[p->adjvex])
			DFS(G, p->adjvex);
		p = p->next;
	}
}


void DFSTraverse(GraphAdjList G)
{
	int i;
	for (i=0; i<G.numVertexes; ++i)
		visited[i] = FALSE;

	for (i=0; i<G.numVertexes; ++i)
		if (!visited[i])
			DFS(G, i);
}
/****************************************/
//图的广度优先遍历
void BFSTraverse(GraphAdjList *GL)
{
	int i;
	Queue Q;
	EdgeNode *p;

	for (i=0; i<GL->numVertexes; ++i)
		visited[i] = FALSE;
	InitQueue(&Q);

	for (i=0; i<GL->numVertexes; ++i)
	{
		if (!visited[i])
		{
			visited[i] = TRUE;
			printf("%c ", GL->adjList[i].data);
			EnQueue(&Q, i);

			while (!QueueEmpty(&Q))
			{
				DeQueue(&Q, &i);
				p = GL->adjList[i].firstedge;
				while (p)
				{
					if (!visited[p->adjvex])
					{
						visited[p->adjvex] = TRUE;
						printf("%c ", GL->adjList[p->adjvex].data);
						EnQueue(&Q, p->adjvex);
					}
					p = p->next;
				}
			}
		}
	}
}
/****************************************/
int main(void)
{
	GraphAdjList G;
	CreateALGraph(&G);
	BFSTraverse(&G);
	return 0;
}


 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用邻接表储存无向C语言代码示例: ```c #include <stdio.h> #include <stdlib.h> // 邻接表节点结构体 typedef struct node { int dest; // 目标顶点编号 struct node *next; // 下一个邻接节点指针 } Node; // 邻接表结构体 typedef struct adj_list { Node *head; // 邻接表头指针 } AdjList; // 结构体 typedef struct graph { int num_vertices; // 顶点数量 AdjList *adj_lists; // 邻接表指针数组 } Graph; // 创建邻接表节点 Node* create_node(int dest) { Node* node = (Node*) malloc(sizeof(Node)); node->dest = dest; node->next = NULL; return node; } // 创建 Graph* create_graph(int num_vertices) { Graph* graph = (Graph*) malloc(sizeof(Graph)); graph->num_vertices = num_vertices; graph->adj_lists = (AdjList*) malloc(num_vertices * sizeof(AdjList)); for (int i = 0; i < num_vertices; i++) { graph->adj_lists[i].head = NULL; } return graph; } // 添加边 void add_edge(Graph* graph, int src, int dest) { // 添加 src -> dest Node* node = create_node(dest); node->next = graph->adj_lists[src].head; graph->adj_lists[src].head = node; // 添加 dest -> src node = create_node(src); node->next = graph->adj_lists[dest].head; graph->adj_lists[dest].head = node; } // 打印邻接表 void print_graph(Graph* graph) { for (int i = 0; i < graph->num_vertices; i++) { Node* node = graph->adj_lists[i].head; printf("Vertex %d: ", i); while (node != NULL) { printf("%d -> ", node->dest); node = node->next; } printf("NULL\n"); } } int main() { // 创建一个包含4个顶点的无向 Graph* graph = create_graph(4); add_edge(graph, 0, 1); add_edge(graph, 0, 2); add_edge(graph, 1, 2); add_edge(graph, 2, 3); // 打印邻接表 print_graph(graph); return 0; } ``` 这段代码会创建一个包含4个顶点的无向,然后添加4条边,并打印邻接表。你可以根据需要修改顶点数量和边的数量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值