【问题标题】:Why my output isn't correct?为什么我的输出不正确?
【发布时间】:2026-02-20 08:00:01
【问题描述】:

我想用字母构建一个图表,但是出了点问题。

我的代码:

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

struct AdjListNode
{
  char *dest;
  struct AdjListNode* next;
};


struct AdjList
{
  struct AdjListNode *head;  // pointer to head node of list
};

struct Graph
{
  int V;
  struct AdjList* array;
};

struct AdjListNode* newAdjListNode(char *dest){
struct AdjListNode* newNode =
    (struct AdjListNode*) malloc(sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->next = NULL;
return newNode;}

struct Graph* createGraph(int V){
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;

// Create an array of adjacency lists.  Size of array will be V
graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList));

// Initialize each adjacency list as empty by making head as NULL
int i;
for (i = 0; i < V; ++i)
    graph->array[i].head = NULL;

return graph;}

void addEdge(struct Graph* graph, char *src, char *dest){
// Add an edge from src to dest.  A new node is added to the adjacency
// list of src.  The node is added at the beginin
struct AdjListNode* newNode = newAdjListNode(dest);
newNode->next = graph->array[0].head;
graph->array[0].head = newNode;

// Since graph is undirected, add an edge from dest to src also
newNode = newAdjListNode(src);
newNode->next = graph->array[1].head;
graph->array[1].head = newNode;}

void printGraph(struct Graph* graph){
int v;
for (v = 0; v < graph->V; ++v)
{
    struct AdjListNode* pCrawl = graph->array[v].head;
    printf("\n Adjacency list of vertex %d\n head ", v);
    while (pCrawl)
    {
        printf("-> %s", pCrawl->dest);
        pCrawl = pCrawl->next;
    }
    printf("\n");}}

int main(){
// create the graph given in above fugure
int V = 5;
struct Graph* graph = createGraph(V);
addEdge(graph, "a", "b");
addEdge(graph, "a", "e");
addEdge(graph, "b", "c");
addEdge(graph, "b", "d" );
addEdge(graph, "b", "e");
addEdge(graph, "c", "d");
addEdge(graph, "d", "e");


// print the adjacency list representation of the above graph
printGraph(graph);

return 0;}

我的输出是这样的:

e->d->e->d->a->b->c->b->a->

我的输出应该是:

a->b->e 
b->a->c->d->e
c->b->d
d->b->c->e
e->a->b->d

请帮帮我。我再次问了一个不同的问题。我想要这个输出。我想用字母创建 adjList

