You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言图结构打印char类型顶点无显示仅输出整数问题求解

问题原因和修改方案

核心问题点

  • 顶点创建逻辑错误:main函数中创建顶点时传入的是1~4的整数,而添加边时传入的是'A'/'B'等字符的ASCII值(比如'A'对应十进制65),顶点库中不存在对应id的顶点,导致所有边创建失败,打印边时无内容输出。
  • 打印格式符错误:输出字符时使用了%d/%i整数格式符,即使vid存的是字符ASCII值也只会打印数字,不会输出字母。
  • C语法错误:结构体vList中直接给visited赋值false是C++的语法,纯C不支持结构体默认成员初始化,编译会报错。
  • 顶点数量不足:预期输出A~E共5个顶点,但原循环只创建了4个顶点。

具体修改步骤

  1. 修改顶点创建逻辑:直接传入'A'~'E'字符作为顶点id,确保和添加边时的入参匹配
  2. 替换打印函数的格式符:将所有打印vid的%i改为%c,同时调整输出对齐格式匹配预期效果
  3. 修正C语法问题:移除结构体中的默认赋值,在创建顶点时手动初始化visited为false
  4. 调整顶点创建循环范围,生成A~E共5个顶点

修改后完整代码

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

struct vList {
  int vid;
  bool visited; // 移除C++风格默认赋值
  struct vList *next;
};

struct eList{
    struct eList *next;
    struct vList *start, *end;
};

struct queue{
    struct vList *vertex;
    struct queue* next;
};

struct queue *createQueueNode(struct vList *vertex) {
  struct queue *node = (struct queue *) malloc(sizeof(struct queue));
  node->vertex = vertex;
  node->next = NULL;
  return node;
}

struct vList *createVertex(int id) {
  struct vList *node = (struct vList *) malloc(sizeof(struct vList));
  node->vid = id;
  node->visited = false; // 手动初始化访问标记
  node->next = NULL;        
  return node;
}

struct vList *searchVertex(struct vList *vroot, int vid){
    struct vList *temp = vroot;
    while(temp != NULL){
        if(temp->vid == vid){
            return temp;
        } else {
            temp = temp->next;
        }
    }
    return NULL;
}

struct eList *createEdges(struct vList *root, int s, int e){
    struct vList *temps = searchVertex(root, s);
    struct vList *tempe = searchVertex(root, e);
    if (temps == NULL || tempe == NULL )
        return NULL;
    else {
        struct eList *edge = (struct eList *) malloc(sizeof(struct eList));
        edge->next = NULL;
        edge->start = temps;
        edge->end = tempe;
        return edge;
    }
}

struct eList *addEdges(struct vList *vroot, struct eList *eroot, char s, char e){
    struct eList *t = createEdges(vroot, s, e);
    struct eList *temp = eroot;
    if (temp == NULL) {
        return t;
    }
    else
    while(temp->next != NULL){
        temp = temp->next;
    }
    temp->next = t;
    return eroot;
}

void printEdges(struct eList *eroot){
    struct eList *temp = eroot;
    if (temp == NULL) {
        printf("Empty.");
        return;
    }
    // 第一条边特殊处理对齐
    printf("\n- %c <----> %c", temp->start->vid, temp->end->vid );
    temp = temp->next;
    while(temp != NULL){
        // 后续边补空格对齐
        printf("\n  %c <----> %c", temp->start->vid, temp->end->vid );
        temp = temp->next;
    }
}

void printVertex(struct vList *vroot){
    struct vList *temp = vroot;
    if (temp == NULL) {
        printf("Empty.");
        return;
    }
    // 第一个顶点特殊处理对齐
    printf("\n- %c", temp->vid );
    temp = temp->next;
    while(temp != NULL){
        // 后续顶点补空格对齐
        printf("\n  %c", temp->vid );
        temp = temp->next;
    }
}

struct vList *addVertex(struct vList *vroot, int vid){
    struct vList *temp = vroot;
    if (temp == NULL) {
        temp = createVertex(vid);
        return temp;
    }
    else{
        while(temp->next != NULL)
            temp = temp->next;
        temp->next = createVertex(vid);
    }
    return vroot;
}

int main() {
    struct vList *vroot = NULL; 
    struct eList *eroot = NULL;
    struct queue *rear = NULL, *front = NULL;

    // 修改:创建A~E共5个顶点
    for (char c = 'A'; c <= 'E'; c++)
        vroot = addVertex(vroot, c);
    eroot = addEdges(vroot, eroot,'A', 'B'); 
    eroot = addEdges(vroot, eroot,'A', 'D'); 
    eroot = addEdges(vroot, eroot,'B', 'C'); 
    eroot = addEdges(vroot, eroot,'B', 'E'); 

    printf("Vertices");
    printVertex(vroot);
    printf("\n\nEdges");
    printEdges(eroot);

    return 0;
}

运行效果

Vertices
- A
  B
  C
  D
  E

Edges
- A <----> B
  A <----> D
  B <----> C
  B <----> E

内容的提问来源于stack exchange,提问作者Henry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 17:39:03