2014-12-20 65 views
0

我想用字母建立一個圖形,但是出現了錯誤。用字母創建圖形

我的代碼:

// A C Program to demonstrate adjacency list representation of graphs 

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

// A structure to represent an adjacency list node 
struct AdjListNode 
{ 
    char *dest; 
    struct AdjListNode* next; 
}; 

// A structure to represent an adjacency liat 
struct AdjList 
{ 
    struct AdjListNode *head; // pointer to head node of list 
}; 

// A structure to represent a graph. A graph is an array of adjacency lists. 
// Size of array will be V (number of vertices in graph) 
struct Graph 
{ 
    int V; 
    struct AdjList* array; 
}; 

// A utility function to create a new adjacency list node 
struct AdjListNode* newAdjListNode(char *dest) 
{ 
    struct AdjListNode* newNode = 
     (struct AdjListNode*) malloc(sizeof(struct AdjListNode)); 
    newNode->dest = dest; 
    newNode->next = NULL; 
    return newNode; 
} 

// A utility function that creates a graph of V vertices 
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; 
} 

// Adds an edge to an undirected 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; 
} 

// A utility function to print the adjacenncy list representation of graph 
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("-> %d", pCrawl->dest); 
      pCrawl = pCrawl->next; 
     } 
     printf("\n"); 
    } 
} 

// Driver program to test above functions 
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; 
} 

我的輸出是這樣的:

head->4210735->4210739->4210735->4210739->4210737->4210735->4210731 ..... 

我的輸出應該是:

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

回答

1

此行是錯誤的

printf("-> %d", pCrawl->dest); 

"%d"是整數說明符,pCrawl->dest是一個指向char所以這個工作,因爲它是印刷的pCrawl->dest的地址,因爲pCrawl->dest指向一個字符串,你應該使用字符串格式說明"%s",所以用這個

printf("-> %s", pCrawl->dest); 
+0

哦,謝謝你的幫助:我忘了寫它。它打印的信件,但仍IT方面NT,我想我的 – william

+0

安輸出新這樣的: – william

+0

頭戴式> E-> D-> E-> D-> C-> E-> b – william