• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
echo-z-z
博客园    首页    新随笔    联系   管理    订阅  订阅

专题讨论1-栈和队列

集美大学课程专题讨论1

项目名称 内容
课程名称 数据结构
班级 网安2511
学号 202521336002
学号 202521336035
学号 202521336001
专题讨论名称 文件查找

一、目的

  • 针对指定的目录(dir目录及其所嵌套包含的所有目录)dir查找所有包含name的文件
  • 能用自然语言描述查找过程
  • 能清楚地写出伪代码,并分析其时间复杂度
  • 能用C/C++代码完整实现查找

二、自然语言描述查找过程

  • 找到指定根目录
  • 根目录中的文件直接按照name来查询匹配,子目录当做新的根目录继续查找,重复,直到该子目录所有内容都查完
  • 返回上一级,把所有的子目录都按照这样的方式查找
  • 终止条件:根目录的所有子目录都查找完为止

三、实验内容与设计思想

模块1:打开/关闭根目录

函数相关伪代码


searchFiles(dirPath, targetName)
{
    // 打开目录dirPath
    dir = open(dirPath)
    如果 dir 打开失败 return直接返回
    traverseDirectory(dir, dirPath, targetName);//调用模块2函数

    close(dir)
    return;
}

函数代码

void searchFiles(char* dirPath, char* targetName) {
    DIR* dir = opendir(dirPath);
    if (dir == NULL) {
        return;
    }
    traverseDirectory(dir, dirPath, targetName);

    closedir(dir);
}

模块2:遍历根目录,拼接完整输出路径

函数相关伪代码

traverseDirectory(dir, dirPath, targetName);
结构体*entry//输入
    循环
      {
        如果是entry是 . 或 ..//当前目录和上级目录
            continue  
        拼接 fullPath = dirPath + "/" + entry->name
        
        调用模块3:判断是文件还是子目录
      }

函数代码

void traverseDirectory(DIR* dir, const char* dirPath, const char* targetName) 
{
    struct dirent* entry;
    while ((entry = readdir(dir)) != NULL) 
    {
        if (strcmp(entry->name, ".") == 0 || strcmp(entry->name, "..") == 0)
        {
            continue;
        }
        char fullPath[1024];
        sprintf(fullPath, "%s/%s", dirPath, entry->name);

        checkFileType(fullPath, entry, targetName);
    }
}

模块3:判断是文件内容还是子目录

函数相关伪代码

checkFileType(fullPath, entry, targetName)
{
       if(文件)
            匹配文件名
            if(entry.name ==targetName)
                输出 fullPath 
        else if (子目录
            递归回到子目录,继续找
            searchFiles(fullPath, targetName)
}

函数代码

void checkFileType(char* fullPath, struct dirent* entry,char* targetName)
{
    struct stat statBuf;
    lstat(fullPath, &statBuf);
    if (S_ISREG(statBuf.st_mode))
    {
        if (strcmp(entry->name, targetName) == 0)
        {
            cout << "找到文件:" << fullPath << endl;
        }
    }
    else if (S_ISDIR(statBuf.st_mode)) {
        // 递归
        searchFiles(fullPath, targetName);
    }
}

四、时间复杂度分析

模块1:

分析:

  • 打开/关闭目录,只执行固定次数,时间复杂度为O(1)

模块2:

分析:

  • 遍历和根目录的条目总数(m)以及各个(k)子目录的条目总数(n)有关;
    但是在模块2的函数调用下每个条目只查找一次
    所以模块2时间复杂度是O(n)

模块3:

分析:

  • 模块3除了递归调用和目录的条目总数(n)有关
    其他,像判断文件和子目录,文件名比较,都只用根据具体长度(如文件名或者完整路径长度)执行固定次数
    所以模块3的时间复杂度也是O(n)

五、总结

本机运行截图

分模块

  • 通过模块化思想把文件查找分成三大部分,写出伪代码
  • ai辅助完成完整运行代码(它给了Linux的API,要换成Windows的才能跑,我感觉逻辑上是一个意思就没换了,
    但是运行的时候用的是Windows VS版本的代码)
  • 根据实际代码分析时间复杂度
  • 因为递归是一种栈的调用,感觉还可以试试看如果用队列该怎么查找

六、附件(参考文献和相关资料)

  1. 实验3-栈与队列
  2. 模块化编程
posted on 2026-04-05 22:40  豆芽菜zzzz  阅读(38)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3