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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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


#include <stdio.h> #include <stdlib.h> #define VertexType char //頂點(diǎn)的數(shù)據(jù)類型(char) #define VertexMax 20 //最大頂點(diǎn)個(gè)數(shù) typedef struct ArcNode//邊表 {int adjvex;//存儲(chǔ)的是該頂點(diǎn)在頂點(diǎn)數(shù)組即AdjList[]中的位置int weight; struct ArcNode *next; }ArcNode;typedef struct VNode //單個(gè)頂點(diǎn) {VertexType vertex;struct ArcNode *firstarc; }VNode;typedef struct //頂點(diǎn)表 {VNode AdjList[VertexMax];//由頂點(diǎn)構(gòu)成的結(jié)構(gòu)體數(shù)組 int vexnum,arcnum; //頂點(diǎn)數(shù)和邊數(shù) }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.輸入頂點(diǎn)數(shù)和邊數(shù) printf("有向帶權(quán)圖\n"); printf("輸入頂點(diǎn)個(gè)數(shù)和邊數(shù):\n");printf("頂點(diǎn)數(shù) n="); scanf("%d",&G->vexnum);printf("邊 數(shù) e="); scanf("%d",&G->arcnum);printf("\n\n"); //2.頂點(diǎn)表數(shù)據(jù)域填值初始化頂點(diǎn)表指針域 printf("輸入頂點(diǎn)元素(需要用空格隔開):");for(i=0;i<G->vexnum;i++){scanf(" %c",&G->AdjList[i].vertex);G->AdjList[i].firstarc=NULL;} printf("\n");//3.輸入邊信息構(gòu)造鄰接表 int n,m;VertexType v1,v2;ArcNode *p1; int value;printf("請(qǐng)輸入邊的信息(需要用空格隔開)包含邊的兩端和權(quán)值:\n\n"); for(i=0;i<G->arcnum;i++){ //輸入邊信息,并確定v1和v2在G中的位置,即頂點(diǎn)在AdjList[]數(shù)組中的位置(下標(biāo)) 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;//填上坐標(biāo) 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權(quán)值[%d]",p->adjvex,p->weight);p=p->next;}} printf("\n"); } void InsertVex(ALGraph *G,VertexType v) {//向鄰接表中添加一個(gè)新頂點(diǎn)G->AdjList[G->vexnum].vertex=v;G->AdjList[G->vexnum].firstarc=NULL;G->vexnum=G->vexnum+1; }void InsertArc(ALGraph *G,VertexType v,VertexType w) {//添加一條新邊到鄰接表 int value;printf("輸入這條邊的權(quán)值:");scanf("%d",&value);ArcNode *p1; int n,m;n=LocateVex(G,v);m=LocateVex(G,w);p1=(ArcNode *)malloc(sizeof(ArcNode));p1->adjvex=m;//填上坐標(biāo) p1->weight=value;p1->next=G->AdjList[n].firstarc;//改鏈(頭插法) G->AdjList[n].firstarc=p1;G->arcnum++;} void DeleteArc(ALGraph *G,VertexType v,VertexType w) {//刪除一條指定的邊 int n,m;n=LocateVex(G,v);m=LocateVex(G,w);ArcNode *pre,*p;pre=G->AdjList[n].firstarc;p=pre->next;while(p!=NULL){if(pre->next->adjvex==p->adjvex){pre->next=p->next;free(p);break;}p=p->next;pre=pre->next;}G->arcnum--; }void DeleteVex(ALGraph *G,VertexType v) {int n;n=LocateVex(G,v);int arcn=0;ArcNode *p;p=G->AdjList[n].firstarc;while(p!=NULL){p=p->next;arcn++;}G->AdjList[n].firstarc=NULL;for(int i=n;i<G->vexnum-1;i++){G->AdjList[i].firstarc=G->AdjList[i+1].firstarc;G->AdjList[i].vertex=G->AdjList[i+1].vertex;}G->vexnum--;G->arcnum=G->arcnum-arcn;} void choice(ALGraph *G) {int i=0;int j=1;VertexType v,w;printf("[1]:添加的新頂點(diǎn)\n");printf("[2]:添加新邊的兩端結(jié)點(diǎn)\n");printf("[3]:刪除邊的兩端結(jié)點(diǎn)\n");printf("[4]:刪除的頂點(diǎn)(及其相關(guān)聯(lián)的邊)\n");printf("[5]:退出\n");while(j){scanf("%d",&i);switch(i){case 1:printf("輸入要添加的新頂點(diǎn):");getchar();scanf("%c",&v); InsertVex(G,v);print(*G);break;case 2:printf("輸入要添加新邊的兩端結(jié)點(diǎn):");getchar(); scanf("%c %c",&v,&w); InsertArc(G,v, w);print(*G);break;case 3:printf("輸入要?jiǎng)h除邊的兩端結(jié)點(diǎn):");getchar();scanf("%c %c",&v,&w); DeleteArc(G,v, w);print(*G);break;case 4:printf("輸入要?jiǎng)h除的頂點(diǎn)(及其相關(guān)聯(lián)的邊):");getchar();scanf("%c",&v); DeleteVex(G,v);print(*G);break;case 5:j=0;break;} }} int main() {ALGraph G;CreateDG(&G);print(G);choice(&G);return 0; }

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。