0

I am trying to create a C program that reads in an input file that contains a set of points representing edges of an undirected graph. I am trying to store each vertex struct in an array that is implemented using a pointer, however the array does not hold all of the vertexes after the load_file runs, it only contains the first one. I am also unsure how to implement a DFS to find the number of edges between each vertex and vertex 1. Currently the bottom function is where this logic would take place but I am not sure what to do.

#include <stdio.h>
#include <stdlib.h>
#include "../HW3/input_error.h"
#define VertexToSearch 1

typedef struct node {
    int value;
    struct node* nextedge;
}edge;

typedef struct node1 {
    int vertexnumber;
    int distance;
    edge* edge;
} vertex;


vertex* load_file(char*, vertex*);
vertex* create_vertex_list(vertex*, int*);
vertex* create_new_edge(int*, int*, vertex*);
void print_distance(vertex*);
vertex* find_vertex(vertex*,int);
enum error program_error;

int main(int argc, char** argv) {
    vertex* array;
    array = load_file(argv[1], array);
    print_distance(array);
}

vertex* load_file(char* filename, vertex* head) {

    int* count = malloc(sizeof (int));
    int* vertex1 = malloc(sizeof (int));
    int* vertex2 = malloc(sizeof (int));
    FILE* file = fopen(filename, "r");

    if (file == NULL) {
        printf("%s did not open", filename);
        program_error = FILE_FAILED_TO_OPEN;
        exit(program_error);
    }

    fscanf(file, "%d", count);
    head = malloc(sizeof(vertex)* *count);
    head = create_vertex_list(head, count);
    for (int i = 0; i<*count; i++) {
        fscanf(file, "\n(%d,%d)", vertex1, vertex2);
        head = create_new_edge(vertex1, vertex2, head);
    }

    free(count);

    free(vertex1);
    free(vertex2);
    fclose(file);

    return head;
}

vertex* create_vertex_list(vertex* head, int* count) {
    vertex *new_node, *current;
    for (int i = 0; i<*count; i++) {
        new_node = malloc(sizeof (vertex));
        new_node->vertexnumber = i + 1;
        new_node->edge = NULL;
        new_node->distance = 0;

       head[i] = *new_node;
    }
    return head;
}

vertex* create_new_edge(int* vertex1, int* vertex2, vertex* head) {
    edge* new = malloc(sizeof (edge));
    edge* new1 = malloc(sizeof (edge));
    new->value = *vertex1;
    new1->value = *vertex2;
    new->nextedge = NULL;
    new->nextedge = NULL;

    head[*vertex1].edge = new1;
    head[*vertex2].edge = new;

    return head;
}
void print_distance(vertex* head){
    while(head != NULL){
        if(head->edge->value == VertexToSearch){
            printf("found");
        }
        head++;
    }
    while(head != NULL){
        printf("%d %d\n",head->vertexnumber,head->distance);
        head++;
    }
}
  • It seems you are using too many pointers and making your program more complicate than it should be. – MikeCAT Mar 09 '16 at 02:37
  • Which pointers are unnecessary? – Brandon Tomblinson Mar 09 '16 at 02:42
  • `count`, `vertex1` and `vertex2` can be simple `int`, and so as the arguments `vertex1` and `vertex2` of `create_new_edge()` and `count` of `create_vertex_list()`. – MikeCAT Mar 09 '16 at 02:46
  • Also note that the argument `head` of `load_file()` should be a local variable because the value passed is not used and is overwritten in the function. – MikeCAT Mar 09 '16 at 02:47
  • Well for count vertex1 and vertex2 I just made them pointers since fscanf expects a pointer, but yes they could just be regular integers and use address operator, and the head passed into load_file is the value from main and I return the new head so that the array in main now holds the vertex pointers so it can be used in print function – Brandon Tomblinson Mar 09 '16 at 02:52
  • You passed *indeterminate value* of uninitialized variable `array` having automatic storage duration that is *meaningless* and using the value will invoke *undefined behavior*. Fortunately you didn't use this indeterminate value, but passing indeterminate value isn't good because it is confusing. – MikeCAT Mar 09 '16 at 02:54
  • how else would you allow array to be used in the rest of main?? I create it but don't allocate any memory for it until the size is known in load_file – Brandon Tomblinson Mar 09 '16 at 02:56
  • I already told: delete the meaningless argument, stop passing the indeterminate value and create *local variable* `vertex* head;` in `load_file()`. Then, assign what is returned from `load_file()` to `array` in function `main()` as you are already doing, except for passing the indeterminate value. – MikeCAT Mar 09 '16 at 02:58

1 Answers1

0

Apart from memory leaks

new_node = malloc(sizeof (vertex)); edge* new = malloc(sizeof (edge)); edge* new1 = malloc(sizeof (edge));

your print function can never works. head++ will never become NULL.

Peter Silon
  • 457
  • 2
  • 11
  • I know it will never work because I haven't really put anything there since I am unsure of how to calculate the distance, but yes I have fixed the memory leak part of the new nodes – Brandon Tomblinson Mar 09 '16 at 20:13