日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构——图-有向带权图的邻接表基础

發(fā)布時間:2023/12/4 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构——图-有向带权图的邻接表基础 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


#include <stdio.h> #include <stdlib.h> #define VertexType char //頂點的數據類型(char) #define VertexMax 20 //最大頂點個數 typedef struct ArcNode//邊表 {int adjvex;//存儲的是該頂點在頂點數組即AdjList[]中的位置int weight; struct ArcNode *next; }ArcNode;typedef struct VNode //單個頂點 {VertexType vertex;struct ArcNode *firstarc; }VNode;typedef struct //頂點表 {VNode AdjList[VertexMax];//由頂點構成的結構體數組 int vexnum,arcnum; //頂點數和邊數 }ALGraph;int LocateVex(ALGraph *G,VertexType v) { int i;for(i=0;i<G->vexnum;i++){if(v==G->AdjList[i].vertex){return i;}}printf("No Such Vertex!\n");return -1; }void CreateDG(ALGraph *G) {int i,j;//1.輸入頂點數和邊數 printf("有向帶權圖\n"); printf("輸入頂點個數和邊數:\n");printf("頂點數 n="); scanf("%d",&G->vexnum);printf("邊 數 e="); scanf("%d",&G->arcnum);printf("\n\n"); //2.頂點表數據域填值初始化頂點表指針域 printf("輸入頂點元素(需要用空格隔開):");for(i=0;i<G->vexnum;i++){scanf(" %c",&G->AdjList[i].vertex);G->AdjList[i].firstarc=NULL;} printf("\n");//3.輸入邊信息構造鄰接表 int n,m;VertexType v1,v2;ArcNode *p1,*p2; int value;printf("請輸入邊的信息(需要用空格隔開):\n\n"); for(i=0;i<G->arcnum;i++){ //輸入邊信息,并確定v1和v2在G中的位置,即頂點在AdjList[]數組中的位置(下標) printf("輸入第%d條邊信息:",i+1); getchar();scanf(" %c %c %d",&v1,&v2,&value);n=LocateVex(G,v1);m=LocateVex(G,v2);if(n==-1||m==-1){printf("NO This Vertex!\n");return;} p1=(ArcNode *)malloc(sizeof(ArcNode));p1->adjvex=m;//填上坐標 p1->weight=value;p1->next=G->AdjList[n].firstarc;//改鏈(頭插法) G->AdjList[n].firstarc=p1;}} void print(ALGraph G) {int i;ArcNode *p;printf("\n-------------------------------");printf("\n圖的鄰接表表示:\n");for(i=0;i<G.vexnum;i++){printf("\n AdjList[%d]%4c",i,G.AdjList[i].vertex);p=G.AdjList[i].firstarc;while(p!=NULL){printf("-->%d權值[%d]",p->adjvex,p->weight);p=p->next;}} printf("\n"); } void InsertVex(ALGraph *G,VertexType v) {} int main() {ALGraph G;CreateDG(&G);print(G);return 0; }

總結

以上是生活随笔為你收集整理的数据结构——图-有向带权图的邻接表基础的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。