#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;
};
#endif
#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{
public:
bool TopologicalSort(const MGraph g);
};
#endif
#include"Stack.h"
#include<iostream>
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(){
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;
}
#include<iostream>
#include"Topo.h"
#include"Stack.h"
using namespace std;
Node::Node(int vex , Node* a ){
vexNum = vex;
next = a;
}
MGraph::MGraph(int num){
Vex = num;
vertices = new Node[Vex]();
}
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++;
}
return true;
}
bool ToPo::TopologicalSort(const MGraph g){
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;
}
#include<iostream>
#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....."<<endl;
system("pause");
return 0;
}