无向表 链表存储方式
#include "stdio.h" #include "stdlib.h" #define MAXVEX 10 typedef char VertexType; typedef int EdgeType; typedef struct EdgeNode { int adjvex; EdgeType weight; struct EdgeNode *next;//下一接边表结点的指针 }EdgeNode;//边表结点 typedef struct VertexNode { VertexType data; EdgeNode *firstedge;//定义边表头的指针 }AdjList[MAXVEX];//主表结点 typedef struct{ AdjList adjList; int numVertexes, numEdges; }GraphAdjList; void CreateALGraph(GraphAdjList *G) { int i, j, k; EdgeNode *e; printf("输入顶点数跟边数:\n"); scanf("%d,%d", G->numVertexes, G->numEdges); for (i = 0; i < G->numVertexes; i++)//输入data firstedge NULL { scanf(&G->adjList[i].data); G->adjList[i].firstedge = NULL; } for (k = 0; k < G->numEdges; k++)//输入边 { printf("输入边(vi,vj)上的顶点序号:\n"); scanf("%d,%d", &i, &j); e = (EdgeNode *)malloc(sizeof(EdgeNode)); e->adjvex = j; e->next = G->adjList[i].firstedge;//将主表的firstedge指针赋值给新创建的边表结点 G->adjList[i].firstedge = e;//主表的firstedge指向新创建的边表结点,实现链表头插 //无向表 对两个结点同时进行插入 边表链 e = (EdgeNode *)malloc(sizeof(EdgeNode)); e->adjvex = i;// e->next = G->adjList[j].firstedge; G->adjList[j].firstedge = e; } } int main() { // printf("hello"); system("pause"); }
posted on 2015-07-17 19:10 winters1992x 阅读(201) 评论(0) 编辑 收藏 举报