📄 databyhand.c
字号:
//手动输入边的起点,终点,以及权值
//以邻接表的存储形式建立图
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
struct node //图的顶点结构
{
int vertex; //顶点数据
struct node *nextnode;
};
typedef struct node *graph;
struct node head[10]; //存储顶点的头结点,其中的个数可以改变,只要满足自己需要的大小即可
int **yjj_node;
static int yjj_cost[20][20]; //图的邻接矩阵,其中的个数可以改变,只要满足自己需要的大小即可
/* --------------------------*/
/* create the graph */
/* --------------------------*/
void creategraph(int **node,int num)
{
graph newnode; //新顶点指针
graph ptr1;
int from; //边的起点
int to; //边的终点
int i;
for(i = 0;i < num;i++) //读取边的循环
{
from = node[i ][0]; //边的起点
to = node[i ][ 1]; //边的终点
yjj_cost[from][to] = node[i][2]; //存储边的权值
/* create newnode */
newnode = (graph )malloc(sizeof(struct node));
newnode->vertex = to;
newnode->nextnode = NULL;
ptr1 = &(head[from]);
while(ptr1->nextnode != NULL)
ptr1 = ptr1->nextnode; //下一个顶点
ptr1->nextnode = newnode; //插入结尾
}
}
/* --------------------------*/
/* the main fuction */
/* --------------------------*/
void main()
{
graph ptr;
int m,p,i,t1,t2,j;
clrscr();
/* 建立二维数组保存用户的输入:包括(起点,终点,以及权值)*/
printf("Please input one number:\n");
scanf("%d",&m);
yjj_node = (int **) malloc(m*sizeof(int *));
for(p = 0;p < m;p ++)
yjj_node[p] = (int *)malloc(3*sizeof(int));
for(t1 = 0;t1 < m;t1 ++)
{
printf("Please input datas:\n");
scanf ("%d,%d,%d",&yjj_node[t1][0],&yjj_node[t1][1], &yjj_node[t1][2]) ;
}
printf("Please output datas:\n");
for(i = 1;i <= 10;i++ )
{
head[i].vertex = i; //设置顶点值
head[i].nextnode = NULL;
}
creategraph(yjj_node,m); //create graph
printf("tu de ling jie biao lei rong:\n");
/* 输出邻接表的内容 */
for(i = 1;i <= 10;i++ )
{
ptr = head[i].nextnode;
if(ptr!=NULL) //判断只输出有相邻节点的点的信息
{
printf("node %d =>",head[i].vertex);
while(ptr != NULL)
{
printf(" %d ",ptr -> vertex);
ptr = ptr -> nextnode;
}
printf("\n");
}
}
/* 输出图的邻接矩阵 */
for(i = 1;i<=6;i++) //6 可以改变 将需要看到的输出来
{
for(j = 1;j<=6;j++)
{
printf(" %d ",yjj_cost[i][j]);
}
printf("\n");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -