我的目标是在C中创建一个程序,它将采用邻接列表文件和给出的参数。我想要制作的程序将使用列表从argv [1]转到argv [2],然后显示可能的最短路径。到目前为止,我所拥有的是关于代码的样子的一些注释:
#include <stdio.h>
#include <stdlib.h>
main
{
FILE *file;
file = fopen("adj.data", "r");
if (file == NULL) {
//if file is not in directory
printf("File was not found!\n Try again when you have a valid file!\n");
return;
}
int i;
while (fgets(buffer,buff,file) != NULL) {
//while still able to read from file
if (sscanf(buffer, "%d %d", &from[i], &to[i]) == 2) {
grab edge i
set arrayi = {from[i], to[i]}
addtolist(from[i])
//adds number to list if not already on it
//so we can check if the arguments are on the list later
addtolist(to[i])
if (argc==3) { //else give error and exit
checklist(argv[1])
checklist(argv[2])
//checks list from earlier to make sure arguments are there,
//else give error and end program
//create queue starting with argv[1]
findshort(argv[1], argv[2], 0)
}
} else {
printf("File does not meet requirements!\n");
fclose(file);
return;
}
}
}
findshort(int f, int t, count) //finds the shortest link from f to t
{
//find all arrays that start with f(take first array and add second element to current queue)
//if there are multiple arrays that start with f then duplicate queue and do the same as the first array
count++
//take second element of array and make it f
//if f = t then print queue + count
//else findshort(f,t,count)
//run each queue through the "findshort" function again
}
再一次,这只是对程序所做的非常粗略的描述,所以不要对这一切的糟糕表现进行投票。我会使用一个结构来跟踪队列,另一个用于节点列表。如果您想知道该文件如下:
cat adj.data
1 2
2 3
3 4
2 5
5 6
6 3
6 7
4 8
5 8
8 1