Hi, I am a newbie to coding. I am trying to implement a graph in the following format.
User input:
v1 v2 edge_cost
1 3 10
1 2 20
2 3 40
3 1 30
I am storing the graph as an adjacency list, so that the output should be the following:
1 -- 3,2
2 -- 3
3 -- 1
It works fine for small inputs but whenever i pass more edges to it i get an error
"Unhandled exception at 0x010b1d2d in CppPractise.exe: 0xC0000005: Access violation reading location 0xfeeefeee."
Can anyone point out how i can correct this?
class graph
{
private:
struct listNode
{
int v;
int e;
listNode *next;
listNode *down;
};
listNode *head;
public:
graph()
{
head = NULL;
}
void inpUsrGraph();
void inpGraph(int ,int , int );
void dispGraph();
};
void graph::inpUsrGraph()
{
int fn_exit=0;
int v1,v2,e;
string do_exit;
cout<<"Enter the graph inputs as V1 V2 E (* to exit)\n";
do{
cin>>do_exit;
if(do_exit.compare("*")==0)
fn_exit=1;
else
{
v1=atoi(do_exit.c_str());
cin>>do_exit;
if(do_exit.compare("*")==0)
fn_exit=1;
else
{
v2=atoi(do_exit.c_str());
cin>>do_exit;
if(do_exit.compare("*")==0)
fn_exit=1;
else
e=atoi(do_exit.c_str());
}
}
if(!fn_exit)
inpGraph(v1,v2,e);
}while(!fn_exit);
}
void graph::inpGraph(int v1,int v2,int cost)
{
listNode *newNode = NULL;
listNode *downNode = NULL;
listNode *nodePtr = NULL;
listNode *downPtr = NULL;
newNode = new listNode;
newNode->v=v1;
newNode->e=-1;
newNode->next=NULL;
newNode->down=NULL;
downNode = new listNode;
downNode->v=v2;
downNode->e=cost;
downNode->down=NULL;
downNode->next=NULL;
if(!head)
{
head=newNode;
head->down=downNode;
}
else
{
nodePtr = head;
do
{
if(newNode->v==nodePtr->v)
{
delete newNode;
newNode=nodePtr;
downPtr=newNode->down;
do
{
if(downPtr->v==downNode->v)
{
delete downNode;
break;
}
else if(downPtr->down==NULL)
{
downPtr->down=downNode;
downPtr=downNode;
}
downPtr=downPtr->down;
}while(downPtr);
}
else if(nodePtr->next==NULL)
{
nodePtr->next=newNode;
newNode->down=downNode;
nodePtr=newNode;
}
nodePtr=nodePtr->next;
}while(nodePtr);
}
}
void graph::dispGraph()
{
listNode *nextPtr;
listNode *nextDown;
nextPtr=head;
while(nextPtr)
{
cout<<nextPtr->v<<" -- ";
nextDown=nextPtr->down;
while(nextDown)
{
cout<<" "<<nextDown->v;
nextDown=nextDown->down;
}
cout<<endl;
nextPtr=nextPtr->next;
}
}