I need help to make this run as a c program. I am better at writing c++ code than I am at writing c code. I had this working as a C++ program and I trying coverting it to C. This program must perform a topologic sort of a graph.
#include<stdlib.h>
#include <stdio.h>
#define MAX_NODE 50
struct node
{
int vertex;
node *next;
};
node *adjacency[MAX_NODE];
int totalNodes;
const int ready=0,wait=1,processed=2;
int status[MAX_NODE];
node *singlelist=NULL;
void createGraph()
{
node *newa,*last;
int friends,friend_value;
printf("Enter total nodes in the graph : ");
scanf("%d",&totalNodes);
for(int i=1;i<=totalNodes;i++)
{
last=NULL;
printf("\nEnter number of nodes in the list of node "<<i<<"\n");
printf("--> That are Friends of "<< i <<" : ");
scanf("%d",&friends);
for(int t=1;t<=friends;t++)
{
printf("Enter friend #"<<t<<" : ");
scanf("%d",&friend_value);
newa=new node;
newa->vertex=friend_value;
newa->next=NULL;
if(adjacency[i]==NULL)
adjacency[i]=last=newa;
else
{
last->next = newa;
last = newa;
}
}
}
}
void dfsVisit(int i)
{
node *tmp,*newa;
int v;
tmp=adjacency[i];
while(tmp != NULL)
{
v=tmp->vertex;
if(status[v]==ready)
dfsVisit(v);
tmp=tmp->next;
}
newa=new node;
newa->vertex=i;
newa->next=singlelist;
singlelist=newa;
status[i]=processed;
}
void topologicalSort()
{
int i;
for(i=1;i<=totalNodes;i++)
status[i]=ready;
for(i=1;i<=totalNodes;i++)
{
if(status[i]==ready)
dfsVisit(i);
}
//print list
while(singlelist!=NULL)
{
printf(" "<<singlelist->vertex);
singlelist=singlelist->next;
}
}
void main()
{
createGraph();
topologicalSort();
getch();
}
The errors were:
Error # Error Description Line #
Error 1 error C2061: syntax error : identifier 'node' 9
Error 2 error C2059: syntax error : '}' 10
Error 3 error C2143: syntax error : missing '{' before '*' 12
Error 4 error C2143: syntax error : missing '{' before '*' 16
Error 5 error C2065: 'node' : undeclared identifier 21
Error 6 error C2065: 'newa' : undeclared identifier 21
Error 7 error C2065: 'last' : undeclared identifier 21
Error 8 error C2100: illegal indirection 21
Error 9 error C2143: syntax error : missing ';' before 'type'22
Error 10 error C2143: syntax error : missing ';' before 'type'25
Error 11 error C2143: syntax error : missing ';' before 'type'25
Error 12 error C2143: syntax error : missing ')' before 'type'25
Error 13 error C2143: syntax error : missing ';' before 'type'25
Error 14 error C2065: 'i' : undeclared identifier 25
Warning 15 warning C4552: '<=' : operator has no effect; 25
expected operator with side-effect
Error 16 error C2065: 'i' : undeclared identifier 25
Error 17 error C2059: syntax error : ')' 25
Error 18 error C2143: syntax error : missing ';' before '{' 26
Error 19 error C2065: 'last' : undeclared identifier 27
Warning 20 warning C4047: '=' : 'int' differs in levels 27
of indirection from 'void *'
Error 21 error C2065: 'i' : undeclared identifier 28
Error 22 error C2296: '<<' : illegal, left operand has 28 type 'char [44]'
Error 23 error C2198: 'printf' : too few arguments for call 28
Error 24 error C2065: 'i' : undeclared identifier 29
Error 25 error C2296: '<<' : illegal, left operand has 29 type 'char [25]'
Error 26 error C2198: 'printf' : too few arguments for call 29
Error 27 error C2065: 'friends' : undeclared identifier 30
Error 28 error C2143: syntax error : missing ';' 31 before 'type'
Error 29 error C2143: syntax error : missing ';' 31 before 'type'
Error 30 error C2143: syntax error : missing ')' 31 before 'type'
Error 31 error C2143: syntax error : missing ';' 31 before 'type'
Error 32 error C2065: 't' : undeclared identifier 31
Error 33 error C2065: 'friends' : undeclared identifier 31
Warning 34 warning C4552: '<=' : operator has no effect; 31
expected operator with side-effect
Error 35 error C2065: 't' : undeclared identifier 31
Error 36 error C2059: syntax error : ')' 31
Error 37 error C2143: syntax error : missing ';' before '{' 32
Error 38 error C2065: 't' : undeclared identifier 33
Error 39 error C2296: '<<' : illegal, left operand has 33 type 'char [15]'
Error 40 error C2198: 'printf' : too few arguments for call 33
Error 41 error C2065: 'friend_value' : undeclared identifier 34
Error 42 error C2065: 'newa' : undeclared identifier 35
Error 43 error C2065: 'new' : undeclared identifier 35
Error 44 error C2146: syntax error : missing ';' before 35 identifier 'node'
Error 45 error C2065: 'node' : undeclared identifier 35
Error 46 error C2065: 'newa' : undeclared identifier 36
Error 47 error C2223: left of '->vertex' must 36 point to struct/union
Error 48 error C2065: 'friend_value' : undeclared identifier 36
Error 49 error C2065: 'newa' : undeclared identifier 37
Error 50 error C2223: left of '->next' must point to 37 struct/union
Error 51 error C2065: 'i' : undeclared identifier 38
Error 52 error C2065: 'i' : undeclared identifier 39
Error 53 error C2065: 'last' : undeclared identifier 39
Error 54 error C2065: 'newa' : undeclared identifier 39
Warning 55 warning C4047: '=' : 'int *' differs in levels 39
of indirection from 'int'
Error 56 error C2065: 'last' : undeclared identifier 42
Error 57 error C2223: left of '->next' must point 42 to struct/union
Error 58 error C2065: 'newa' : undeclared identifier 42
Error 59 error C2065: 'last' : undeclared identifier 43
Error 60 error C2065: 'newa' : undeclared identifier 43
Error 61 error C2065: 'node' : undeclared identifier 52
Error 62 error C2065: 'tmp' : undeclared identifier 52
Error 63 error C2065: 'newa' : undeclared identifier 52
Error 64 error C2100: illegal indirection 52
Error 65 error C2143: syntax error : missing ';' 53 before 'type'
Error 66 error C2065: 'tmp' : undeclared identifier 55
Warning 67 warning C4047: '=' : 'int' differs in levels 55
of indirection from 'int *'
Error 68 error C2065: 'tmp' : undeclared identifier 56
Warning 69warning C4047: '!=' : 'int' differs in levels 56
of indirection from 'void *'
Error 70 error C2065: 'v' : undeclared identifier 58
Error 71 error C2065: 'tmp' : undeclared identifier 58
Error 72 error C2223: left of '->vertex' must point 58 to struct/union
Error 73 error C2065: 'v' : undeclared identifier 59
Error 74 error C2065: 'v' : undeclared identifier 60
Error 75 error C2065: 'tmp' : undeclared identifier 61
Error 76 error C2065: 'tmp' : undeclared identifier 61
Error 77 error C2223: left of '->next' must point 61 to struct/union
Error 78 error C2065: 'newa' : undeclared identifier 64
Error 79 error C2065: 'new' : undeclared identifier 64
Error 80 error C2146: syntax error : missing ';' before 64 identifier 'node'
Error 81 error C2065: 'node' : undeclared identifier 64
Error 82 error C2065: 'newa' : undeclared identifier 65
Error 83 error C2223: left of '->vertex' must point 65 to struct/union
Error 84 error C2065: 'newa' : undeclared identifier 66
Error 85 error C2223: left of '->next' must point 66 to struct/union
Error86 error C2065: 'newa' : undeclared identifier 67
Warning 87 warning C4047: '=' : 'int *' differs in levels 67
of indirection from 'int'
Error 88 error C2223: left of '->vertex' must point 89 to struct/union
Error 89 error C2198: 'printf' : too few arguments for call 89
Error 90 error C2223: left of '->next' must point 90 to struct/union
Warning 91 warning C4013: 'getch' undefined; assuming 100
extern returning int