Windows C++ 非递归式(stack)深度优先遍历目录
#include <Windows.h>
#include <cstdio>
#include <cstring>
#include <string>
#include <stack> typedef void (__stdcall *P_WALK_DIR_CALLBACK)(const std::string &In_strFilePath); int WalkDir(const char *In_pcRoot, P_WALK_DIR_CALLBACK In_pfunCallBack)
{
int iRetVal = ;
std::string strRoot;
std::stack<std::string> stkDirs; if (In_pcRoot == NULL || In_pfunCallBack == NULL)
{
iRetVal = -;
goto fun_ret;
} strRoot = In_pcRoot;
if (strRoot.empty())
{
iRetVal = -;
goto fun_ret;
} if (strRoot.back() != '\\' && strRoot.back() != '/')
strRoot += '\\';
stkDirs.push(strRoot); while (!stkDirs.empty())
{
std::string strDirForWalk;
WIN32_FIND_DATAA Win32FindData = {};
HANDLE hFindHandle = NULL; strDirForWalk = stkDirs.top();
stkDirs.pop();
hFindHandle = FindFirstFileA((strDirForWalk + "*").c_str(), &Win32FindData);
if (hFindHandle == INVALID_HANDLE_VALUE)
continue; if (!(strlen(Win32FindData.cFileName) == && strncmp(Win32FindData.cFileName, ".", ) == )
&& !(strlen(Win32FindData.cFileName) == && strncmp(Win32FindData.cFileName, "..", ) == ))
{
if (Win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
stkDirs.push(strDirForWalk + Win32FindData.cFileName + "\\");
else
In_pfunCallBack(strDirForWalk + Win32FindData.cFileName);
}
while (FindNextFileA(hFindHandle, &Win32FindData))
{
if (!(strlen(Win32FindData.cFileName) == && strncmp(Win32FindData.cFileName, ".", ) == )
&& !(strlen(Win32FindData.cFileName) == && strncmp(Win32FindData.cFileName, "..", ) == ))
{
if (Win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
stkDirs.push(strDirForWalk + Win32FindData.cFileName + "\\");
else
In_pfunCallBack(strDirForWalk + Win32FindData.cFileName);
}
} if (hFindHandle != INVALID_HANDLE_VALUE)
FindClose(hFindHandle);
} fun_ret:
return iRetVal;
} void inline __stdcall WalkDirCallBack(const std::string &In_strFilePath)
{
printf("%s\n", In_strFilePath.c_str());
return;
} void main(int argc, char **argv)
{
WalkDir(argv[], WalkDirCallBack);
return;
}
Windows C++ 非递归式(stack)深度优先遍历目录的更多相关文章
- 非递归实现先序遍历 java leecode 提交
写完才知道自己学习都是似是而非啊,大家可以也在leecode上提交代码,纯手写,离开eclipse第一种方式:数据结构书上的,使用栈大概思路.1.不断将根节点的左孩子的左孩子直到为空,在这个过程入栈. ...
- 数据结构-树以及深度、广度优先遍历(递归和非递归,python实现)
前面我们介绍了队列.堆栈.链表,你亲自动手实践了吗?今天我们来到了树的部分,树在数据结构中是非常重要的一部分,树的应用有很多很多,树的种类也有很多很多,今天我们就先来创建一个普通的树.其他各种各样的树 ...
- 图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS)
参考网址:图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS) - 51CTO.COM 深度优先遍历(Depth First Search, 简称 DFS) 与广度优先遍历(Breath ...
- 基于visual Studio2013解决面试题之0401非递归遍历二叉树
题目
- 二叉树3种递归和非递归遍历(Java)
import java.util.Stack; //二叉树3种递归和非递归遍历(Java) public class Traverse { /******************一二进制树的定义*** ...
- Java实现二叉树的先序、中序、后序、层序遍历(递归和非递归)
二叉树是一种非常重要的数据结构,很多其它数据结构都是基于二叉树的基础演变而来的.对于二叉树,有前序.中序以及后序三种遍历方法.因为树的定义本身就是递归定义,因此采用递归的方法去实现树的三种遍历不仅容易 ...
- 非递归遍历二叉树Java版的实现代码(没写层次遍历)
直接上代码呵呵,里面有注解 package www.com.leetcode.specificProblem; import java.util.ArrayList; import java.util ...
- C++编程练习(17)----“二叉树非递归遍历的实现“
二叉树的非递归遍历 最近看书上说道要掌握二叉树遍历的6种编写方式,之前只用递归方式编写过,这次就用非递归方式编写试一试. C++编程练习(8)----“二叉树的建立以及二叉树的三种遍历方式“(前序遍历 ...
- Java ---- 遍历链表(递归与非递归实现)
package test; //前序遍历的递归实现与非递归实现 import java.util.Stack; public class Test { public static void main( ...
随机推荐
- JavaScript中的继承与原型链
先看一个例子 function User(){} var u1 = new User(); console.log(u1.prototype);// undefined 使用对象实例无法访问到prot ...
- HDUOJ---2642Stars(二维树状数组)
Stars Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)Total Submi ...
- nyoj------------找球号(一)
找球号(一) 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 在某一国度里流行着一种游戏.游戏规则为:在一堆球中,每个球上都有一个整数编号i(0&l ...
- 用 python 爬虫抓站的一些技巧总结
学用python也有3个多月了,用得最多的还是各类爬虫脚本:写过抓代理本机验证的脚本,写过在discuz论坛中自动登录自动发贴的脚本,写过自动收邮件的脚本,写过简单的验证码识别的脚本,本来想写goog ...
- 更改 pandas dataframe 中两列的位置
更改 pandas dataframe 中两列的位置: 把其中的某列移到第一列的位置. 原来的 df 是: df = pd.read_csv('I:/Papers/consumer/codeandpa ...
- App上架重磅通知:App Store安全新规17年1月生效
作者:沙铭 来源:公众号 沙铭世界观 ID:mobview 做推广的也许并不了解什么是ATS(App Transport Security),不过这却是一个定时炸弹,引爆点在2016年底,后果就是你不 ...
- Android开发:轻松实现图片倒影效果
效果如下: <ignore_js_op> device_thumb.png (68.26 KB, 下载次数: 41) 下载附件 保存到相册 2011-12-11 09:46 上传 主 ...
- Google官方下拉刷新组件---SwipeRefreshLayout
今天在Google+上看到了SwipeRefreshLayout这个名词,遂搜索了下,发现竟然是刚刚google更新sdk新增加的一个widget,于是赶紧抢先体验学习下. SwipeRefreshL ...
- oc 工厂方法
通过上例看oc创建实例有点麻烦,oc里面可以创建工厂方法可以让这个操作更简单一些(其实就是c#或者java里面的静态方法). 新建一个“Cocoa Touch Class”文件,命名为People P ...
- clscfg.bin: error while loading shared libraries: libcap.so.1:
RAC安装过程中,安装GI,运行root.sh脚本时报如下错误: # /u01/app//grid/root.sh Running Oracle 11g root script... The foll ...