设计一个树型目录结构的文件系统,其根目录为 root,各分支可以是目录,也可以是文件,最后的叶子都是文件。

我实现的功能是提供父目录(兄弟目录),输入文件名,创建树型目录结构,文本文件不可以再有子目录。最终打印出每个创建的文件夹或文本文件的路径。

代码:

#include <cstdio>
#include <cstdlib>
#include <iostream> using namespace std; typedef struct node //节点
{
char *data; //文件名
int dir_file; //文件夹或者文本文件
struct node *first_child,*next_sibling;
} tree; //多叉树 typedef struct //存放树节点的队列
{
tree * data;
int pre;
} Box; typedef struct
{
Box data[];
int front,rear;
} Queue; Queue que; void height(tree *t) // 层序遍历
{
tree *p = t,*q;
que.data[].data = p;
que.data[].pre = -;
que.front = -;
que.rear = ;
if(p->first_child == NULL)
return ;
while(que.front != que.rear)
{
que.front++;
q = que.data[que.front].data->first_child;
while(q != NULL)
{
que.rear++;
que.data[que.rear].data = q;
que.data[que.rear].pre = que.front;
q = q->next_sibling;
}
}
} tree *insertTree(char *ch, tree *parent, tree *pre_sibling,int judge) //插入节点
{
tree *child = (tree *)malloc(sizeof(tree));
child->data = ch;
child->dir_file = judge; if (parent != NULL)
{
parent->first_child = child;
}
if (pre_sibling != NULL)
{
pre_sibling->next_sibling = child;
} child->first_child = NULL;
child->next_sibling = NULL; return child;
} tree *create() //创建头节点
{
tree *root = (tree *)malloc(sizeof(tree));
root->data = "root";
root->dir_file = ;
root->first_child = NULL;
root->next_sibling = NULL;
return root;
} void insert(Queue q,int index,int tj) //插入函数
{
char *name;
name = new char [];
bool judge;
if(q.data[index].data->dir_file == ) //文本文件
{
cout<<"the current file is "<<q.data[index].data->data<<endl;
cout<<"input the name :";
cin>>name;
cout<<"input \"0\":";
cin>>judge;
tree *node = insertTree(name,NULL,q.data[index].data,judge);
}
else if(q.data[index].data->dir_file == && tj == ) //要在同一目录下创建另一文件
{
cout<<"the current file is "<<q.data[index].data->data<<endl;
cout<<"input the name :";
cin>>name;
cout<<"if it is folder, input \"1\",else input \"0\":";
cin>>judge;
tree *node = insertTree(name,NULL,q.data[index].data,judge);
}
else //在某一目录下创建文件
{
cout<<"the current folder is "<<q.data[index].data->data<<endl;
cout<<"input the name :";
cin>>name;
cout<<"if it is folder, input \"1\",else input \"0\":";
cin>>judge;
tree *node = insertTree(name,q.data[index].data,NULL,judge);
} } int main()
{
int mark = ;
int num = ;
int tj = ;
int temp[];
tree *root = create();
height(root);
while(mark != -)
{
cout<<"now is No."<<num<<",input the number of file(must be existed):";
cin>>num;
cout<<"the same level(the first can't choose Yes) ? 1.Yes 2.No"<<endl;
cin>>tj;
if(tj == )
tj = ;
else
tj = ;
insert(que,num-,tj);
height(root);
cout<<"Quit input -1 ,continue input 1 :";
cin>>mark;
}
for(int i = ; i<=que.rear; i++) //打印路径
{
int k = que.data[i].pre;
int j = ;
while(k != -)
{
temp[j] = k;
j++;
k = que.data[k].pre;
}
while(j > )
{
j--;
cout<<"/"<<que.data[temp[j]].data->data;
}
cout<<"/"<<que.data[i].data->data<<endl;
}
}

运行实例:

 

