用c语言生成一个邻接表的结构体
时间: 2023-04-03 15:04:57 浏览: 96
好的,我可以回答这个问题。邻接表是一种表示图的数据结构,可以用来存储图中的节点和边。在 C 语言中,可以通过结构体来定义邻接表。一个基本的邻接表结构体包含两个成员:一个指向节点的指针数组和一个指向边的指针数组。节点指针数组中的每个元素指向一个节点,边指针数组中的每个元素指向一个边。节点和边的定义可以根据具体的需求来设计。
相关问题
用C语言编程实现邻接表生成逆邻接表。
好的,以下是实现邻接表生成逆邻接表的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 CreateGraph(ALGraph *G) {
int i, j, k;
ArcNode *p;
printf("请输入顶点数和边数:\n");
scanf("%d%d", &G->vexnum, &G->arcnum);
printf("请输入顶点信息:\n");
for (i = 0; i < G->vexnum; i++) {
scanf("%d", &G->vertices[i].data);
G->vertices[i].firstarc = NULL;
}
printf("请输入边的信息(起点 终点):\n");
for (k = 0; k < G->arcnum; k++) {
scanf("%d%d", &i, &j);
p = (ArcNode *)malloc(sizeof(ArcNode));
p->adjvex = j;
p->nextarc = G->vertices[i].firstarc;
G->vertices[i].firstarc = p;
}
}
// 生成逆邻接表
void Inverse(ALGraph *G, ALGraph *GR) {
int i;
ArcNode *p;
GR->vexnum = G->vexnum;
GR->arcnum = G->arcnum;
for (i = 0; i < G->vexnum; i++) {
GR->vertices[i].data = G->vertices[i].data;
GR->vertices[i].firstarc = NULL;
}
for (i = 0; i < G->vexnum; i++) {
p = G->vertices[i].firstarc;
while (p != NULL) {
ArcNode *q = (ArcNode *)malloc(sizeof(ArcNode));
q->adjvex = i;
q->nextarc = GR->vertices[p->adjvex].firstarc;
GR->vertices[p->adjvex].firstarc = q;
p = p->nextarc;
}
}
}
// 主函数测试逆邻接表生成
int main() {
ALGraph G, GR;
CreateGraph(&G);
Inverse(&G, &GR);
printf("邻接表:\n");
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 -> ", G.vertices[p->adjvex].data);
p = p->nextarc;
}
printf("NULL\n");
}
printf("逆邻接表:\n");
for (int i = 0; i < GR.vexnum; i++) {
printf("%d -> ", GR.vertices[i].data);
ArcNode *p = GR.vertices[i].firstarc;
while (p != NULL) {
printf("%d -> ", GR.vertices[p->adjvex].data);
p = p->nextarc;
}
printf("NULL\n");
}
return 0;
}
```
在这个程序中,我们首先定义了邻接表的结构体,其中`VNode`表示一个顶点,`ArcNode`表示一个邻接点,`AdjList`是邻接表的类型,用于存储所有顶点的信息和它们的邻接点信息。然后,我们实现了两个函数来生成邻接表和逆邻接表。最后,在主函数中,我们使用`CreateGraph`函数生成邻接表,并使用`Inverse`函数生成逆邻接表,然后分别输出它们的信息。
注意,这里的逆邻接表是由原来的邻接表生成的,因此逆邻接表的顶点信息和边数与原来的邻接表相同。不同之处在于,对于每个顶点,逆邻接表中存储的是指向该顶点的所有边,而不是该顶点指向的所有边。
用C语言编写普利姆算法邻接表的代码
下面是用C语言编写普利姆算法邻接表的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define MAX_VERTICES 100
typedef struct node {
int vertex;
int weight;
struct node* next;
} Node;
typedef struct graph {
Node* vertices[MAX_VERTICES];
int num_vertices;
} Graph;
typedef struct heap {
int* vertex_list;
int* weight_list;
int size;
int capacity;
} Heap;
Graph* createGraph(int num_vertices) {
Graph* graph = (Graph*)malloc(sizeof(Graph));
graph->num_vertices = num_vertices;
for (int i = 0; i < num_vertices; i++) {
graph->vertices[i] = NULL;
}
return graph;
}
void addEdge(Graph* graph, int src, int dest, int weight) {
Node* node = (Node*)malloc(sizeof(Node));
node->vertex = dest;
node->weight = weight;
node->next = graph->vertices[src];
graph->vertices[src] = node;
node = (Node*)malloc(sizeof(Node));
node->vertex = src;
node->weight = weight;
node->next = graph->vertices[dest];
graph->vertices[dest] = node;
}
Heap* createHeap(int capacity) {
Heap* heap = (Heap*)malloc(sizeof(Heap));
heap->vertex_list = (int*)malloc(sizeof(int) * capacity);
heap->weight_list = (int*)malloc(sizeof(int) * capacity);
heap->size = 0;
heap->capacity = capacity;
return heap;
}
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
void heapify(Heap* heap, int i) {
int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < heap->size && heap->weight_list[left] < heap->weight_list[smallest]) {
smallest = left;
}
if (right < heap->size && heap->weight_list[right] < heap->weight_list[smallest]) {
smallest = right;
}
if (smallest != i) {
swap(&heap->vertex_list[smallest], &heap->vertex_list[i]);
swap(&heap->weight_list[smallest], &heap->weight_list[i]);
heapify(heap, smallest);
}
}
void insert(Heap* heap, int vertex, int weight) {
if (heap->size == heap->capacity) {
return;
}
heap->vertex_list[heap->size] = vertex;
heap->weight_list[heap->size] = weight;
int i = heap->size;
while (i > 0 && heap->weight_list[i] < heap->weight_list[(i - 1) / 2]) {
swap(&heap->vertex_list[i], &heap->vertex_list[(i - 1) / 2]);
swap(&heap->weight_list[i], &heap->weight_list[(i - 1) / 2]);
i = (i - 1) / 2;
}
heap->size++;
}
int extractMin(Heap* heap) {
if (heap->size == 0) {
return -1;
}
int min_vertex = heap->vertex_list[0];
heap->vertex_list[0] = heap->vertex_list[heap->size - 1];
heap->weight_list[0] = heap->weight_list[heap->size - 1];
heap->size--;
heapify(heap, 0);
return min_vertex;
}
int isInHeap(Heap* heap, int vertex) {
for (int i = 0; i < heap->size; i++) {
if (heap->vertex_list[i] == vertex) {
return 1;
}
}
return 0;
}
int prim(Graph* graph) {
int key[MAX_VERTICES];
int inMST[MAX_VERTICES];
int parent[MAX_VERTICES];
Heap* heap = createHeap(graph->num_vertices);
for (int i = 0; i < graph->num_vertices; i++) {
key[i] = INT_MAX;
inMST[i] = 0;
parent[i] = -1;
}
insert(heap, 0, 0);
key[0] = 0;
while (heap->size > 0) {
int u = extractMin(heap);
inMST[u] = 1;
Node* node = graph->vertices[u];
while (node != NULL) {
int v = node->vertex;
int weight = node->weight;
if (!inMST[v] && weight < key[v]) {
key[v] = weight;
parent[v] = u;
if (isInHeap(heap, v)) {
for (int i = 0; i < heap->size; i++) {
if (heap->vertex_list[i] == v) {
heap->weight_list[i] = weight;
break;
}
}
} else {
insert(heap, v, weight);
}
}
node = node->next;
}
}
int total_weight = 0;
for (int i = 1; i < graph->num_vertices; i++) {
total_weight += key[i];
}
return total_weight;
}
int main() {
Graph* graph = createGraph(5);
addEdge(graph, 0, 1, 2);
addEdge(graph, 0, 3, 6);
addEdge(graph, 1, 2, 3);
addEdge(graph, 1, 3, 8);
addEdge(graph, 1, 4, 5);
addEdge(graph, 2, 4, 7);
addEdge(graph, 3, 4, 9);
int total_weight = prim(graph);
printf("Minimum Spanning Tree Weight: %d\n", total_weight);
return 0;
}
```
在上述代码中,我们首先定义了 `Node` 结构体和 `Graph` 结构体,用于表示图的邻接表表示法。然后,我们定义了 `Heap` 结构体,用于实现最小堆。接下来,我们实现了 `createGraph` 和 `addEdge` 函数,用于创建图和添加边。然后,我们实现了 `createHeap` 函数和堆操作函数,包括 `swap`、`heapify`、`insert`、`extractMin` 和 `isInHeap`。最后,我们实现了主函数 `prim`,用于执行普利姆算法,并返回最小生成树的权重。在主函数中,我们首先初始化 `key`、`inMST` 和 `parent` 数组,然后将起始顶点加入堆中。接下来,我们在堆中取出权重最小的顶点,并将其标记为已访问。然后,我们遍历与该顶点相邻的所有顶点,并更新它们的权重和父节点。如果该顶点不在堆中,则将其加入堆中;否则,更新其权重。最后,我们计算最小生成树的权重并返回。
以上代码仅供参考,实际使用时可能需要进行一定的修改。
阅读全文