关键路径c语言源程序,图关键路径(C语言)

数据结构中求关键路径的算法

#include

#include

#include

#include

typedef struct ArcCell{

int adj;/*顶点关系类型,用1表示相邻,0表示不相邻*/

}ArcCell,**AdjMatrix;/*邻接矩阵*/

typedef struct type{

char data[3];/*顶点值*/

int info;/*弧的长度*/

struct type *next;/*顶点的下一个指针*/

}VertexType;

typedef struct{

VertexType *vexs;/*顶点向量*/

AdjMatrix arcs;/*邻接矩阵*/

int vexnum,arcnum;/*图的顶点数和边数*/

}MGraph;

/* ******************8 */

typedef int Status;

#define STACK_INIT_SIZE 50

typedef int ElemType;

typedef struct STACK /*定义栈类型*/

{

ElemType *base;

ElemType *top;

int stacksize;

int length;

}SqStack,*Stack;

void InitStack(Stack *S) /*初始化栈*/

{

*S=(SqStack *)malloc(sizeof(SqStack));

(*S)->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));

if(!(*S)->base)exit(-1);

(*S)->top=(*S)->base;

(*S)->stacksize=STACK_INIT_SIZE;

(*S)->length=0;

}

Status DestroyStack(Stack *S) /* 销毁栈*/

{

free((*S)->base);

free((*S));

return 1;

}

Status StackEmpty(SqStack S) /*判断栈空否*/

{

if(S.top==S.base) return 1;

else

return 0;

}

void Push(Stack *S,ElemType e) /*把数据压入栈*/

{

if((*S)->top - (*S)->base>=(*S)->stacksize)

{

(*S)->base=(ElemType *) realloc((*S)->base,

((*S)->stacksize + 10) * sizeof(ElemType));

if(!(*S)->base)exit(-1);

(*S)->top=(*S)->base+(*S)->stacksize;

(*S)->stacksize +=10;

}

*((*S)->top++)=e;

++(*S)->length;

}

Status Pop(Stack *S,ElemType *e)/*删除栈顶元素*/

{

if((*S)->top==(*S)->base) return 0;

*e=*((*S)->top-1);

--(*S)->length;

(*S)->top--;

return 1;

}

/* ******************** */

void InitGraph(MGraph *G)/*初始图*/

{ int i,nu,mu;

printf("\n输入顶点的个数和(边)弧的个数:");

scanf("%d%d",&nu,&mu);

G->arcs=(ArcCell **)malloc(nu*sizeof(ArcCell *));

for(i=0;i

G->arcs[i]=(ArcCell *)malloc(nu*sizeof(ArcCell));

G->vexs=(VertexType *)malloc(nu*sizeof(VertexType));/*分配顶点空间*/

G->vexnum=nu;G->arcnum=mu;/*图的顶点数和边数*/

}

void DestroyGraph(MGraph *G)/* 销毁图*/

{ int i;

free(G->vexs);

for(i=0;ivexnum;i++)

free(G->arcs[i]);

}

void InsertGraph(MGraph *G,int i,VertexType e)

{ if(i<0||i>G->vexnum) return;

G->vexs[i].next=e.next;

G->vexs[i].info=http://doc.wendoc.com;

strcpy(G->vexs[i].data,e.data);

}

int Locate(MGraph G,VertexType v1)/*确定v1在图顶点中的位置*/

{ int i;

for(i=0;i

if(strcmp(v1.data,G.vexs[i].data)==0) return i;

return -1;

}

void CreateUND(MGraph *G)/*采用数组(邻接矩阵)和邻接表表示无向图*/

{int i,j,k,*p,d,w;

VertexType v1,v2,*q2,*q;

p=(int *)malloc(G->vexnum*sizeof(int));

for(i=0;ivexnum;i++) p[i]=0;

for(i=0;ivexnum;++i)/*初始邻接表*/

{ for(j=0;jvexnum;++j) G->arcs[i][j].adj=0;}

pr

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值