I am trying to understand how can I make a graph in C.
So I wrote some program.
#include "declarations.h"
graph root = NULL;
int main()
{
cgraph();
}
void cgraph(void)
{
int n, choice, dir, count,sib;
choice = 1;
count = 1;
graph priv, temp;
printf("Printf we are making a graph the first is root node\n");
while (choice == 1) {
count++;
if (count == 1) {
printf("This is going to be root node \n");
scanf("%d", &n);
root = cnode(n);
count--;
priv = root;
} //ending if
else {
printf("Enter the number of siblings to this node");
scanf("%d",&sib);
while (sib >0)
{
printf ("Enter direction you want to go LEFT 1 RIGHT 2 TOP 3 DOWN 4\n");
scanf("%d", &dir);
printf("Enter the data for graph node\n");
scanf("%d", &n);
temp = cnode(n);
if (dir == 1) {
priv->LEFT = temp;
}
if (dir == 2) {
priv->RIGHT = temp;
}
if (dir == 3) {
priv->TOP = temp;
}
if (dir == 4) {
priv->DOWN = temp;
}
sib--;
} //ending while of sibling
priv = temp; //priv is equal to which temp?
} //ending else
printf ("Enter 1 to continue adding nodes to graph any thing else would take you out\n");
scanf("%d", &choice);
} //ending while
} //ending main
graph cnode(int data)
{
graph temp = (graph) malloc(sizeof(graph));
temp->data = data;
temp->LEFT = NULL;
temp->RIGHT = NULL;
temp->TOP = NULL;
temp->DOWN = NULL;
temp->color = -1;
return temp;
}
qnode travel_graph(graph rh)
{
qnode qt;
if(rh->LEFT)
{
qt= cque(rh);
}
if(rh->RIGHT)
{
qt=cque(rh);
}
if(rh->TOP)
{
qt=cque(rh);
}
if(rh->DOWN)
{
qt=cque(rh);
}
return qt;
}
the problem is I am not getting as what do I need to be able to do so that addition of node is easy.
The problem with above sort of code is it will add a node
A->B
but not
A->B
|
v
C
I thought of a recursive function but not clear as what do I write for this.
Can any one suggest any thing?