Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8256523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:53:00+00:00 2026-06-08T01:53:00+00:00

I am new to graphs and I am trying to write very simple programs

  • 0

I am new to graphs and I am trying to write very simple programs in graph. I have written two functions, one of which creates an empty graph with as many vertices as inputted by the user. The other one, adds a directed edge between two of the vertices.


The former executes successfully, but the later does not. The program stops running, however the code compiles successfully.


#include <stdio.h>
#include <stdlib.h>
#define MAX 1000

struct node
{
    int data;
    struct node *next;
};

struct node *arr[MAX];

void createEmptyGraph(int n)
{
    // n is the number of vertices
    int i;

    for(i=0;i<n;i++)
    {
       arr[i]=NULL;
    }

    printf("\nAn empty graph with %d vertices has been created",n);
    printf("\nNo edge is connected yet");
}

void addNode(int startVertex,int endVertex)
{
    // For directed edges
    struct node *n;
    n=(struct node *)malloc(sizeof(struct node));
    n->next=arr[startVertex];
    arr[startVertex]->next=n;

    printf("\nAn edge between directed from %d to %d has been   added",startVertex,endVertex);
}

int main(void)
{
    int num;

    printf("Enter the number of vertices in the graph: ");
    scanf("%d",&num);

    createEmptyGraph(num);
    addNode(0,1);

    return 0;
}

I am using adjacency list representation of graphs. Please point out the errror(s).


One more thing, in the createEmptyGraph() method, why can’t I do something like this


for(i=0;i<n;i++)
{
    arr[i]->data=d;
    arr[i]->next=NULL;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:53:02+00:00Added an answer on June 8, 2026 at 1:53 am

    You declare an array of pointers:

    struct node *arr[MAX];
    

    In your createEmptyGraph() method, you need to allocate each structure in the array first – instead you are setting simply the pointer to null..

    for(i=0;i<n;i++)
    {
        arr[i]=NULL;
    }
    

    The following won’t work because you’ve not allocated each entry:

    for(i=0;i<n;i++)
    {
        arr[i]->data=d;
        arr[i]->next=NULL;
    }
    

    Allocate first (malloc()) and then you can set as above…

    So, because of the fact that you’ve not allocated, the following will not work:

    n->next=arr[startVertex]; // this is okay, you've set it to NULL
    arr[startVertex]->next=n; // ERROR: you are accessing a NULL pointer!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry I am very new to open graph and have been having difficulty to
I'm trying to write a simple WPF app that has two ellipses, joined by
So I'm trying to use the new batches functionality to the graph API, which
I am trying to write a code to generate a graph like this: http://www.mathgoodies.com/lessons/graphs/images/line_example1.jpg
I am trying to integrate the new Open Graph objects , actions and aggregations
I accidentally deleted node 0, which was my reference node on a new graph,
new to c#. I'm trying to make a simple system where I can search
I'm trying to familiarize myself with Facebook's new Graph API and so far I
I am struggling with the new graph api, trying to publis stream with flash
I started playing with the new graph api with the python sdk. I'm trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.