图的拓扑排序

#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>

struct LinkNode
{
    int pos;//邻接点的下标
    LinkNode *next;
};
struct VertextNode
{
    int in;//入度
    int val;//值域
    LinkNode *firstEdge;
};

void InsertLinkNode(LinkNode **l, int val)
{
    LinkNode *tmp = new LinkNode;
    tmp->pos = val;
    tmp->next = *l;
    (*l) = tmp;
}

int TopologicalSort(VertextNode *AdjList, int num)
{
    int count = 0;//节点数
    std::stack<int> myStack;
    for(int i = 0; i < num; ++i)
    {
        if(AdjList[i].in == 0)
        {
            myStack.push(i);
        }
    }
    while(!myStack.empty())
    {
        ++count;
        int k = myStack.top();
        myStack.pop();
        std::cout << k << std::endl;
        while(AdjList[k].firstEdge)
        {
            int j = AdjList[k].firstEdge->pos;
            LinkNode *p = AdjList[k].firstEdge;
            AdjList[k].firstEdge = p->next;
            delete p;
            if((--AdjList[j].in) == 0)
            {
                myStack.push(j);
            }
        }

    }
    if(count == num)
    {
        return 1;
    }
    return 0;
}
int main(int nArgs, char* pArg[])
{
    VertextNode AdjList[14];
    int num = 14;
    for(int i = 0; i < 14; ++i)
    {
        AdjList[i].firstEdge = NULL;
        AdjList[i].val = i;
        switch (i)
        {
        case 0:
            {
                AdjList[i].in = 0;
                InsertLinkNode(&AdjList[i].firstEdge, 4);
                InsertLinkNode(&AdjList[i].firstEdge, 5);
                InsertLinkNode(&AdjList[i].firstEdge, 11);
                break;
            }
        case 1:
            {
                AdjList[i].in = 0;
                InsertLinkNode(&AdjList[i].firstEdge, 2);
                InsertLinkNode(&AdjList[i].firstEdge, 4);
                InsertLinkNode(&AdjList[i].firstEdge, 8);
                break;
            }
        case 2:
            {
                AdjList[i].in = 2;
                InsertLinkNode(&AdjList[i].firstEdge, 5);
                InsertLinkNode(&AdjList[i].firstEdge, 6);
                InsertLinkNode(&AdjList[i].firstEdge, 9);
                break;
            }
        case 3:
            {
                AdjList[i].in = 0;
                InsertLinkNode(&AdjList[i].firstEdge, 2);
                InsertLinkNode(&AdjList[i].firstEdge, 13);
                break;
            }
        case 4:
            {
                AdjList[i].in = 2;
                InsertLinkNode(&AdjList[i].firstEdge, 7);
                break;
            }
        case 5:
            {
                AdjList[i].in = 3;
                InsertLinkNode(&AdjList[i].firstEdge, 8);
                InsertLinkNode(&AdjList[i].firstEdge, 12);
                break;
            }
        case 6:
            {
                AdjList[i].in = 1;
                InsertLinkNode(&AdjList[i].firstEdge, 5);
                break;
            }
        case 7:
            {
                AdjList[i].in = 2;
                break;
            }
        case 8:
            {
                AdjList[i].in = 2;
                InsertLinkNode(&AdjList[i].firstEdge, 7);
                break;
            }
        case 9:
            {
                AdjList[i].in = 2;
                InsertLinkNode(&AdjList[i].firstEdge, 10);
                InsertLinkNode(&AdjList[i].firstEdge, 11);
                break;
            }
        case 10:
            {
                AdjList[i].in = 1;
                InsertLinkNode(&AdjList[i].firstEdge, 13);
                break;
            }
        case 11:
            {
                AdjList[i].in = 2;
                break;
            }
        case 12:
            {
                AdjList[i].in = 1;
                InsertLinkNode(&AdjList[i].firstEdge, 9);
                break;
            }
        case 13:
            {
                AdjList[i].in = 2;
                break;
            }
        default:
            break;
        }
    }
    
    if(TopologicalSort(AdjList, 14))
    {
        std::cout << "无回路" << std::endl;
    }
    else
    {
        std::cout << "有回路" << std::endl;
    }

    system("pause");
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值