遍历链表做某事
Iterate Through a Linked List to Do Something
我有一个链接列表,其中包含一个文件的路径以及它所属的组 ID。我的程序在当前目录中查找常规文件,并且我试图遍历链表以便对链表执行某些操作。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
typedef struct FileGroups
{
int groupID;
char *path;
struct FileGroups* next;
} FileGroups;
FileGroups *head;
int GroupID = 1;
void insert(char *path)
{
FileGroups *temp;
temp = (FileGroups*)malloc(sizeof(FileGroups));
temp->groupID = GroupID++;
temp->path = malloc(strlen(path)*sizeof(char));
strcpy(temp->path, path);
temp->next = head;
head = temp;
temp = temp->next;
}
void print()
{
FileGroups *temp;
temp = head;
printf("\nLinked list: \n");
while(temp!=NULL)
{
printf("%d %s\n", temp->groupID, temp->path);
temp = temp->next;
}
}
void listFilesRecursively(const char *basePath)
{
char path[1024];
struct dirent *dp;
DIR *dir = opendir(basePath);
if (!dir)
{
return;
}
while ((dp = readdir(dir)) != NULL)
{
if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
{
struct stat sb;
FileGroups *temp;
temp = head;
strcpy(path, basePath);
strcat(path, "/");
strcat(path, dp->d_name);
if(stat(path, &sb) == 0 && S_ISREG(sb.st_mode))
{
insert(path);
while(temp!=NULL)
{
printf("Do something with %s\n", temp->path);
temp = temp->next;
}
printf("\n");
}
else
{
return;
}
}
}
closedir(dir);
}
int main()
{
listFilesRecursively(".");
print();
return 0;
}
这是我在 运行 程序时得到的输出:
我不确定我是否正确地遍历了链表,因为它似乎在迭代地打印出“做某事”,但每次通过循环时,它似乎都会额外调用 printf("Do something\n");
无论以前的文件路径与它所在的当前文件路径是什么,我都希望它只对添加到列表中的当前文件路径做一些事情。它似乎也没有 printf("Do something\n");
在它的第一个循环中,因为在我们甚至用目录中的第一个文件打印出“做某事”之前有一个换行符,最后一件事是它没有对目录中的最后一个文件做任何事情./testing.txt
目录。预先感谢您的建议和建议!
这是因为您在构建列表时循环遍历了整个列表。循环遍历目录内容后打印列表,或者更好的是,只打印目录搜索循环中的单个项目。
while(temp!=NULL)
{
printf("Do something with %s\n", temp->path);
temp = temp->next;
}
应该是:
printf("Do something with %s\n", path);
我有一个链接列表,其中包含一个文件的路径以及它所属的组 ID。我的程序在当前目录中查找常规文件,并且我试图遍历链表以便对链表执行某些操作。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
typedef struct FileGroups
{
int groupID;
char *path;
struct FileGroups* next;
} FileGroups;
FileGroups *head;
int GroupID = 1;
void insert(char *path)
{
FileGroups *temp;
temp = (FileGroups*)malloc(sizeof(FileGroups));
temp->groupID = GroupID++;
temp->path = malloc(strlen(path)*sizeof(char));
strcpy(temp->path, path);
temp->next = head;
head = temp;
temp = temp->next;
}
void print()
{
FileGroups *temp;
temp = head;
printf("\nLinked list: \n");
while(temp!=NULL)
{
printf("%d %s\n", temp->groupID, temp->path);
temp = temp->next;
}
}
void listFilesRecursively(const char *basePath)
{
char path[1024];
struct dirent *dp;
DIR *dir = opendir(basePath);
if (!dir)
{
return;
}
while ((dp = readdir(dir)) != NULL)
{
if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
{
struct stat sb;
FileGroups *temp;
temp = head;
strcpy(path, basePath);
strcat(path, "/");
strcat(path, dp->d_name);
if(stat(path, &sb) == 0 && S_ISREG(sb.st_mode))
{
insert(path);
while(temp!=NULL)
{
printf("Do something with %s\n", temp->path);
temp = temp->next;
}
printf("\n");
}
else
{
return;
}
}
}
closedir(dir);
}
int main()
{
listFilesRecursively(".");
print();
return 0;
}
这是我在 运行 程序时得到的输出:
我不确定我是否正确地遍历了链表,因为它似乎在迭代地打印出“做某事”,但每次通过循环时,它似乎都会额外调用 printf("Do something\n");
无论以前的文件路径与它所在的当前文件路径是什么,我都希望它只对添加到列表中的当前文件路径做一些事情。它似乎也没有 printf("Do something\n");
在它的第一个循环中,因为在我们甚至用目录中的第一个文件打印出“做某事”之前有一个换行符,最后一件事是它没有对目录中的最后一个文件做任何事情./testing.txt
目录。预先感谢您的建议和建议!
这是因为您在构建列表时循环遍历了整个列表。循环遍历目录内容后打印列表,或者更好的是,只打印目录搜索循环中的单个项目。
while(temp!=NULL)
{
printf("Do something with %s\n", temp->path);
temp = temp->next;
}
应该是:
printf("Do something with %s\n", path);