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( ...
随机推荐
- PHP-手册阅读笔记
1.第一次遇到$_ENV为空数组的情况, 原来是PHP.INI中variables_order为'GPCS'(表示系统在定义PHP预定义变量时的顺序是GET,POST,COOKIES,SERVER,只 ...
- HDUOJ ---悼念512汶川大地震遇难同胞——来生一起走
悼念512汶川大地震遇难同胞——来生一起走 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- Android——Intent动作汇总(转)
String ADD_SHORTCUT_ACTION 动作:在系统中添加一个快捷方式.. "android.intent.action.ADD_SHORTCUT" String A ...
- AP_应付模组在月结的处理
2014-06-04 Created By BaoXinjian 1. 完成所有交易及检查Interface (1). Invoice and Credits (2). Prepa ...
- Linux内核同步 - Seqlock
一.前言 普通的spin lock对待reader和writer是一视同仁,RW spin lock给reader赋予了更高的优先级,那么有没有让writer优先的锁的机制呢?答案就是seqlock. ...
- sublime text怎样安装ctags来定位函数
sublime确实是一款非常不错的开发软件.用起来非常爽,里面集成了非常多插件.仅仅要安装就可以, 下来来介绍下sublime中ctags插件的安装,安装这个插件之后就能够高速定位某函数了,很方便. ...
- 编码规范:Eclipse Checkstyle配置
http://chenzhou123520.iteye.com/blog/1627618 http://www.cnblogs.com/lanxuezaipiao/p/3202169.html
- 群智能优化算法-测试函数matlab源码
群智能优化算法测试函数matlab源代码 global M; creatematrix(2); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %画ackley图. %%%% ...
- Android开发6——布局中的wrap_content和fill_parent以及match_parent
一.言简意赅 fill_parent 是让控件宽或者高占全屏 wrap_content是让控件的高或宽仅仅把控件里的内容包裹住而不是全屏 二.分别来看 1 fill_parent 设置一个构件的布局 ...
- 国际化模块 angular-translate 简单方便快捷翻译中英文等多语言环境
很多web服务面对的不仅仅是当地用户,多语言环境不仅能提升逼格,更重要是一种用户体验. angular.js 作为前后端拆分的解决方案之一,当然离不开前端框架处理国际化的问题,angular.js 官 ...