Linked List error (Language: C) -
The program should create a graph and store its neighbors in the adjacent list. While parsing the list, I get a SEGFAULT
This is very strange because when I try to use one from a list, it works fine.
My code:
int main (zero) {typedef struct linklist} {int vertice; Struct linkedList * Next; } Will; Type-Ff Structure Vertex {Int color; Int noOfNeighbs; LL * adj; } Top; Int E, V; Scanf ("% d% d", & amp; V, & E); // printf ("% d% d", E, V); Int i, j; // loop variables vertex erofhuratis [V]; For (i = 0; i next = arrOfVertices [First V-1] .adj; ArrOfVertices [firstV-1] .adj = Temporary; }} // print the catalog function this part segfaults oddly and why do not I know (j = 0; j & lt; v; j ++) {ll * parser = arrOfVertices [j] .adj; // printf ("% d: color:% d \ n", i + 1, arrOfVertices [i] .color); While (parser! = Null) {printf ("% d \ n", parser-> above); Parse = parsar> gt; next; }} Return 0; }
In your code, it seems that you define an array of size V Wanted, where the real value is recorded by the V user. However, in C, the compiler needs to know the size of the arrays in advance. C can allow you to declare a type and variable in any order, but that does not mean it will be an order of execution.
If you want to use an array, you can arrange in advance, and check that it is too large for the entered values. Or, you need to create an indicator for an array of the top. After that, after the entry of user V, you need to create multiple structures such as the user by using malloc and sizeof (top) as you (?) Did in the later part of the program.
ps As I mentioned, you are also going out of bound reference in line oroffertsis [V] .adj = NULL; Areas are 0-based in C, so if V is 5, and you generate 5 vertical (according to the methods given above), then you see the last head, like arrOfVertices [4].
Comments
Post a Comment