recategorized by
490 views
0 votes
0 votes
//Just check create_graph function.

#include <iostream>
#include <malloc.h>
using namespace std;
struct Adj_node
{
    int node;
    struct Adj_node *next;
};
typedef struct Adj_node Adj_node;

struct Edge
{
    int src, dest;
};
typedef struct Edge Edge;

struct Graph
{
    int v, e;
    Edge *edge;
};
typedef struct Graph Graph;

Adj_node **adj = NULL;
Graph G;
Graph *graph=&G;

//Forward declarations of all functions used:-
void create_graph(int , int );
void create_adjacency_list();
void insert_node_at_index(int , int );
void print_adj_list();


//Functions definition starts from here:-
void create_graph(int v, int e)
{
    //cout<<"1";   //Error Checkpoint
    graph->v = v;
    graph->e = e;
    //cout<<"2";  //Error Checkpoint
    Edge *temp = (Edge *)malloc(sizeof(Edge)*e);
    graph->edge = temp;
    for(int i=0;i<e;i++)                 
    {                                   
        cout<<"Enter the source of edge "<<i+1<<endl;
        cin>>graph->edge[i].src;
        cout<<"Enter the destination of edge "<<i+1<<endl;
        cin>>graph->edge[i].dest;
        cout<<endl;
    }
    for(int i=0;i<e;i++)       //showing segmentation fault when i reaches to i= e-2 
    {                          //For example if e = 3, then when i = 2 then just after writing
        cout<<"the source of edge: "<<i+1<<" is: ";       // "the destination of edge 3 is <vertex no>"
        cout<<graph->edge[i].src;                         // showing segmentation fault at this moment
        cout<<endl;
        cout<<"the destination of edge: "<<i+1<<" is: ";  
        cout<<graph->edge[i].dest;
        cout<<endl;
    }
    //cout<<"1";   //Error Checkpoint
    create_adjacency_list();
}

void create_adjacency_list()
{
    adj = (Adj_node **)malloc(G.v*sizeof(Adj_node *));
    Adj_node *temp;
    int a, b;
    for(int i=0; i<G.v ; i++)
    {
        a = graph->edge[i].src;
        b = graph->edge[i].dest;
        insert_node_at_index(a, b);
    }
}

void insert_node_at_index(int x, int y)
{
    Adj_node *temp1, *temp2;
    temp1->node = x;
    temp2->node = y;
    if(*(adj+x)==NULL)
        *(adj+x) = temp2;
    else
    {
        Adj_node *temp;
        temp = *(adj+x);
        while(temp->next != NULL)
            temp = temp->next;
        temp->next = temp2;
    }
    if(*(adj+y)==NULL)
        *(adj+y) = temp1;
    else
    {
        Adj_node *temp;
        temp = *(adj+y);
        while(temp->next != NULL)
            temp = temp->next;
        temp->next = temp1;
    }
}

void print_adj_list()
{
    for(int i=0;i<graph->v;i++)
    {
        if(*(adj+i)==NULL)
            cout<<"node "<<i<<" has no adjacent";
        else
        {
            cout<<"nodes adjacent to node "<<i<<" are as follow:-"<<endl;
            Adj_node *temp = *(adj+i);
            while(temp!=NULL)
            {
                cout<<"node "<<(*(adj+i))->node<<"\t";
            }
        }
        cout<<endl;
    }
}
int main ()
{
    int v, e;
    cout<<"Enter the number of vertices:"<<endl;
    cin>>v;
    cout<<"Enter the number of edges:"<<endl;
    cin>>e;
    create_graph(v, e);
    print_adj_list();
    return 0;
}

How to get rid of this problem?

recategorized by

Please log in or register to answer this question.

Related questions

0 votes
0 votes
0 answers
2
0 votes
0 votes
1 answer
4
deepak_8404 asked Oct 1, 2023
290 views
Consider a lower triangular matrix stored in row major order as p[-25 - - + 749][-25 - - - + 749] with base address = 6800, size of each element = 6 byte. Find the value ...