| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦   
共有 426 人关注过本帖
标题:图的拓扑排序问题
收藏  订阅  推荐  打印
wangyinshiwo
Rank: 2
等级:注册会员
帖子:72
积分:1142
注册:2007-11-9
图的拓扑排序问题

/* Note:Your choice is C IDE */
#include "stdio.h"
#include<malloc.h>
#define MAX 100
typedef struct edgesnode
{
    int adjvex;
    int value;
    struct edgesnode *next;
}arcnode;
typedef struct vexnode
{
    char data;
    int count;
    arcnode *firsthead;
}headnode;
typedef struct
{
    int n,e;
    headnode adjlist[MAX];
}graphlist;
int creatgraph(graphlist *g)
{
    int i,b,t,w;
    arcnode *p;
    printf("顶点数和边数:");
    scanf("%d%d",&g->n,&g->e);
    for(i=0;i<g->n;i++)
    {
        getchar();
        printf("序号为%d的顶点信息:",i);
        scanf("%c",&g->adjlist[i].data);
        g->adjlist[i].firsthead=NULL;
    }
    for(i=0;i<g->e;i++)
    {
        printf("序号为%d的边=>",i);
        printf("起点号 终点号 权值:");
        scanf("%d%d%d",&b,&t,&w);
        if(b<g->n&&t<g->n&&w>0)
        {
            p=(arcnode *)malloc(sizeof(arcnode));
            p->value=w;p->adjvex=t;
            p->next=g->adjlist[b].firsthead;
            g->adjlist[b].firsthead=p;
        }
        else
        {
            printf("输入错误:");
            return 0;
        }
    }
    return 1;
}
void topsort(headnode adj[],int n)
{
    int i,j;
    int st[MAX],top=0;
    arcnode *p;
    for(i=0;i<n;i++)
    if(adj[i].count==0)
    { top++;st[top]=i; }
    while(top!=0)
    {
        i=st[top];top--;
        printf("%d",i);
        p=adj[i].firsthead;
        while(p)
        {
            j=p->adjvex;
            adj[j].count--;
            if(adj[j].count==0)
            { top++;st[top]=j; }
            p=p->next;
        }
    }
}
void main()
{
    graphlist g;
    headnode adj[MAX];
    creatgraph(&g);
    topsort(adj,g.n);
}
大家帮我看看应该怎么改才能运行。
2008-6-15 16:08
wangyinshiwo
Rank: 2
等级:注册会员
帖子:72
积分:1142
注册:2007-11-9

怎么没有人啊!自己给自己顶一个。

抽刀断水水更流,举杯消愁愁更愁。
2008-6-16 23:55
共有 425 人关注过本帖
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.048461 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved