圖的鄰接表存儲表現示例講授。本站提示廣大學習愛好者:(圖的鄰接表存儲表現示例講授)文章只能為提供參考,不一定能成為您想要的結果。以下是圖的鄰接表存儲表現示例講授正文
//---------圖的鄰接表存儲表現-------
#include<stdio.h>
#include<stdlib.h>
#define MAX_VERTEXT_NUM 20
typedef int InfoType;
typedef char VertextType;
typedef struct ArcNode
{
int adjvex;
struct ArcNode *nextArc;
InfoType *info;
}ArcNode;
typedef struct VNode
{
VertextType data;
ArcNode *firstArc;
}VNode, AdjList[MAX_VERTEXT_NUM];
typedef struct
{
AdjList verTices;
int vexNum;
int arcNum;
int kind;
}ALGraph;
void CreateGraph(ALGraph *G);
void DisplayGraph(ALGraph *G);
int main()
{
ALGraph *Graph = (ALGraph *)malloc(sizeof(ALGraph));
CreateGraph(Graph);
DisplayGraph(Graph);
system("pause");
}
void CreateGraph(ALGraph *G)
{
int i,j,k;
ArcNode *arcNode;
printf_s("請輸出極點數和邊數:");
scanf_s("%d,%d",&G->vexNum, &G->arcNum);
//樹立極點表
printf_s("樹立極點表\n");
for (i = 0; i < G->vexNum; i++)
{
printf_s("請輸出第%d個極點:", i);
fflush(stdin);//刷新緩沖區
G->verTices[i].data = getchar();
G->verTices[i].firstArc = NULL;
}
//樹立邊表
printf_s("樹立邊表\n");
for (k = 0; k < G->arcNum; k++)
{
printf_s("請輸出(vi-vj)的極點對序號");
scanf_s("%d,%d", &i, &j);
arcNode = (ArcNode *)malloc(sizeof(ArcNode));
arcNode->adjvex = j;
arcNode->nextArc = G->verTices[i].firstArc;//拔出表頭
G->verTices[i].firstArc = arcNode;
arcNode = (ArcNode *)malloc(sizeof(ArcNode));
arcNode->adjvex = i;
arcNode->nextArc = G->verTices[j].firstArc;//拔出表頭
G->verTices[j].firstArc = arcNode;
}
}
void DisplayGraph(ALGraph *G)
{
int i;
for (i = 0; i < G->vexNum; i++)
{
printf_s("%d->", i);
while (G->verTices[i].firstArc != NULL)
{
printf_s("%d->", G->verTices[i].firstArc->adjvex);
G->verTices[i].firstArc = G->verTices[i].firstArc->nextArc;
}
printf_s("\n");
}
}
請輸出極點數和邊數:6,7
樹立極點表
請輸出第0個極點:0
請輸出第1個極點:1
請輸出第2個極點:2
請輸出第3個極點:3
請輸出第4個極點:4
請輸出第5個極點:5
樹立邊表
請輸出(vi-vj)的極點對序號0,1
請輸出(vi-vj)的極點對序號0,4
請輸出(vi-vj)的極點對序號1,4
請輸出(vi-vj)的極點對序號1,5
請輸出(vi-vj)的極點對序號2,3
請輸出(vi-vj)的極點對序號2,5
請輸出(vi-vj)的極點對序號3,5
0->4->1->
1->5->4->0->
2->5->3->
3->5->2->
4->1->0->
5->3->2->1->
請按隨意率性鍵持續. . .