邻接表的创建 c语言 数据结构 超详细~~

#include<stdio.h>
typedef char vertextype;
typedef int edgetype;
#define maxsize 100
#include<stdlib.h>
typedef struct edgenode
{
int adjvex;//邻接表的结点下标
struct edgenode* next;//指向下一个顶点的指针
}edgenode;
typedef struct vertexnode
{
int data;//顶点的数据域
edgenode* firstchild;//指向边表第一个结点的指针
}vertexnode;
typedef struct graph
{
vertexnode arc[maxsize];
int numedge, numnode;//顶点结点,边表
}graph;
void init(graph* G)
{
printf(“请输入顶点结点和边表的个数\n”);
scanf_s("%d%d", &G->numnode, &G->numedge);
printf(“图的初始化工作完毕\n”);
}
//建立
void create(graph* G)
{
int i,j,k,m;
printf(“请输入顶点表\n”);
for (m = 0; m < G->numnode; m++)
{
scanf_s("%d", &G->arc[m].data);
G->arc[m].firstchild = NULL;
}
for (k = 0; k < G->numedge; k++)
//循环条件不可以用i和j,因为内部已经使用了
//循环的截止条件已经为numedge,而不是numnode
{
edgenode* e=(edgenode*)malloc(sizeof(edgenode));
printf(“请输入边(vi,vj)的边的下标i,j”);
scanf_s("%d%d", &i, &j);
e->adjvex = j;
e->next = G->arc[i].firstchild;
G->arc[i].firstchild = e;
e = (edgenode*)malloc(sizeof(edgenode));
e->adjvex = i;
e->next = G->arc[j].firstchild;
G->arc[j].firstchild = e;
}
printf(“图的创建已完毕\n”);
}
int main(void)
{
graph* G =(graph*)malloc(sizeof(graph));
init(G);
create(G);
system(“pause”);
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
邻接表是一种表示图的数据结构,可以用来存储图的顶点和边。在C语言中,可以使用链表来实现邻接表。 首先,我们需要定义图的顶点结构体和边结构体: ```c // 边的结构体 typedef struct Edge { int dest; // 边的目标顶点 struct Edge* next; // 指向下一条边的指针 } Edge; // 顶点的结构体 typedef struct Vertex { int value; // 顶点的值 Edge* head; // 指向第一条边的指针 } Vertex; ``` 然后,我们可以定义邻接表的结构体,其中包含顶点数组和顶点数量: ```c typedef struct Graph { int numVertices; // 顶点数量 Vertex* vertices; // 顶点数组 } Graph; ``` 接下来,我们可以实现邻接表创建函数、添加边的函数和打印函数: ```c // 创建图的邻接表 Graph* createGraph(int numVertices) { Graph* graph = (Graph*)malloc(sizeof(Graph)); graph->numVertices = numVertices; graph->vertices = (Vertex*)malloc(numVertices * sizeof(Vertex)); // 初始化顶点数组 for (int i = 0; i < numVertices; i++) { graph->vertices[i].value = i; graph->vertices[i].head = NULL; } return graph; } // 添加边到图的邻接表 void addEdge(Graph* graph, int src, int dest) { Edge* newEdge = (Edge*)malloc(sizeof(Edge)); newEdge->dest = dest; newEdge->next = graph->vertices[src].head; graph->vertices[src].head = newEdge; // 无向图需添加反向边 newEdge = (Edge*)malloc(sizeof(Edge)); newEdge->dest = src; newEdge->next = graph->vertices[dest].head; graph->vertices[dest].head = newEdge; } // 打印图的邻接表 void printGraph(Graph* graph) { for (int i = 0; i < graph->numVertices; i++) { printf("顶点 %d 的边:", i); Edge* curr = graph->vertices[i].head; while (curr != NULL) { printf(" -> %d", curr->dest); curr = curr->next; } printf("\n"); } } ``` 现在,我们可以在主函数中使用这些函数来创建图、添加边并打印邻接表: ```c int main() { int numVertices = 5; Graph* graph = createGraph(numVertices); addEdge(graph, 0, 1); addEdge(graph, 0, 4); addEdge(graph, 1, 2); addEdge(graph, 1, 3); addEdge(graph, 1, 4); addEdge(graph, 2, 3); addEdge(graph, 3, 4); printGraph(graph); return 0; } ``` 运行以上代码,将输出如下结果: ``` 顶点 0 的边: -> 4 -> 1 顶点 1 的边: -> 4 -> 3 -> 2 -> 0 顶点 2 的边: -> 3 -> 1 顶点 3 的边: -> 4 -> 2 -> 1 顶点 4 的边: -> 3 -> 1 -> 0 ``` 这样,我们就完成了邻接表C语言实现。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值