❶ 怎么用c语言生成一个固定顶点数和固定边数的无向图
#defineInfinity1000#defineMaxVertexNum35#defineMAX40#include#include#include#include#includetypedefstructarcell//边的权值信息{intadj;//权值}arcell,adjmatrix[MaxVertexNum][MaxVertexNum];//图的邻接矩阵类型typedefstructvexsinfo//顶点信息{intposition;//景点的编号charname[32];//景点的名称charintroction[256];//景点的介绍}vexsinfo;typedefstructmgraph//图结构信息{vexsinfovexs[MaxVertexNum];//顶点向量(数组)adjmatrixarcs;//邻接矩阵intvexnum,arcnum;//分别指定顶点数和边数}mgraph;//全局变量intvisited[35];//用于标志是否已经访问过intd[35];//用于存放权值或存储路径顶点编号mgraphcampus;//图变量(大学校园)//(1)对图初始化mgraphinitgraph(){inti=0,j=0;mgraphc;c.vexnum=28;//顶点个数c.arcnum=39;//边的个数for(i=0;i",c.vexs[d[s]].name);//输出该路径。s=0时为起点mprintf("%s",c.vexs[d[s]].name);//输出最后一个景点名(即顶点n的名字,此时s==k)printf("\n\n");}else{s=0;while(sc.vexnum){printf("\n你所输入的景点编号不存在\n");printf("请重新输入:");scanf("%d",&v0);}//whilefor(v=0;v%s",c.vexs[w].name);}printf("---->%s",c.vexs[v].name);printf("\n总路线长为%d米\n\n",d[v]);}//for}//shortestpath//(6)-(11)修改图的信息。包括建图、更新信息、删除、增加结点和边//(6)构造图的邻接矩阵intcreatgragh(mgraph&c)//建图。以图的邻接矩阵存储图{inti,j,m,n;intv0,v1;intdistance;printf("请输入图的顶点数和边数:\n");scanf("%d%d",&c.vexnum,&c.arcnum);printf("下面请输入景点的信息:\n");for(i=0;i=0&&n>=0){c.arcs[m][n].adj=distance;c.arcs[n][m].adj=c.arcs[m][n].adj;}}return1;}//creatgragh//(7)更新图的部分信息。返回值:1intnewgraph(mgraph&c){intchangenum;//计数。用于记录要修改的对象的个数inti,m,n,t,distance,v0,v1;printf("\n下面请输入你要修改的景点的个数:\n");scanf("%d",&changenum);while(changenumc.vexnum){printf("\n输入错误!请重新输入");scanf("%d",&changenum);}for(i=0;ic.arcnum){printf("\n输入错误!请重新输入");scanf("%d",&changenum);}printf("\n下面请输入更新边的信息:\n");for(i=1;i=0&&n>=0){c.arcs[m][n].adj=distance;c.arcs[n][m].adj=c.arcs[m][n].adj;}}return1;}//newgraph
❷ C语言编写一个无向图程序,急用,能用的话高分回报
#include <stdlib.h>
#include <conio.h>
#include <time.h>
void Menu();
void Plus();
void Minus();
void Multiply();
void Dir();
int main()
{
int n, flag = 0;
while(1)
{
Menu();
do
{
flag = 0;
scanf("%d", &n);
switch(n)
{
case 1: Plus(); break;
case 2: Minus(); break;
case 3: Multiply(); break;
case 4: Dir(); break;
case 5: exit(0);
default:
{
printf("输入有误, 请重新输入!");
flag = 1;
}
}
}
while(flag);
}
return 0;
}
void Menu()
{
system("cls");
printf("\t\t欢迎来到小学生算数训练\n");
printf("\t\t\t1.加法训练\n");
printf("\t\t\t2.减法训练\n");
printf("\t\t\t3.乘法训练\n");
printf("\t\t\t4.除法训练\n");
printf("\t\t\t5.退出\n");
printf("\t\t\t请选择: ");
}
void Plus()
{
system("cls");
printf("\t\t现在是加法训练\n\n");
srand((unsigned)time(NULL));
int plu[10][4];
int m, n, result, input;
for(int i = 0; i < 10; i++)
{
m = rand() % 10;
n = rand() % 10;
printf("请计算: %d + %d = ", m, n);
result = m + n;
scanf("%d", &input);
if(input != result)
printf("真可惜, 回答错误, 请再接再厉!\n");
else
printf("恭喜你, 回答正确, 请继续加油!\n");
plu[i][0] = m;
plu[i][1] = n;
plu[i][2] = input;
plu[i][3] = result;
}
printf("===============十道题目回答如下=================\n\n");
for(int j = 0; j < 10; j++)
{
printf("%d + %d = %d\t", plu[j][0], plu[j][1], plu[j][2]);
if(plu[j][2] != plu[j][3])
printf("(正确答案为%d)", plu[j][3]);
printf("\n");
}
printf("输入任意键返回主菜单\n");
getch();
}
void Minus()
{
system("cls");
printf("\t\t现在是减法训练\n\n");
srand((unsigned)time(NULL));
int plu[10][4];
int m, n, result, input;
for(int i = 0; i < 10; i++)
{
do
{
m = rand() % 10;
n = rand() % 10;
}while(m < n);
printf("请计算: %d - %d = ", m, n);
result = m - n;
scanf("%d", &input);
if(input != result)
printf("真可惜, 回答错误, 请再接再厉!\n");
else
printf("恭喜你, 回答正确, 请继续加油!\n");
plu[i][0] = m;
plu[i][1] = n;
plu[i][2] = input;
plu[i][3] = result;
}
printf("===============十道题目回答如下=================\n\n");
for(int j = 0; j < 10; j++)
{
printf("%d - %d = %d\t", plu[j][0], plu[j][1], plu[j][2]);
if(plu[j][2] != plu[j][3])
printf("(正确答案为%d)", plu[j][3]);
printf("\n");
}
printf("输入任意键返回主菜单\n");
getch();
}
void Multiply()
{
system("cls");
printf("\t\t现在是乘法训练\n\n");
srand((unsigned)time(NULL));
int plu[10][4];
int m, n, result, input;
for(int i = 0; i < 10; i++)
{
m = rand() % 10;
n = rand() % 10;
printf("请计算: %d * %d = ", m, n);
result = m * n;
scanf("%d", &input);
if(input != result)
printf("真可惜, 回答错误, 请再接再厉!\n");
else
printf("恭喜你, 回答正确, 请继续加油!\n");
plu[i][0] = m;
plu[i][1] = n;
plu[i][2] = input;
plu[i][3] = result;
}
printf("===============十道题目回答如下=================\n\n");
for(int j = 0; j < 10; j++)
{
printf("%d * %d = %d\t", plu[j][0], plu[j][1], plu[j][2]);
if(plu[j][2] != plu[j][3])
printf("(正确答案为%d)", plu[j][3]);
printf("\n");
}
printf("输入任意键返回主菜单\n");
getch();
}
void Dir()
{
system("cls");
printf("\t\t现在是除法训练\n\n");
srand((unsigned)time(NULL));
int plu[10][4];
int m, n, result, input;
for(int i = 0; i < 10; i++)
{
do
{
m = rand() % 10;
n = rand() % 10;
}while(m == 0 || n == 0);
result = m * n;
int temp;
temp = m;
m = result;
result = temp;
printf("请计算: %d / %d = ", m, n);
scanf("%d", &input);
if(input != result)
printf("真可惜, 回答错误, 请再接再厉!\n");
else
printf("恭喜你, 回答正确, 请继续加油!\n");
plu[i][0] = m;
plu[i][1] = n;
plu[i][2] = input;
plu[i][3] = result;
}
printf("===============十道题目回答如下=================\n\n");
for(int j = 0; j < 10; j++)
{
printf("%d / %d = %d\t", plu[j][0], plu[j][1], plu[j][2]);
if(plu[j][2] != plu[j][3])
printf("(正确答案为%d)", plu[j][3]);
printf("\n");
}
printf("输入任意键返回主菜单\n");
getch();
}
❸ 无向图G.,有n个顶点,m条边,如何采用邻接表存储该图主要是想知道算法~用c语言
无向图就是不分方向的图
连接表的横列有N项,纵列也是N项
形成的N*N项每项都被称为边结点
每项都有纵横两个坐标,例如点(N,N-1),表示的就是从第N点向第N-1点有无路径。
由于有E条边,自然有E条路径,但是由于无向,=双向,所以要乘以二
❹ 请教编程大神数据结构题目:c语言使用邻接链表存储结构实现用户输入的无向图的连通性判断
#include<stdio.h>
#include<stdlib.h>
#defineMAX_VERTEX_NUM100
typedefstructArcNode{
intadjvex;//该边的另一个顶点的位置
structArcNode*nextarc;//指向下一条边
}ArcNode;
typedefstructVNode{
intdata;//顶点的值
ArcNode*firstarc;//指向第一条依附该顶点的边的指针
}VNode,AdjList[MAX_VERTEX_NUM];
typedefstruct{
AdjListvertices;//顶点数组
intvexnum,arcnum;
}ALGraph;
intLocateVex(ALGraphG,intv){//定位函数
for(inti=0;i<G.vexnum;i++){
if(v==G.vertices[i].data)returni;
}
}
voidCreateUDG(ALGraph&G){
ArcNode*p,*q;
inti,j,k,v1,v2;
printf("分别输入顶点个数和边的数目: ");
scanf("%d%d",&G.vexnum,&G.arcnum);
printf("分别输入各个顶点值: ");
for(i=0;i<G.vexnum;i++){
scanf("%d",&G.vertices[i].data);
G.vertices[i].firstarc=NULL;//初始化
}
printf("分别输入各条边的两个顶点: ");
for(k=0;k<G.arcnum;k++){
scanf("%d%d",&v1,&v2);
i=LocateVex(G,v1);j=LocateVex(G,v2);//定位
p=(ArcNode*)malloc(sizeof(ArcNode));//申请一个结点
p->adjvex=j;p->nextarc=NULL;//赋值
p->nextarc=G.vertices[i].firstarc;//连接结点
G.vertices[i].firstarc=p;//连接结点
q=(ArcNode*)malloc(sizeof(ArcNode));
q->adjvex=i;q->nextarc=NULL;
q->nextarc=G.vertices[j].firstarc;
G.vertices[j].firstarc=q;
}
}
voidPrintUDG(ALGraphG){//输出邻接表
inti,j;
for(i=0;i<G.vexnum;i++){
printf("%d:",i);
ArcNode*p;
p=G.vertices[i].firstarc;
while(p!=NULL){
printf("->%d",p->adjvex);
p=p->nextarc;
}
printf(" ");
}
}
intmain(){
ALGraphG;
CreateUDG(G);
PrintUDG(G);
return0;
}
❺ C语言实现无向图
可以用邻接矩阵表示法:
#define max 100
typedef struct
{
int vex[max];//存储顶点值,类型可以变
int edge[max][max];//存储顶点之间的关系,以1或者0表示,1为有边,0为无
int e,v;//vertex存储顶点数,edge存储边的条数,所以无向图1的个数是边的个数的两倍,谢谢。
}m;