拓扑排序

//Stack.h
#ifndef STACK_H
#define STACK_H

class Stack{
public:

    Stack(int n = 10);
    bool push(int a);
    int pop();
    bool isfull();//判断是否满
    bool isempty();
private:
    int size;//表示最大容量
    int *base, *top;//top指向栈顶前一个元素
};
#endif
//Topo.h
#ifndef TUOPU_H
#define TUOPU_H

class Node{
public:
    int vexNum;
    Node* next;

    Node(int vex = 0, Node* a=NULL);
};

class MGraph{
//图的储存用邻接表,要求入度
public:
    int Vex;
    Node* vertices;

    MGraph(int num = 0);
    bool Inition();

};

class ToPo{
    /*算法:
        1.计算每个顶点的入度(初始化)
          初始化栈
        2.将入度为0的点压栈
        3.弹栈,并将以该顶点为弧尾的顶点入度-1
        4.重复2,3,直到栈空(有可能顶点都遍历,有可能存在环)
    */
public:
    bool TopologicalSort(const MGraph g);
};

#endif
//Stack.cpp
#include"Stack.h"

#include
using namespace std;

Stack::Stack(int n){
    size = n;
    top = base = new int[size];
}

bool Stack::push(int a){
    *top = a;
    top++;
    return true;
}

int Stack::pop(){
    /*if (top == base){
    exit(0);
    }*/
    return *(--top);
}

bool Stack::isfull(){
    if (top - base >= size)
        return true;
    else
        return false;
}

bool Stack::isempty(){
    if (top == base)
        return true;
    else
        return false;
}
//ToPo.cpp
#include
#include"Topo.h"
#include"Stack.h"
using  namespace std;

//Node
Node::Node(int vex , Node* a ){
    vexNum = vex;
    next = a;
}


//MGraph
MGraph::MGraph(int num){
    Vex = num;
    vertices = new Node[Vex]();
    /*for (int i = 0; i < Vex; i++){
        cout << vertices[i].vexNum;
    }
    cout << endl;*/
}


bool MGraph::Inition(){
    if (!Vex) return false;
    int b, e, value;
    while (cin >> b&&b){
        cin >> e ;
        Node* p = vertices[b-1].next;
        vertices[b-1].next = new Node(e-1,p);//前插
        vertices[e-1].vexNum++;//记录入度
    }
    /*for (int i = 0; i < Vex; i++){
        for (Node* p = vertices[i].next; p; p = p->next){
            cout << p->vexNum << "  ";
        }
        cout << endl;
    }*/
    return true;
}

//Topo
    /*算法:
    1.计算每个顶点的入度(初始化)
    初始化栈
    2.将入度为0的点压栈
    3.弹栈,并将以该顶点为弧尾的顶点入度-1
    4.重复2,3,直到栈空(有可能顶点都遍历,有可能存在环)
    */
bool ToPo::TopologicalSort(const MGraph g){
    //如果最后有环,输出false,如果全部顶点都输出,则返回true
    Stack S(g.Vex);
    int *count = new int[g.Vex];
    for (int i = 0; i < g.Vex; i++){
        count[i] = g.vertices[i].vexNum;
        if (!count[i])
            S.push(i);
    }
    int flag = 0;
    while (!S.isempty()){
        int i = S.pop(); flag++;
        for (Node* p = g.vertices[i].next; p; p = p->next){
            if (--count[p->vexNum] == 0){
                S.push(p->vexNum);
            }
        }
        cout << i+1<< "  ";
    }
    if (flag == g.Vex)
        return true;
    else
        return false;

}
//main.cpp
#include
#include"Topo.h"
using namespace std;


int main(){
    int n;
    cout << "Please input the Vex-Number...." << endl;
    cin >> n;
    MGraph g(n);
    g.Inition();
    ToPo p;
    if(!p.TopologicalSort(g))
        cout<<"with cycle....."<"pause");
    return 0;
}


/*测试数据
13
1 2
1 6
1 7
3 1
3 4
4 6
6 5
7 5
7 10
8 7
9 8
10 11
10 12
10 13
12 13
*/

你可能感兴趣的:(数据结构,拓扑排序)