生活随笔
收集整理的這篇文章主要介紹了
数据结构——图-有向带权图的邻接表
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#include <stdio.h>
#include <stdlib.h>
#define VertexType char
#define VertexMax 20 typedef struct ArcNode
{int adjvex
;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
;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"); printf("輸入頂點(diǎn)元素(需要用空格隔開):");for(i
=0;i
<G
->vexnum
;i
++){scanf(" %c",&G
->AdjList
[i
].vertex
);G
->AdjList
[i
].firstarc
=NULL;} printf("\n");int n
,m
;VertexType v1
,v2
;ArcNode
*p1
; int value
;printf("請(qǐng)輸入邊的信息(需要用空格隔開)包含邊的兩端和權(quán)值:\n\n"); for(i
=0;i
<G
->arcnum
;i
++){ 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權(quán)值[%d]",p
->adjvex
,p
->weight
);p
=p
->next
;}} printf("\n");
} void InsertVex(ALGraph
*G
,VertexType v
)
{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
;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ò),歡迎將生活随笔推薦給好友。