c - 为什么我的输出不正确?

标签 c data-structures graph

我想用字母构建一个图表,但有问题。

我的代码:

#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

最佳答案

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

试试这个:

#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
    }
}

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

关于c - 为什么我的输出不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27583307/

相关文章:

c - 将函数 *cmp(const *void, const *void) 作为 C 中的参数传递并使用它来创建队列?

c - sizeof struct 小于 4 B 被报告为实际

c - `const T *restrict` 能保证指向的对象没有被修改吗?

c - C 中的快速 union 实现

c# - OxyPlot:使用 OxyPlot 和 WindowsForms 在一个窗口上绘制多个图形

c - 如何在c中创建文件的精确副本

algorithm - 如何获得斐波那契堆的最坏情况

algorithm - 如何判断一个图是否单连通?

algorithm - 为什么依赖图不表示为双向无环图?

java - 从HashSet得到的流过滤器的时间复杂度是多少?