【问题讨论】:

    标签: c data-structures graph


    【解决方案1】:

    我评论了更改的部分并解释了原因。我没有更改您的代码,以便您可以轻松理解。

    试试这个:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct AdjListNode
    {
        char *dest;
        struct AdjListNode* next;
    };
    
    struct AdjList
    {
        struct AdjListNode *head;  // pointer to head node of list
    };
    
    struct Graph
    {
        int V;
        struct AdjList* array;
    };
    
    struct AdjListNode* newAdjListNode(char *dest)
    {
        struct AdjListNode* newNode = (struct AdjListNode*) malloc(sizeof(struct AdjListNode));
        newNode->dest = dest;
        newNode->next = NULL;
        return newNode;
    }
    
    struct Graph* createGraph(int V)
    {
        struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
        graph->V = V;
    
        // Create an array of adjacency lists.  Size of array will be V
        graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList));
    
        // Initialize each adjacency list as empty by making head as NULL
        int i;
        for (i = 0; i < V; ++i)
            graph->array[i].head = NULL;
    
        return graph;
    }
    
    void addEdge(struct Graph* graph, char *src, char *dest)
    {
        // Add an edge from src to dest.  A new node is added to the adjacency
        // list of src.  The node is added at the beginin
    
        struct AdjListNode* newNode = newAdjListNode(dest);
    
        //***Changed. you need to add edge in src node. But you were always adding in 0
        newNode->next = graph->array[src[0]-'a'].head;
        graph->array[src[0]-'a'].head = newNode;
    
        // Since graph is undirected, add an edge from dest to src also
        newNode = newAdjListNode(src);
    
        //***Changed. you need to add edge in dest node. But you were always adding in 1
        newNode->next = graph->array[dest[0]-'a'].head;
        graph->array[dest[0]-'a'].head = newNode;
    }
    
    void printGraph(struct Graph* graph)
    {
        int v;
        for (v = 0; v < graph->V; ++v)
        {
            struct AdjListNode* pCrawl = graph->array[v].head;
            printf("\n Adjacency list of vertex %d\n head %c", v, v + 'a');
            while (pCrawl)
            {
                printf("-> %s", pCrawl->dest);
                pCrawl = pCrawl->next;
            }
            printf("\n");
        }
    }
    
    int main()
    {
        // create the graph given in above fugure
        int V = 5;
        struct Graph* graph = createGraph(V);
        addEdge(graph, "a", "b");
        addEdge(graph, "a", "e");
        addEdge(graph, "b", "c");
        addEdge(graph, "b", "d" );
        addEdge(graph, "b", "e");
        addEdge(graph, "c", "d");
        addEdge(graph, "d", "e");
    
    
        // print the adjacency list representation of the above graph
        printGraph(graph);
    
        return 0;
    }
    

    它会给你输出:

    a-> e-> b
    b-> e-> d-> c-> a
    c-> d-> b
    d-> e-> c-> b
    e-> d-> b-> a
    

    如果你想得到完全相同的输出:

    a-> b-> e
    b-> a-> c-> d-> e
    c-> b-> d
    d-> b-> c-> e
    e-> a-> b-> d
    

    只需按照以下方式更改 addEdge() 函数:

    void addEdge(struct Graph* graph, char *src, char *dest)
    {
        // Add an edge from src to dest.  A new node is added to the adjacency
        // list of src.  The node is added at the beginin
    
        struct AdjListNode* newNode = newAdjListNode(dest);
    
        //***Changed. you need to add adge in src node. But you were always adding in 0
        struct AdjListNode* temp=graph->array[src[0]-'a'].head;
    
        if(temp==NULL)  // First element of the list
          graph->array[src[0]-'a'].head=newNode;
        else
        {
            while(temp->next!=NULL) // finding the last element of the list
                temp=temp->next;
            temp->next=newNode; // Adding current element to the last
        }
    
        // Since graph is undirected, add an edge from dest to src also
        newNode = newAdjListNode(src);
    
        //***Changed. you need to add adge in dest node. But you were always adding in 1
        temp = graph->array[dest[0]-'a'].head;
    
        if(temp==NULL) // First element of the list
          graph->array[dest[0]-'a'].head=newNode;
        else
        {
            while(temp->next!=NULL) // finding the last element of the list
                temp=temp->next;
            temp->next=newNode; // Adding current element to the last
        }
    }
    

    第一个代码将在前面添加新边缘,第二个代码将在最后添加它。
    希望对你有帮助:)

    【讨论】:

    • 谢谢,这对我很有帮助,但了解的东西很少。你为什么这样写:graph->array[dest[0]-'a']。什么是'a'?为什么是'a'?
    • 因为你的优势从'a'开始。所以我把它作为节点 0 'b' 作为 1 等等.. 你已经接受了其他答案.. 我猜你找到了你的解决方案
    【解决方案2】:

    这是一个使用线性但有效的二维数组的实现,因为它的大小是已知的,但这只是为了保持一些相似性,否则已知V 我会声明#define V 5 和一个静态二维数组@ 987654323@。除了简化之外,我将char* 字符串参数更改为int,因为它们代表一个字符。我还将V size 参数传递给函数。

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    void printGraph(int *graph, int V) {
        int v, n;
        for (v=0; v<V; ++v) {
            printf ("%c", v + 'a');
            for (n=0; n<V; ++n) {
                if (graph [v * V + n])
                    printf("->%c", n + 'a');
            }
            printf("\n");
        }
    }
    
    void addEdge(int *graph, int V, int src, int dest) {
        src  = tolower(src)  - 'a';
        dest = tolower(dest) - 'a';
        if (src < 0 || src >= V || dest < 0 || dest >= V || src == dest)
            return;     // error
        graph [src  * V + dest] = 1;
        graph [dest * V + src ] = 1;
    }
    
    int *createGraph (int V) {
        return calloc (V * V, sizeof(int));
    }
    
    int main(){
        int V = 5;
        int *graph;
        if (graph = createGraph(V)) {
            addEdge(graph, V, 'a', 'b');  // changed from string to char
            addEdge(graph, V, 'a', 'e');
            addEdge(graph, V, 'b', 'c');
            addEdge(graph, V, 'b', 'd');
            addEdge(graph, V, 'b', 'e');
            addEdge(graph, V, 'c', 'd');
            addEdge(graph, V, 'd', 'e');
    
            printGraph(graph, V);
            free (graph);
        }
        return 0;
    }
    

    程序输出

    a->b->e
    b->a->c->d->e
    c->b->d
    d->b->c->e
    e->a->b->d
    

    您使用“a”到“e”来标记顶点。在我的回答中,我将这些更改为 'a' 到 'e' 并使用这些标签来索引 5x5 数组并操作这些顶点标签,使它们在 0 到 4 的范围内。我使用 tolower() 处理标签 'A ' 到 'E' 被使用,但这并不是真正必要的,因为它们没有被使用。然后我减去 'a' 以将标签的范围从 0 开始。作为 int,值 'a' 为 97,因此您可以看到 'a' - 'a' = 0'e' - 'a' = 4

    【讨论】:

    • 我可以为 ------src = tolower(src) - 'a'; 写不同的东西吗? dest = tolower(dest) - 'a';----- 我可以写 'b' 而不是 'a' 吗?
    • @william 我为我的答案添加了更多解释。
    最近更新 更多