柒姐吧 关注:45贴子:8,012
  • 5回复贴,共1

#include<stdio.h>
#include<string.h>
#define N 50
#define M2*N-1
typedef struct
{
chardata[5];
intweight;
j++;
}
m+=ht[i].weight;
sum+=hr[i].weight*j;
printf("\n");
}
printf("\n 平均长度=%g\n",1.0*sum/m);
}
void main()
{
intn=15,i;
char
*str[]={"The","of","a","to","and","in","that","he","is","at","on","for","His","are","be"};
intfnum []={1192,677,541,518,462,450,242,195,190,181,174,157,138,124,123};
HTNodeht[M];
HCodehcd[N];
for(i=0;i<n;i++)
{
strcpy(ht[i].data,str[i]);
ht[i].weight=fnum[i];
}
printf("\n");
CreateHT(ht,n);
CreateHCode(ht,hcd,n);
DispHCode(ht,hcd,n);
printf("\n");
}
【实验结果】
1、
2、 编程实现图的邻接矩阵和邻接表存储的相关运算。
【程序代码】
typedef int InfoType;
#define MAXV 100
typedef struct
{int no;
Info Type info;
}VertexType;
typedef struct
{ int edges[MAXV][MAXV];
int vexnum,arcnum;
VertexType vexs[MAXV];
}MGraph;
typedef struct ANode
{ int adjvex;
struct ANode*nextarc;
InfoType info;
}ArcNode;
typedef int Vertex;
typedef struct Vnode
{ Vertex data;
ArcNode*firstarc;
}VNode;
typedef VNode AdjList[MAXV];
typedef struct
{
AdjListadjlist;
intn,e;
}ALGraph;
#include<stdio.h>
#include<malloc.h>
#include"graph.h"
#define INF32767
void MatToList(MGraph g,ALGraph*&G)
{
inti,j,n=g.vexnum;
ArcNode*p;
G=(ALGraph*)malloc(sizeof(ALGraph));
for(i=0;i<n;i++)
G->adjlist[i].firstarc=NULL;
for(i=0;i<n;i++)
for(j=n-1;j>=0;j--)
if(g.edges[i][j]!=0)
{
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=j;
p->info=g.edges[i][j];
p->nextarc=G->adjlist[i].firstarc;
G->adjlist[i].firstarc=p;
}
G->n=n;G->e=g.arcnum;
}
void ListToMat(ALGraph*G,MGraph&g)
{
inti,j,n=G->n;
ArcNode*p;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
g.edges[i][j]=0;
for(i=0;i<n;i++)
{
p=G->adjlist[i].firstarc;
while(p!=NULL)
{
g.edges[i][p->adjvex]=p->info;
p=p->nextarc;
}
}
g.vexnum=n;g.arcnum=G->e;
}
void DispMat(MGraph g)
{
inti,j;
for(i=0;i<g.vexnum;i++)
{
for(j=0;j<g.vexnum;j++)
if(g.edges[i][j]==INF)
printf("%3s","∞");
else
printf("%3d",g.edges[i][j]);
printf("\n");
}
}
void DispAdj(ALGraph*G)
{
inti;
ArcNode*p;
for(i=0;i<G->n;i++)
{
p=G->adjlist[i].firstarc;
if(p!=NULL)printf("%d:",i);
while(p!=NULL)
{
printf("%3d",p->adjvex);
p=p->nextarc;
}
printf("\n");
}
}
#include<stdio.h>
#include<malloc.h>
#include"graph.h"
extern voidMatToList(MGraph,ALGraph*&);
extern void ListToMat(ALGraph*,MGraph&);
extern void DispMat(MGraph);
extern void DespAdj(ALGraph*);
void main()
{
inti,j;
MGraphg,g1;
ALGraph*G;
intA[MAXV][6]={
{0,5,0,7,0,0},
{0,0,4,0,0,0},
{8,0,0,0,0,9},
{0,0,5,0,0,6},
{0,0,0,5,0,0},
{3,0,0,0,1,0}};
g.vexnum=6;g.arcnum=10;
for(i=0;i<g.vexnum;i++)
for(j=0;j<g.vexnum;j++)
g.edges[i][j]=A[i][j];
printf("\n");
printf("有向图G的邻接矩阵:\n");
DispMat(g);
G=(ALGraph*)malloc(sizeof(ALGraph));
printf("图G的邻接矩阵转换成邻接表:\n");
MatToList(g,G);
DispAdj(G);
printf("图G的邻接表转换成邻接矩阵:\n");
ListToMat(G,g1);
DispMat(g1);
printf("\n");
}


IP属地:江苏1楼2016-11-24 09:47回复
    你有毒吧你


    来自Android客户端2楼2016-12-17 08:54
    收起回复
      哈哈哈哈哈哈哈哈


      来自iPhone客户端3楼2017-01-26 10:29
      回复
        这是记事本啊


        来自iPhone客户端4楼2017-07-31 16:04
        回复