Click here to Skip to main content
15,920,633 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Following is a C++ program. Please help me to convert it into C. It also need to be linux compatible.

What I have tried:

<pre>// A C++ program to print topological sorting of a DAG 
#include <iostream> 
#include <list> 
#include <stack> 
using namespace std; 

class Graph 
	int V; 

	list<int> *adj; 

	void topologicalSortUtil(int v, bool visited[], stack<int> &Stack); 
	Graph(int V); 
	void addEdge(int v, int w); 

	void topologicalSort(); 

Graph::Graph(int V) 
	this->V = V; 
	adj = new list<int>[V]; 

void Graph::addEdge(int v, int w) 
void Graph::topologicalSortUtil(int v, bool visited[], 
								stack<int> &Stack) 
	visited[v] = true; 

	list<int>::iterator i; 
	for (i = adj[v].begin(); i != adj[v].end(); ++i) 
		if (!visited[*i]) 
			topologicalSortUtil(*i, visited, Stack); 


void Graph::topologicalSort() 
	stack<int> Stack; 

	// Mark all the vertices as not visited 
	bool *visited = new bool[V]; 
	for (int i = 0; i < V; i++) 
		visited[i] = false; 
	for (int i = 0; i < V; i++) 
	if (visited[i] == false) 
		topologicalSortUtil(i, visited, Stack); 

	while (Stack.empty() == false) 
		cout << << " "; 

int main() 
	int n,v1,v2;
	Graph g(n); 
	for(int i=1;i<=n;i++)
    {	cin>>v1>>v2;
	g.addEdge(v1, v2);} 

	cout << " Topological Sort of the given graph \n"; 

	return 0; 
Updated 30-Dec-22 15:27pm

This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it’ll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.

And to be honest, you'd have to do so much work to get the C++ specific features of that code working in C that your homework deadline would have been long gone by the time you finished. And the code would probably be several hundred times larger than the relevant "Native C" program.
Share this answer
This is not an exercise for a beginner, so I assume you have some experience. This site[^] should prove helpful, because it covers both the C++ and C libraries.

Each #include is from the C++ STL, so you need to make the code work after removing them. You therefore need C equivalents of iostream, stack, list, and (for allocating memory) new.

The C equivalent of iostream is stdio.h, where you need to find replacements for cin and cout.

The C equivalent of new is malloc. The original code doesn't bother to delete[] the arrays that it allocates, but the C equivalent for that is free if you want to add it as an enhancement.

You should have enough experience to implement a stack and a list (a two-way linked list). Your stack needs to support the functions empty, top, push, and pop. Your list needs to support the functions push_back, begin, and end. Curiously, the original code could have used vector, which also supports those functions but is simpler (just a dynamically expandable array).

I see nothing that is specific to any operating system, so your code should work fine on Linux, Windows, or wherever once you've completed the work just outlined.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900