自定义队列的例子
程序员文章站
2022-05-20 21:31:52
...
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[101];
int in[101];
void topsort(int n)
{
int *que;
int next;
que = new int[n + 1];
int font, real;
int m;
font = real = 0;
for (int k = 1; k <= n; k++)
{
if (in[k] == 0)
{
cout<<"in["<<k<<"] is 0"<<endl;
que[real++] = k;
cout<<real<<" is real"<<endl;
// 进去
}
}
while (font != real)
{
m=que[font++];
for( int j=0;j<adj[m].size();j++){
next=adj[m][j];
in[next]--;
cout<< " m is :"<<m<<" __ next is:"<<next<<endl;
if(in[next]==0){
que[real++]=next;
cout<<real<<" is in que"<<endl;
}
}
}
if(real!=n){
cout<<"0";
}
else
cout<<"1";
return ;
}
int main()
{
int n;
int node1, node2;
int num;
cin >> n;
cout<<" n is "<<n<<endl;
for (int i = 1; i <= n; i++)
{
cin >> num;
if (num!=0)
{
for (int j = 1; j <=num; j++)
{
cin >> node1;
in[i]++;
adj[node1].push_back(i);
cout<<i<<" is in adj["<<node1<<"]"<<endl;
}
}
}
topsort(n);
}
本身代码是没有什么好说的,只是在计算in[next]–;时候错误写成了in[next–];
导致出现了段错误, 看来调试是个好东西。
上一篇: 创建一个二维数组,求路线,使得和最小
下一篇: 二叉树的前序遍历(两种方法)