Reading in data to a Linked-List

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. Trying to read in the data and store the edges into there locations in the List adjacency for that vertex. This example would make a graph with five vertexes with edges from 1 to 2&3. 2 to 4&3&1 etc. It also stores in the opposites ex 2 1 9.0.

When reading it in and printing it back out it seems that it is only storing the last data entered for that vertex. EX. When trying to print out the data read in i only get 1 3 12.0, 2 5 20.0, 3 5 15.0, ( 4 2 18.0, 5 3 15.0 would show up if `if(i<n)` was removed it is there so it does not show duplicate edges). Can anyone help show me what I am doing incorrect?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// CSCI 3300

#include <cstdio>
using namespace std;

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

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

typedef ListCell* List;

struct Vertex
{
   bool signaled;
   long distance;
   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;
	      vertexInfo[i].adjacency = new ListCell(i, 0.0, 0);
      }
   }
};

//==============================================================
//                   tail
//==============================================================
// 
//==============================================================

   List tail(List L)
   {
      return L->next;
   }
//==============================================================
//                   isEmpty
//==============================================================
// 
//==============================================================

   bool isEmpty(List L)
   {
      return L == NULL;
   }
//==============================================================
//                   readIn
//==============================================================
// 
//==============================================================

Graph readIn()
{
   int g;
   int p1;
   int p2;
   float edge;
   scanf("%i ", &g);

   Graph myGraph(g);
   scanf("%i", &p1);
   while(p1 != 0)
   {
      scanf("%i", &p2);
      scanf("%f", &edge);         
      
      myGraph.vertexInfo[p1].adjacency = new ListCell 
      (p2,edge,myGraph.vertexInfo[p1].adjacency->next);
     

      myGraph.vertexInfo[p2].adjacency = new ListCell
      (p1, edge, myGraph.vertexInfo[p2].adjacency->next);
      
      scanf("%i", &p1);
   }
   return myGraph;
}

//==============================================================
//                   printOut
//==============================================================
// 
//==============================================================

void printOut(Graph myGraph)
{  
   int n;
   int length = myGraph.numVertices;
   float d;
   List p;

   printf("There are %i vertices.\n", length);
   printf("The edges are as follows. \n\n");
   for(int i=1; i<=length; i++)
   {
      p= myGraph.vertexInfo[i].adjacency;
      for(p=p; !isEmpty(p); p=tail(p))
      {
         n = myGraph.vertexInfo[i].adjacency -> vertex;
         d = myGraph.vertexInfo[i].adjacency -> weight; 
         if(i<n)
         {
            printf("%i %i %7.3f \n",i,n,d);
         }
      }
   }
}
//==============================================================
//                   main
//==============================================================

int main(int argc, char** argv)
{
   Graph myGraph = readIn();
   printOut(myGraph);
   return 0;
}
Last edited on
You seem to be doing the same thing twice. Line 41, 87, and 91 are the same but you have already done line 87 and 91 on line 41. Choose one man, else you will run out of memory with sufficiently large input.

Also note that every time you get a new edge to connect, if a vertex is already connected to another, then this connection is lost in favor of the new edge. Example still being line 87 and 91. If you have connected say vertex 2 and 3, and you get a new connection (2, 5), the edge 2 had to 3 will be lost and it will get a new edge which is (2, 5).

To fix this, you have to create an insert method which will instead insert the new edge into the vertex' adjacency list rather than creating a new edge.

Btw your program is leaking a lot of memory, memory management is very important when dealing with graph ADT.

Off topic/
for loops don't need to have something in every single spot. These examples of for-loops are still legal:

1
2
3
4
5
6
for(;;) { ... } // infinite loop

// any combination of these are valid for loops
for(/*initialization*/; ;) { ... } // infinite loop with initialization
for(;/*condition*/;) { ... } // glorified while loop
for (;;/*assignment*/) { ... } // infinite loop with assignment 
Last edited on
Topic archived. No new replies allowed.