📄 l_graph.java
字号:
/* =============== Program Description =============== */
/* 程序名称: l_Graph.c */
/* 程序目的: 设计一个将图形转成邻接列表的程序。 */
/* Written By Kuo-Yu Huang. (WANT Studio.) */
/* =================================================== */
#include <stdlib.h>
#define VertexNum 6 /* 定义顶点数 */
struct Node /* 声明图形顶点结构 */
{
int Vertex; /* 邻接顶点数据 */
struct Node *Next; /* 下一个邻接顶点 */
};
typedef struct Node *Graph; /* 定义图形结构 */
struct Node Head[VertexNum];/* 顶点数组 */
/* --------------------------------------------------- */
/* 建立邻接顶点至邻接列表内 */
/* --------------------------------------------------- */
void Create_L_Graph(int Vertex1,int Vertex2)
{
Graph Pointer; /* 节点声明 */
Graph New; /* 新顶点声明 */
New = (Graph) malloc(sizeof(struct Node)); /* 配置存储空间 */
if ( New != NULL ) /* 配置成功 */
{
New->Vertex = Vertex2; /* 邻近顶点 */
New->Next = NULL; /* 下一个邻接顶点指针 */
/* Pointer指针设为顶点数组之首节点 */
Pointer = &(Head[Vertex1]);
while ( Pointer->Next != NULL )
Pointer = Pointer->Next; /* 往下一个节点 */
Pointer->Next = New; /* 串连在链表尾端 */
}
}
/* --------------------------------------------------- */
/* 输出邻接列表内数据 */
/* --------------------------------------------------- */
void Print_L_Graph(struct Node *Head)
{
Graph Pointer; /* 节点声明 */
Pointer = Head->Next; /* Pointer指针设为首节点 */
while ( Pointer != NULL ) /* 当节点为NULL结束循环 */
{
printf("[%d]",Pointer->Vertex);
Pointer = Pointer->Next; /* 往下一个节点 */
}
printf("\n");
}
/* --------------------------------------------------- */
/* 主程序 */
/* --------------------------------------------------- */
void main ()
{
int Source; /* 起始顶点 */
int Destination; /* 终止顶点 */
int i,j;
for ( i=0;i<VertexNum;i++ )
{
Head[i].Vertex = i;
Head[i].Next = NULL;
}
while ( 1 )
{
printf("Please input the Edge's source : ");
scanf("%d",&Source);
if ( Source == -1 )
break;
printf("Please input the Edge's Destination : ");
scanf("%d",&Destination);
/* 错误:超出范围 */
if ( Source >= VertexNum || Destination >= VertexNum)
printf("@Error@ : Out of range!!\n");
else /* 调用建立邻接列表 */
Create_L_Graph(Source,Destination);
}
printf("##Graph##\n");
for ( i=0;i<VertexNum;i++)
{
printf("Vertex[%d] : ",i);
Print_L_Graph(&Head[i]);/* 调用输出邻接列表数据 */
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -