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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

数据结构——图-有向图和无向图的邻接表基础

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

#include <stdio.h> #include <stdlib.h> #define VertexType char //頂點的數據類型(char) #define VertexMax 20 //最大頂點個數 typedef struct ArcNode//邊表 {int adjvex;//存儲的是該頂點在頂點數組即AdjList[]中的位置 struct ArcNode *next; }ArcNode;typedef struct VNode //單個頂點 {VertexType vertex;struct ArcNode *firstarc; }VNode;typedef struct //頂點表 {VNode AdjList[VertexMax];//由頂點構成的結構體數組 int vexnum,arcnum; //頂點數和邊數 int kind; //記錄圖的類型 }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; }//1.有向圖 void CreateDG(ALGraph *G) {int i,j;//1.輸入頂點數和邊數 printf("輸入頂點個數和邊數:\n");printf("頂點數 n="); scanf("%d",&G->vexnum);printf("邊 數 e="); scanf("%d",&G->arcnum);printf("\n"); printf("\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; printf("請輸入邊的信息(不需要用空格隔開):\n\n"); for(i=0;i<G->arcnum;i++){ //輸入邊信息,并確定v1和v2在G中的位置,即頂點在AdjList[]數組中的位置(下標) printf("輸入第%d條邊信息:",i+1); scanf(" %c%c",&v1,&v2);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->next=G->AdjList[n].firstarc;//改鏈(頭插法) G->AdjList[n].firstarc=p1;}//for G->kind=1; } //2.無向圖 void CreateUDG(ALGraph *G) {int i,j;//1.輸入頂點數和邊數printf("輸入頂點個數和邊數:\n");printf("頂點數 n="); scanf("%d",&G->vexnum);printf("邊 數 e="); scanf("%d",&G->arcnum);printf("\n"); printf("\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; printf("請輸入邊的信息:\n\n"); for(i=0;i<G->arcnum;i++){ //輸入邊信息,并確定v1和v2在G中的位置,即頂點在AdjList[]數組中的位置(下標) printf("輸入第%d條邊信息:",i+1); scanf(" %c%c",&v1,&v2);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->next=G->AdjList[n].firstarc;//改鏈(頭插法) G->AdjList[n].firstarc=p1;p2=(ArcNode *)malloc(sizeof(ArcNode));//無向圖的對稱 p2->adjvex=n;p2->next=G->AdjList[m].firstarc;G->AdjList[m].firstarc=p2;}//for G->kind=2; } 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",p->adjvex);p=p->next;}} printf("\n"); } void GraphChoice(ALGraph *G) {int target;printf(" 請選擇圖的類型:1.有向圖DG 2.無向圖UDG\n\n");scanf("%d",&target);//選擇圖的類型 printf("\n");switch (target) //根據所選類型,調用不同的函數實現構造圖的功能 {case 1:printf("您選擇的是 1.DG\n\n"); printf("-------------------------------\n");CreateDG(G);break;case 2:printf("您選擇的是 2.UDG\n\n"); printf("-------------------------------\n");CreateUDG(G);break;default:printf("無效選擇!!!請重新選擇!\n\n"); printf("-------------------------------\n");GraphChoice(G); break;} }int main() {ALGraph G;GraphChoice(&G);print(G);return 0; }

總結

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

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