无法创建邻接列表

时间:2016-03-09 21:04:51

标签: c adjacency-list

我正在尝试使用邻接列表来表示图形,但我目前正在努力解决它。由于某种原因,边缘被分配到错误的顶点,我无法弄清楚原因。我逐步完成代码并且前3个顶点对被添加得很好但是由于某种原因在4号上没有任何工作正确并且我最终创建了多个新边缘甚至它们的值都不正确。下面的示例输入和C代码。任何人都知道为什么会发生这种情况?注意

void print_distance(vertex*, int);
    int check_an_edge(edge*);
void free_head(vertex*); 

尚未实施,但free_head用于释放整个列表

5
(2,3)
(1,4)
(1,3)
(3,4)
(4,5)
#include <stdio.h>
#include <stdlib.h>
#include "input_error.h"
#define VertexToSearch 1

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

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


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

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

vertex* load_file(char* filename) {
    int count;
    int vertex1;
    int vertex2;
    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);
    vertex* head = malloc(sizeof(vertex)* count);
    create_vertex_list(head, count);
    for (int i = 0; i < count; i++) {
        fscanf(file, "\n(%d,%d)", &vertex1, &vertex2);
        create_new_edge(vertex1, vertex2, head);
    }
    fclose(file);
    return head;
}

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

        *(head +i)= *new_node;
    }
}

void 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;
    if ((head +vertex1 - 1)->edge == NULL) {
        (head +vertex1 - 1)->edge  = new1;
    } else {
        edge* temp = (head +vertex1 - 1)->edge;
        while (temp != NULL) {
            if (temp->nextedge == NULL) {
                temp->nextedge = new1;
                break;
            } else {
                temp = temp->nextedge;
            }
        }
    }
    if ((head +vertex2 - 1)->edge  == NULL) {
        (head +vertex2 - 1)->edge  = new;
    } else {
        edge* temp = (head +vertex2 - 1)->edge ;
        while (temp != NULL) {
            if (temp->nextedge == NULL) {
                temp->nextedge = new1;
                break;
            } else {
                temp = temp->nextedge;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在第二个create_new_edge语句的if函数中,您尝试添加new1。我认为这是一个复制粘贴错误,您应该将其更改为new

if ((head +vertex2 - 1)->edge  == NULL) {
    (head +vertex2 - 1)->edge  = new;
} else {
    edge* temp = (head +vertex2 - 1)->edge ;
    while (temp != NULL) {
        if (temp->nextedge == NULL) {
            temp->nextedge = new1; // Change here new1 to new
            break;
        } else {
             emp = temp->nextedge;
        }
    }
}