生活随笔
收集整理的這篇文章主要介紹了
数据结构——图-有向带权图的邻接表基础
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#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("有向帶權圖\n"); printf("輸入頂點個數和邊數:\n");printf("頂點數 n="); scanf("%d",&G
->vexnum
);printf("邊 數 e="); scanf("%d",&G
->arcnum
);printf("\n\n"); printf("輸入頂點元素(需要用空格隔開):");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
,*p2
; int value
;printf("請輸入邊的信息(需要用空格隔開):\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權值[%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;
}
總結
以上是生活随笔為你收集整理的数据结构——图-有向带权图的邻接表基础的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。