0

The input is in the following format.

    5
    1 2  9.0
    1 3 12.0
    2 4 18.0
    2 3  6.0
    2 5 20.0
    3 5 15.0
    0
    1 5

The first number is the number of vertexes in the graph. Then next lines up to 0 are the edges of the graph. With the first and second numbers being the vertexes and the third being how far the edge is between them. I can not figure out how to store the data into the List adjacency for each of the vertexes when reading it in. EX. Vertex 1 would have two List cells containing 2 9.0 and 3 12.0. I would also need to put the 1 9.0 and 1 12.0 into vertexes 2 and 3. But I can not figure out how to store the data into the ListCells

Code so Far:

#include <cstdio>
using namespace std;
typedef ListCell* List;

struct ListCell
{
   ListCell* next;
   int vertex;
   double weight;

   ListCell(int v, double w, ListCell* nxt)
   {
      vertex = v;
      weight = w;
      next = nxt;
   }
};

struct Vertex
{
   bool signaled;
   long distance;
   Vertex next;
   List adjacency;    
};

struct Graph
{
   int     numVertices;
   Vertex* vertexInfo;

   Graph(int n)
   {
      numVertices = n;
      vertexInfo  = new Vertex[n+1];
      for(int i = 1; i <= n; i++)
      {
         vertexInfo[i].signaled = false;
      }
   }
};

//==============================================================
//                   readIn
//==============================================================
// 
//==============================================================

void readIn()
{
   int n, p1, p2;
   double edge;
   scanf("%i ", &n);

   Graph(n);
   while(scanf("%i " &p1) != 0)
   {

   }
}
2
  • 2
    OK, good, feel free to carry on... Commented Mar 19, 2014 at 13:01
  • 1
    Maybe you should move the input functionality into the Graph class? Commented Mar 19, 2014 at 13:04

1 Answer 1

0

I use to define data structures in a way suitable for the business logic.

I would suggest you have a look on The Art of Computer programming to have an idea of some best practices.

Put an eye on "Linear Lists" chapter

hint: traverse the list and append the new node (please take care of corner cases):

Vertex* v = vertexInfo[i];
while (v->next!=null) {
   v = v->next;
}
v->next = new Vertex(....);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.