-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoposort.c
98 lines (81 loc) · 1.4 KB
/
toposort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<iostream>
#include<cstdlib>
#include<stack>
#include<conio.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
typedef struct node node;
node * create(int d)
{
node *t=(node *)malloc(sizeof(node));
t->data=d;
t->next=NULL;
return t;
}
int a[]={0,0,0,0,0,0};
void topological(node *g[],stack <int> &s,int ver )
{
a[ver]=1;
node *t=g[ver];
while(t!=NULL)
{
t=t->next;
if(a[t->data]==0)
topological(g,s,t->data);
s.push(t->data);
}
}
void visit(node *g[])
{
int i;
stack <int> s;
for(i=0;i<6;i++)
{
if(a[i]!=0)
topological(g,s,i);
}
while(s.empty())
{
printf("%d ",s.pop());
}
}
int main()
{
int i;
node *g[6];
g[0]=create(2);
g[0]->next=create(3);
g[1]=create(1);
g[1]->next=create(4);
g[1]->next->next=create(5);
g[2]=create(1);
g[2]->next=create(5);
g[3]=create(2);
g[3]->next=create(5);
g[3]->next->next=create(6);
g[4]=create(3);
g[4]->next=create(2);
g[4]->next->next=create(4);
g[4]->next->next->next=create(6);
g[5]=create(4);
g[5]->next=create(5);
for(i=0;i<4;i++)
{
node *t=g[i];
cout<<"Node="<<i+1<<"=";
while(t!=NULL)
{
cout<<t->data<<" ";
t=t->next;
}
cout<<"\n";
}
cout<<"\n";
dfs(g);
getch();
return 0;
}