修改下列代码错误#include<bits/stdc++.h> using namespace std; char s[13][20]={0}; struct ArcNode { int adjest; ArcNode *next; }; typedef struct { int vertex; int count; ArcNode *firstedge; } VNode; class AdjGraph { private: int VertexNum; int ArcNum; public: VNode adjlist[100]; AdjGraph(int a[],int n,int e); ~AdjGraph(); }; AdjGraph::AdjGraph(int a[],int n,int e) { int i,j,k; VertexNum=n; ArcNum=e; for(i=1;i<=VertexNum;i++) { adjlist[i].vertex=a[i]; adjlist[i].firstedge=NULL; } int q; for(k=0;k<ArcNum;k++) { char s2[20]={0}; char s3[20]={0}; char s0[20]={0}; cin>>s2; cin>>s3; for(q=1;q<=n;q++) { s0=s[q]; if(strcmp(s0,s2)==0) i=q; if(strcmp(s0,s3)==0) j=q; } ArcNode *s=new ArcNode; s->adjest=j; s->next=adjlist[i].firstedge; adjlist[i].firstedge=s; } } AdjGraph::~AdjGraph() { } void TopSort(AdjGraph *G,int n) { int i,j,l=0; int b[100]={0}; int St[100],top=-1; ArcNode p; for (i=1;i<=n;i++) G->adjlist[i].count=0; for (i=1;i<=n;i++) { p=G->adjlist[i].firstedge; while (p!=NULL) { G->adjlist[p->adjest].count++; p=p->next; } } for (i=n;i>0;i--) if (G->adjlist[i].count==0) { top++; St[top]=i; } while (top>-1) { i=St[top];top--; b[l]=i; l++; p=G->adjlist[i].firstedge; while (p!=NULL) { j=p->adjest; G->adjlist[j].count--; if (G->adjlist[j].count==0) { top++; St[top]=j; } p=p->next; //找下一个邻接点 } } if(l!=n) { cout<<"False"; } else { for(i=0;i<l;i++) { cout<<s[b[i]]<<" "; } } } int main() { int n,e,i; ArcNode p; cin>>n>>e; char s1[20]={0}; for(i=1;i<=n;i++) { cin>>s1; s[i]=s1; } int a[100]={0}; for(i=1;i<=n;i++) { a[i]=i; } AdjGraph A(a,n,e); / for(i=1;i<=n;i++) { cout<<A.adjlist[i].vertex<<"--->"; p=A.adjlist[i].firstedge; while(p!=NULL) { cout<<p->adjest<<"--->"; p=p->next; } cout<<endl; }/ AdjGraph *G=&A; TopSort(G,n); }
时间: 2023-12-03 12:02:55 浏览: 144
C++万能头: bits/stdc++.h 下载
这段代码存在多处错误,包括:
1. 包含了不必要的头文件 `<bits/stdc++.h>`,应该只包含需要的头文件。
2. `char s[13][20]={0}` 中,应该使用 `'\0'` 表示空字符,而不是 `0`。
3. `ArcNode *s=new ArcNode` 中,应该使用 `ArcNode* s=new ArcNode`。
4. `p->adjest` 中,应该使用 `p->adjvex`。
5. `//找下一个邻接点` 应该换行。
以下是修改后的代码:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
char s[13][20] = {'\0'};
struct ArcNode {
int adjvex;
ArcNode* next;
};
struct VNode {
int vertex;
int count;
ArcNode* firstedge;
};
class AdjGraph {
private:
int VertexNum;
int ArcNum;
public:
VNode adjlist[100];
AdjGraph(int a[], int n, int e);
~AdjGraph();
};
AdjGraph::AdjGraph(int a[], int n, int e) {
int i, j, k;
VertexNum = n;
ArcNum = e;
for (i = 1; i <= VertexNum; i++) {
adjlist[i].vertex = a[i];
adjlist[i].firstedge = NULL;
}
int q;
for (k = 0; k < ArcNum; k++) {
char s2[20] = {'\0'};
char s3[20] = {'\0'};
char s0[20] = {'\0'};
cin >> s2;
cin >> s3;
for (q = 1; q <= n; q++) {
strcpy(s0, s[q]);
if (strcmp(s0, s2) == 0) i = q;
if (strcmp(s0, s3) == 0) j = q;
}
ArcNode* s = new ArcNode;
s->adjvex = j;
s->next = adjlist[i].firstedge;
adjlist[i].firstedge = s;
}
}
AdjGraph::~AdjGraph() {}
void TopSort(AdjGraph* G, int n) {
int i, j, l = 0;
int b[100] = {0};
int St[100], top = -1;
ArcNode* p;
for (i = 1; i <= n; i++) G->adjlist[i].count = 0;
for (i = 1; i <= n; i++) {
p = G->adjlist[i].firstedge;
while (p != NULL) {
G->adjlist[p->adjvex].count++;
p = p->next;
}
}
for (i = n; i > 0; i--)
if (G->adjlist[i].count == 0) {
top++;
St[top] = i;
}
while (top > -1) {
i = St[top];
top--;
b[l] = i;
l++;
p = G->adjlist[i].firstedge;
while (p != NULL) {
j = p->adjvex;
G->adjlist[j].count--;
if (G->adjlist[j].count == 0) {
top++;
St[top] = j;
}
p = p->next; //找下一个邻接点
}
}
if (l != n) {
cout << "False";
} else {
for (i = 0; i < l; i++) {
cout << s[b[i]] << " ";
}
}
}
int main() {
int n, e, i;
ArcNode* p;
cin >> n >> e;
char s1[20] = {'\0'};
for (i = 1; i <= n; i++) {
cin >> s1;
strcpy(s[i], s1);
}
int a[100] = {0};
for (i = 1; i <= n; i++) {
a[i] = i;
}
AdjGraph A(a, n, e);
// for(i=1;i<=n;i++) {
// cout<<A.adjlist[i].vertex<<"--->";
// p=A.adjlist[i].firstedge;
// while(p!=NULL) {
// cout<<p->adjvex<<"--->";
// p=p->next;
// }
// cout<<endl;
// }
AdjGraph* G = &A;
TopSort(G, n);
return 0;
}
```
阅读全文