设计一个树型目录结构的文件系统,其根目录为 root,各分支可以是目录,也可以是文件,最后的叶子都是文件。的更多相关文章

  1. JSP中的一个树型结构

    看方力勋的javaWeb,采用左右值来表示树型结构(就是俺门的多级分类)表结构 页面代码 <%@ page language="java" import="java ...

  2. Nginx虚拟目录(alias)和根目录(root)

    功能要求: 假设nginx配置的域名是www.kazihuo.com,现有静态资源/home/www/oye目录需要通过nginx访问. 功能实现: 前提要求: 1.在nginx.conf中到处第二行 ...

  3. Linux文件系统的目录结构

    Linux下的文件系统为树形结构,入口为/ 树形结构下的文件目录: 无论哪个版本的Linux系统,都有这些目录,这些目录应该是标准的.各个Linux发行版本会存在一些小小的差异,但总体来说,还是大体差 ...

  4. Linux 文件系统的目录结构

    http://www.jb51.net/LINUXjishu/151820.htmlLinux下的文件系统为树形结构,入口为/ 树形结构下的文件目录: 无论哪个版本的Linux系统,都有这些目录,这些 ...

  5. WPF 4 目录树型显示

    原文:WPF 4 目录树型显示      本篇将通过WPF4 制作简单的目录树型结构显示实例,完成本篇内容我们将作出下图所示的应用程序.      从图中我们可以看到程序主要分为两部分:左边显示本地驱 ...

  6. Linux文件系统简介一(磁盘分区、开机过程、目录结构、文件权限、文件扩展名、目录管理)

    Linux:就是一组软件,一套操作系统=核心+系统呼叫接口层. 1.操作系统 操作系统 = 核心(内核) + 系统呼叫(接口) 操作系统其实也是一组程序,重点在于管理计算机的所有活动以及驱动系统中的所 ...

  7. Linux文件系统的主要目录结构说明及分区方案

    Linux操作系统有一些固定的目录.各种Linux发行版的目录结构虽然不会一模一样,但是不会有很大差异.知道了这些目录的作用,不仅对你进行磁盘分区规划很有帮助,而且会让你以后的日常维护工作变得轻松.只 ...

  8. python os.walk处理树状目录结构的文件

    在项目工作中,时常需要用到处理文件的方法,尤其是在windows环境下的树状目录结构 os.walk恰好能完美的处理这种树状目录结构文件,能高效地帮助我们得到我们需要处理的文件 目录结构: Deskt ...

  9. linux系统的目录结构

    前言 对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要 ...

随机推荐

  1. php批量检测并去除BOM头的代码

    开发中会遇到BOM头, 导致程序无法执行. 浏览器返回接口如下图: 去除BOM头解决方法:<?phpini_set('memory_limit','1024M'); function check ...

  2. springboot启动流程(目录)

    springboot出现有段时间了,不过却一直没有怎么去更多地了解它.一方面是工作的原因,另一方面是原来觉得是否有这个必要,但要持续做java似乎最终逃不开要去了解它的命运.于是考虑花一段时间去学习一 ...

  3. 节日营销!这样搞-App运营日常

    节日送礼需求日益增长,当儿女们有了购买需求的时候,商家如何突出重围,成为孝子们的首选?如何做好节日营销?几个经验分享一下: 1.抓住节日特色 结合节日风格特色,营造节日气氛,如母亲节这种节日,主要体现 ...

  4. JS 页面刷新以及页面返回的几种方式

    1.通过标签形式的跳转页面  <a class="popup" href="~/WeiXin/Shoppingguide/StockData">&l ...

  5. day14-python之集合函数字符串格式化

    1.集合 #!/usr/bin/env python # -*- coding:utf-8 -*- # s=set(['alex','alex','sb']) # print(s) # s=set(' ...

  6. 作为一名SAP从业人员,需要专门学习数学么

    最近和SAP成都研究院的开发同事聊到过这个话题,Jerry来说说自己的看法. 先回忆回忆自己本科和研究生学过的数学课程.Jerry的大一生活是在电子科技大学的九里堤校区度过的,本科第一门数学课就是微积 ...

  7. [LeetCode] 121. 买卖股票的最佳时机 ☆(动态规划)

    https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/solution/xiang-xi-tong-su-de-si-lu- ...

  8. Python基础——运行机理

    Python解释器 从Python这门编程语言的实现上来讲,Python是一个名为解释器的软件包,包含:一个解释器 和 支持的库 解释器又有不同版本:CPython.Jython.IronPython ...

  9. [转]memcached对key和value的限制 memcached的key最大长度和Value最大长度

    转自: 老白干之再干 memcached的简单限制就是键(key)和item的限制.最大键长为250个字符.可以接受的储存数据不能超过1MB,因为这是典型slab 的最大值.这里我们可以突破对key长 ...

  10. Linux学习笔记之二

    vim编辑器 :三种工作模式 vim /tmp/xueying.txt 命令模式 a.i.o/esc     \  :wq  保存并退出 /              \ 输入模式           ...