循环浏览文件夹而无需递归 [英] Loop through folders without recursion

查看:77
本文介绍了循环浏览文件夹而无需递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种算法,因此OS不会出现问题,即如何在不使用递归的情况下遍历文件夹.

I was looking for an algorithm, so OS is not problem, in how to loop through folders without the use of recursion.

递归不是答案,因为递归不能到达无穷大"甚至更远,而"while循环"可以到达那里.

Recursion is not the answer because recursion cannot go to "infinity" and beyond, while a "while loop" can get there.

编程语言无关紧要.

推荐答案

您可以使用 深度优先遍历的堆栈数据结构.这是C#中的一些示例代码:

You can use a stack data structure for depth-first traversal. Here is some sample code in C#:

    var stack = new Stack<string>();

    stack.Push(@"C:\");

    while (stack.Count > 0)
    {
        var currentDirectory = stack.Pop();
        Console.WriteLine("Visiting: " + currentDirectory);

        foreach (var childDirectory in Directory.GetDirectories(currentDirectory))
        {
            stack.Push(childDirectory);
        }
    }

这篇关于循环浏览文件夹而无需递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