#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");
}
}
|
新聞熱點
疑難解答
圖片精選