Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to make a program that searches for node with two arches to it. ->()<-
Please help. I have this but I dont know how to ivert it to mine. The following code is for the opposite <-()->.

C++
#include <iostream>
using namespace std;

const int n=8;
struct link
{
	char key;
	link *next;
}*g[n];

void init(link *G[n]);
int search_node(link *G[n], char c);
int search_arc(link *G[n], char c1, char c2);
void add_node(link *G[n], char c);
void add_arc(link *G[n], char c1, char c2);
void list_node(link *G[n]){
	cout<<"\n";
	for(int i=0; i<n; i++){
		if(g[i]){
			cout<<g[i]->key;
		}
	}
}
void main()
{
	
	add_node(g, 'A');
	add_node(g, 'B');
	add_node(g, 'C');
	add_node(g, 'D');
	add_node(g, 'E');
	add_node(g, 'F');
	add_node(g, 'G');
	add_node(g, 'H');


	add_arc(g, 'A', 'B');
	add_arc(g, 'A', 'C');
	add_arc(g, 'H', 'C');
	add_arc(g, 'H', 'D');
	add_arc(g, 'B', 'F');
	add_arc(g, 'B', 'H');
	add_arc(g, 'C', 'D');
	add_arc(g, 'D', 'F');
	add_arc(g, 'E', 'G');
	add_arc(g, 'G', 'E');
	
	
	for(int i=0; i<n; i++)
	{
			/*checks for node with two exiting arches and prints the node */
			if(g[i]->next!=NULL && g[i]->next->next!=NULL)	
					cout<<"\nTwo pair of arc from one node: "<<g[i]->key;
	}
	int tra;
	cin>>tra;
}

void init(link *G[n])
{
	for (int i=0; i<n; i++)
		G[i]=NULL;
}

int search_node(link *G[n], char c)
{
	int flag=0;
	for(int i=0; i<n; i++)
	{
		if(G[i])
			if(G[i]->key==c)
			{
				flag=1;
				break;
			}
	}
	return flag;
}

int search_arc(link *G[n], char c1, char c2)
{
	int flag=0;
	if(search_node(G,c1)&&search_node(G,c2))
	{
		int i=0;
		while(G[i]->key!=c1)i++;
		link *p=G[i];
		while(p->key!=c2&&p->next!=NULL)p=p->next;
		if(p->key==c2)flag=1;
	}
	return flag;
}

void add_node(link *G[n], char c)
{
	if(search_node(G,c))
		cout<<"\nNode exists";
	else
	{
		int j=0;
		while(G[j]&&(j<n))j++;
		if(G[j]==NULL)
		{
			G[j]=new link;
			G[j]->key=c;
			G[j]->next=NULL;
		}
		else cout<<"\noverflow occured";
	}
}

void add_arc(link *G[n], char c1, char c2)
{
	int i=0;
	link *p;
	if(search_arc(G,c1,c2))
		cout<<"\nArc exists";
	else
	{
		if(!search_node(G,c1)) {cout<<"DOBB";add_node(G,c1);}
		if(!search_node(G,c2)) {cout<<"DOBB";add_node(G,c2);}
		while(G[i]->key!=c1) i++;
		p=new link;
		p->key=c2;
		p->next=G[i]->next;
		G[i]->next=p;
	}
}


What I have tried:

Well, the line which should be fixes is with comment but I do not know how to change it. Another thing is, there are functions for creating arches and adding nodes. But the program does not ask me to do it, they are made in the code. Is there a mistake?
Posted
Comments
Sergey Alexandrovich Kryukov 15-May-16 16:47pm    
What does it mean, exactly? Is it a directed graph or not?
—SA
[no name] 16-May-16 11:52am    
It is a directer graph when I search things like -> and <- ..
Do you have any idea?
Sergey Alexandrovich Kryukov 16-May-16 16:40pm    
One thing does not assume another.
—SA
Sergey Alexandrovich Kryukov 15-May-16 16:50pm    
Your major design mistake is the node relationship established by name, which is a character.
Even though such graphs implementation can be developed, the approach is so bad that the rest of the code hardly deserves further consideration. I suggest you fix it, and then try to solve the whole problem again, and then, if you still have problems, we can discuss them.

Second problem is: the code should be re-written to C++. It's not "real" C++ now. Create a class/struct representing the graph. Strictly speaking, it's not a must, but it make maintainability of code unreasonably bad.

—SA
David 'dex' Schwartz 7-Jun-17 7:05am    
Very broken code. Either use a fixed array of link nodes or use the heap but not mix them up the way you currently do.
The question of how to detect ->()<- translates to:
For each node.
Find all links that end at that node.
If it has (at least?) two terminating links then print it.

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