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

  • SEARCH
  • Home
  • 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 8667801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:03:45+00:00 2026-06-12T18:03:45+00:00

We are told our input file would be a simple list of numbers: 1

  • 0

We are told our input file would be a simple list of numbers:

1 3 4

2 3

3 4

4 1 2

Where the first number is the source node, and the proceeding numbers are it’s adjacent nodes.

I am trying to figure out how to best store this.
I wanted to firstly initialize a “graph”, an array that contains all these nodes.
Then upon reading the file, line by line, I would store the root node into the graph array, and then update the node’s outlist (adjacent nodes) with the following numbers until we reach the end of the line, repeating this for each line until EOF.

However I’m struggling on how to initialize the graph, do I just assume a certain size and realloc() once the size is hit? Do I read the file first and count the number of lines to find out the size, then re-read the file to store the nodes? Is there any other way?

Here is the code for my data structures:

int initialize (Graph *mygraph, int MaxSize) {
  mygraph->MaxSize = MaxSize;
  mygraph->table = (Node *)malloc(sizeof(Node) * MaxSize);
  return 0;
}

  int insert_node (Graph *mygraph, int n, char *name) {
  mygraph->table[n].name = strdup(name);
  mygraph->table[n].outdegree = 0;
  return 0;
}

  int insert_link (Graph *mygraph, int source, int target) {
  List *newList = (List *)malloc(sizeof(List));
  newList->index = target;
  newList->next = mygraph->table[source].outlist;
  mygraph->table[source].outlist = newList;
  return 0;
}

So upon reading the file,

  1. I initialize the graph.

  2. I read the first number, store it as a new graph node.

  3. I read the next numbers until hitting “\n”, and store these as graph links to the above root node.

  4. I do this for each line until hitting EOF.

As you can see I have no idea what the “MaxSize” until the whole file is read.

Thanks!
I’m rather new to C so sorry if I’ve done anything silly.

  • 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-12T18:03:46+00:00Added an answer on June 12, 2026 at 6:03 pm

    You could have some initial guess for MaxSize (e.g. 8) and grow when needed your data (perhaps by graph->MaxSize += graph->MaxSize/2) using realloc, or just by malloc-ing a bigger new chunk, copying the older chunk inside, then free-ing that older chunk). Don’t forget to check the successful result of any malloc or calloc or realloc call, they could (rarely) fail.

    Notice that I have no idea of how your Graph and Node type is declared (just guessing).

    I am assuming and guessing you have declared something like

     typedef struct node_st Node;
     typedef struct graph_st Graph;
     struct node_st {
        char*name; // strdup-ed
        unsigned outdegree;
     };
     struct graph_st {
       unsigned MaxSize;
       Node* table; //calloc-ed, of allocated size MaxSize
     };
    

    So for example your insert_node function might be

    void insert_node (Graph *mygraph, int n, char *name) {
      assert (mygraph != NULL);
      assert (n >= 0);
      assert (name != NULL && *name != (char)0);
      unsigned maxsize = mygraph->MaxSize;
      if (maxsize <= n) {
        unsigned newmaxsize = n + maxsize/2 + 1;
        Node* newtable = calloc (newmaxsize, sizeof(Node));
        if (!newtable) 
           perror("growing table in graph"), exit(EXIT_FAILURE);
        for (unsigned i=0; i<maxsize; i++) 
           newtable[i] = mygraph->table[i];
        free (mygraph->table);
        mygraph->table = newtable;
        mygraph->MaxSize = newmaxsize;
      };
      mygraph->table[n].name = strdup(name);
      mygraph->table[n].outdegree = 0;
    }
    

    You probably don’t need insert_node to return a value (otherwise you won’t always return 0). So I made it a void returning function (i.e. a “procedure” or “routine”).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our current solutions/projects have several classes combined into one file, I'm told this was
In our company's coding standard, we have been told to be aware of the
I'm trying to reproduce the effect our friend @Dr. Molle told us on this
I work at an eCommerce company. Our DBA recently told me that using SQL
I was told that a function defined as static in one .c file is
I was recently told it was bad practice to haved marked a number of
Recently I was told to add auto_increment primary key to all our tables because
We are told that we should implement hashCode() for our classes but most people
I have been told to create forum functionality in our website..I have done it
My professor, (for some unexplained reason) wants to change all input-output files of our

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.