为什么我的输出是不正确的? [英] Why my output isn't correct?
问题描述
我想建立与字母的图形,但也有一些是错误的。
I want to build a graph with letters but there is something wrong.
我的code:
#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;}
我的输出是这样的:
my output like this :
E-> D-> E-> D-> A-> B-> C-> B-> A - >
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
Please help me . I asked again but different question .I want to this output. I want to create adjList with letters
推荐答案
我评论中更改的部分,并解释了原因。我没有改变你的code,这样就可以很容易理解。
I commented the changed portion and explained why. I didn't change your code, so that you can understand easily.
试试这个:
#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;
}
这会给你的输出:
It will give you output :
a-> e-> b
b-> e-> d-> c-> a
c-> d-> b
d-> e-> c-> b
e-> d-> b-> a
如果你想获得完全相同的输出是这样的:
And if you want to get exact same output like this :
a-> b-> e
b-> a-> c-> d-> e
c-> b-> d
d-> b-> c-> e
e-> a-> b-> d
只是改变addEdge()函数的方式如下:
just change the addEdge() function following way :
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
}
}
1 code将在前面加上新的边缘和第2 code将在最后添加它。结果
希望它可以帮助:)
1st code will add new edge at front and 2nd code will add it at last.
Hope it helps :)
这篇关于为什么我的输出是不正确的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!