设计一个树型目录结构的文件系统,其根目录为 root,各分支可以是目录,也可以是文件,最后的叶子都是文件。
2024-08-27 01:28:18
设计一个树型目录结构的文件系统,其根目录为 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;
}
}
运行实例:
最新文章
- Ctrip Mydream
- 3942: [Usaco2015 Feb]Censoring [KMP]
- CPU boot up过程
- 028. asp.net数据绑定控件值DataList控件
- vector 的resize 和 reserve
- 【转载】Shared Configuration
- div和span的区别
- 【C语言】输入一组整数,求出这组数字子序列和中最大值
- grunt实用总结
- 9.Java主要有那几种文件类型?各自的作用是什么?
- 001: 徒手建立一个JavaWeb应用
- String的valueOf()用于将其它类型转换为字符串
- Dynamics CRM - 使用 C# Plugin 调用 SQL 存储过程
- C Mergeable Stack(list超好用)
- hdu 1907 (尼姆博弈)
- git和svn的对比
- tp3 链接生成二维码
- Fiddler Web Debugger是什么?(图文详解)
- 阿里云服务器内部dns可能出错
- ALPHA-3