C 中带有加权弧的双向图。检查输入时出错

问题描述 投票:0回答:0

我已经制作了这个程序来为大学创建一个加权图,但是当我在 main 中询问输入时,错误检查会出现一些问题。编译没有错误,但是输出很奇怪。关于如何解决这个问题的任何建议?谢谢:

#include <stdio.h>
#include <stdlib.h>

// Define a structure to represent a node in the graph
typedef struct node {
    int key;             // Index of the destination vertex
    int weight;
    struct node* next;    // Pointer to the next node in the adjacency list
} node_t;

// Define a structure to represent the graph
typedef struct graph {
    int V;            // Number of vertices in the graph
    node_t** adjList; // Array of pointers to the head of each adjacency list
} graph_t;

node_t* newNode(int dest);
graph_t* createGraph(int V);
void addEdge(graph_t* graph, int src, int key, int w);
void printGraph(graph_t* graph);
int isPresent(graph_t* g, int source, int key);

// Driver code to test the implementation
int main() {
    int nV = 0;
    printf("Insert number nV\n");
    scanf("%d", &nV);

    graph_t* graph = createGraph(nV);

    for(int i = 0; i < nV; i++)
    {
        int source = 0;
        int key = 0;
        int weight = 0;
        printf("Inert node, specify Source, Key and Weight [v:%d]: ", i);
        scanf("%d %d %d", &source, &key, &weight);

        while(source >= nV){
            printf("Source Out of Bounds, retry\n");
            scanf("%d", &source);
        }

        while(key >= nV){
            printf("Key Out of Bounds, retry\n");
            scanf("%d", &key);
        }

        while(isPresent(graph, source, key) == 1)
        {
            printf("Arc found, retry; new source, key:\n");
            scanf("%d %d", &source, &key);
        }

        printGraph(graph);
        addEdge(graph, source, key, weight);
    }
    printf("\nFinal Print\n");
    printGraph(graph);
    return 0;
}

// Function to create a new node with the given destination vertex index
node_t* newNode(int dest) {
    node_t* newNode = (node_t*) malloc(sizeof(node_t));
    newNode->key = dest;
    newNode->next = NULL;
    return newNode;
}

// Function to create a new graph with the given number of vertices
graph_t* createGraph(int V) {
    graph_t* graph = (graph_t*) malloc(sizeof(graph_t));
    graph->V = V;
    graph->adjList = (node_t**) malloc(V * sizeof(node_t*));
    int i;
    for (i = 0; i < V; i++) {
        graph->adjList[i] = NULL;
    }
    return graph;
}

// Function to add an edge to the graph
void addEdge(graph_t* graph, int src, int key, int w) {
    // Add edge from src to key
    node_t* newN = newNode(key);
    newN->weight = w;
    newN->next = graph->adjList[src];
    graph->adjList[src] = newN;

    // Add edge from key to src (if the graph is undirected)
    node_t* newN2 = newNode(src);
    newN2->next = graph->adjList[key];
    newN2->weight = w;
    graph->adjList[key] = newN2;
}

int isPresent(graph_t* g1, int source, int key)
{
    graph_t* gTmp = g1;
    while(gTmp->adjList[source] != NULL)
    {
      if(gTmp->adjList[source]->key == key)
      {
          printf("Key in Common: %d\n", key);
          return 1;
      }
        gTmp->adjList[source] = gTmp->adjList[source]->next;
    }
    return 0;
}

// Function to print the adjacency list of the graph
void printGraph(graph_t* graph) {
    int i;
    for (i = 0; i < graph->V; i++) {
        node_t* currNode = graph->adjList[i];
        printf("Adjacency list of vertex:\n");
        while (currNode != NULL) {
            printf(" %d --> %d - W: %d\n", i, currNode->key, currNode->weight);
            currNode = currNode->next;
        }
        printf("\n");
    }
}

如果输入正确,我没有问题,而如果输入错误,则输出不固定。

输出示例:

Insert number nV
3
Inert node, specify Source, Key and Weight [v:0]: 0 1 55
Adjacency list of vertex:

Adjacency list of vertex:

Adjacency list of vertex:

Inert node, specify Source, Key and Weight [v:1]: 0 2 33

Adjacency list of vertex:

Adjacency list of vertex:
 1 --> 0 - W: 55

Adjacency list of vertex:

Inert node, specify Source, Key and Weight [v:2]: 0 1 44

Adjacency list of vertex:

Adjacency list of vertex:
 1 --> 0 - W: 55

Adjacency list of vertex:
 2 --> 0 - W: 33


**Final Print**

Adjacency list of vertex:
 0 --> 1 - W: 44

Adjacency list of vertex:
 1 --> 0 - W: 44
 1 --> 0 - W: 55

Adjacency list of vertex:
 2 --> 0 - W: 33


Process finished with exit code 0
c graph weighted error-checking
© www.soinside.com 2019 - 2024. All rights reserved.