我正在尝试创建一个在图形中生成bfs的程序。我创建了邻接矩阵和bfs函数。如果我输入一个小输入(如4个节点),该程序可以工作,但如果我输入更大的输入,我开始得到分段错误。
事情是,我不会总是在同一个地方。每次我运行它,它可能会在不同的迭代中崩溃,但肯定在同一个循环中。这是我的代码:(我只是发布了我认为与问题相关的函数,但如果你认为你需要更多的代码,那就问一下。)
void matrixBfs(int n, int matrix[n][n], Node nodeLocker[n], int source)
{
int aux;
Box* head =(Box*) malloc(sizeof(Box*));
if (head == NULL)
{
printf("Memory failure\n");
}
head->next = NULL;
add(head, source);
nodeLocker[source].checked = 1;
while (len(head) > 0)
{
aux = pop(head);
for(int i=0; i<n; i++)
{
if ((matrix[aux][i]) && !(nodeLocker[i].checked))
{
add(head, i);
nodeLocker[i].checked = 1;
}
}
}
head = NULL;
free(head);
}
这是崩溃的功能,通过打印,我确定在
上崩溃了while(len(head)&gt; 0)
和
add(head,i);
语句。
这些功能是:
// Returns the length of the list.
int len(Box* head)
{
int i = 0;
Box* current = head;
for(i; current->next != NULL; i++)
{
current = current-> next;
}
return i;
}
// Adds an element at the end of the list
void add(Box* head, int value)
{
Box* current = head;
while((current->next) != NULL)
{
current = (current->next);
}
Box* auxNode = (Box*) malloc(sizeof(Box*));
if (auxNode == NULL)
{
printf("Memory failure\n");
}
auxNode->id = value;
auxNode->next = NULL;
current->next = auxNode;
}
同样,通过打印,我确定在检查链表的下一个节点时它们都会崩溃。也就是说,
for(i; current-&gt; next!= NULL; i ++)
和
while((current-&gt; next)!= NULL)
我很抱歉延长了我的问题,并非常感谢你对此事的任何帮助。所以提前谢谢。
编辑:以下是需要的整个代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Lists.c"
typedef struct Nodo
{
int id; // Representative value
int checked; // 0 if the node wasn't visited; and 1 if it was.
} Node;
void initializeLocker(int n, Node nodeLocker[n])
{
for (int i=0; i < n; i++)
{
nodeLocker[i].id = i;
nodeLocker[i].checked = 0;
}
}
void matrixBfs(int n, int matrix[n][n], Node nodeLocker[n], int source)
{
int aux;
Box* head =(Box*) malloc(sizeof(Box*));
if (head == NULL)
{
printf("Memory failure\n");
}
head->next = NULL;
add(head, source);
nodeLocker[source].checked = 1;
while (len(head) > 0)
{
aux = pop(head);
for(int i=0; i<n; i++)
{
if ((matrix[aux][i]) && !(nodeLocker[i].checked))
{
add(head, i);
nodeLocker[i].checked = 1;
}
}
}
head = NULL;
free(head);
}
int main()
{
FILE* file = fopen("Input.in","r");
char c[5];
char* pch;
int n;
int num;
int num2;
fgets(c, 5, file);
n = atoi(c); // Number of nodes.
if (n < 0)
{
printf("Must input at least one node");
exit(1);
}
int matrix[n][n];
// Initialization loop
for (int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
matrix[i][j] = 0;
}
}
// Insertion loop.
while(fgets(c, 5, file))
{
pch = strtok(c, " ");
num = atoi(pch) - 1;
pch = strtok(NULL, " ");
num2 = atoi(pch) - 1;
matrix[num][num2] = 1;
matrix[num2][num] = 1;
}
fclose(file); // No further use of the file.
pch = NULL;
free(pch);
Node nodeLocker[n];
initializeLocker(n, nodeLocker);
matrixBfs(n, matrix, nodeLocker, 0);
for (int i=0; i<n; i++)
{
if(nodeLocker[i].checked == 0)
{
printf("The entered graph has at least 1 node aislated. Please enter
a new graph.");
exit(1);
}
}
initializeLocker(n, nodeLocker);
nodeLocker[0].checked = 1;
matrixBfs(n, matrix, nodeLocker, 1);
for (int i=1; i<n; i++)
{
if (nodeLocker[i].checked == 0)
{
printf("%d is a linker agent.\n", 1);
break;
}
}
for (int i=1; i<n; i++)
{
initializeLocker(n, nodeLocker);
nodeLocker[i].checked = 1;
matrixBfs(n, matrix, nodeLocker, 0);
for (int j=0; j<n; j++)
{
if (nodeLocker[j].checked == 0)
{
printf("%d is a linker agent.\n", i+1);
break;
}
}
}
return 0;
}
“Box”结构在此文件中定义:
#include <stdio.h>
#include <stdlib.h>
typedef struct Box
{
int id;
struct Box* next;
} Box;
// Adds an element at the end of the list.
void add(Box* head, int value)
{
Box* current = head;
Box* next = NULL;
if (current != NULL)
{
next = current->next;
}
while(next != NULL)
{
current = next;
next = current->next;
}
Box* auxNode = (Box*) malloc(sizeof(Box*));
if (auxNode == NULL)
{
printf("Memory failure\n");
}
auxNode->id = value;
auxNode->next = NULL;
current->next = auxNode;
}
// Returns the length of the list
int len(Box* head)
{
int i = 0;
Box* current = head;
Box* next = NULL;
if (current != NULL)
{
next = current->next;
}
for(i; next != NULL; i++)
{
current = next;
next = current-> next;
}
return i;
}
// Prints the entire list
void printLine(Box* head)
{
if (head->next == NULL)
{
printf("The list is empty\n");
return;
}
Box* current = head;
current = current->next;
printf("[%d", (current->id) + 1);
while (current->next != NULL)
{
current = current->next;
printf(", %d", (current->id) + 1);
}
printf("]\n");
}
// Deletes and returns the last element of the list
int pop(Box* head)
{
Box* auxNode;
int value;
if (len(head) == 1)
{
auxNode = head->next;
head->next = NULL;
value = auxNode->id;
auxNode = NULL;
free(auxNode);
return value;
}
Box* current = head;
while (current->next->next != NULL)
{
current = current->next;
}
auxNode = current->next;
value = current->next->id;
current->next = NULL;
auxNode = NULL;
free(auxNode);
return value;
}
最后,使用“Input.txt”文件输入输入。这里,第一行显示图形的节点数,其余显示它们之间的连接:
9
1 2
1 3
1 5
2 3
2 5
3 5
4 7
5 6
5 7
5 9
6 7
6 9
7 8
7 9
8 9
答案 0 :(得分:0)
在导致分段错误的两个语句中,您都没有检查当前是否为NULL。而是在len和add函数中使用类似的东西:
{{1}}