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

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

设计一个树型目录结构的文件系统,其根目录为 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[100];
    int front,rear;
} Queue;

Queue que;

void height(tree *t)                     // 层序遍历
{
    tree *p = t,*q;
    que.data[0].data = p;
    que.data[0].pre = -1;
    que.front = -1;
    que.rear = 0;
    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 = 1;
    root->first_child = NULL;
    root->next_sibling = NULL;
    return root;
}

void insert(Queue q,int index,int tj)           //插入函数
{
    char *name;
    name = new char [10];
    bool judge;
    if(q.data[index].data->dir_file == 0)   //文本文件
    {
        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 == 1 && tj == 1)    //要在同一目录下创建另一文件
    {
        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 = 0;
    int num = 1;
    int tj = 0;
    int temp[100];
    tree *root = create();
    height(root);
    while(mark != -1)
    {
        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 == 1)
            tj = 1;
        else
            tj = 0;
        insert(que,num-1,tj);
        height(root);
        cout<<"Quit input -1 ,continue input 1 :";
        cin>>mark;
    }
    for(int i = 1; i<=que.rear; i++)      //打印路径
    {
        int k = que.data[i].pre;
        int j = 0;
        while(k != -1)
        {
            temp[j] = k;
            j++;
            k = que.data[k].pre;
        }
        while(j > 0)
        {
            j--;
            cout<<"/"<<que.data[temp[j]].data->data;
        }
        cout<<"/"<<que.data[i].data->data<<endl;
    }
}

 

运行实例:

 

 

posted @ 2018-05-18 21:08 Nikki_o3o 阅读( ...) 评论( ...) 编辑 收藏
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Linux下设计一个二级(或者树型结构文件系统,需要至少实现以下功能:log。 首先,一个文件系统是指对文件目录进行管理的系统。在Linux中,文件系统通常被挂载在虚拟文件系统层上,该层将磁盘上的文件系统映射为文件目录的层次结构。 为了实现日志功能,我们可以设计一个日志文件,用于记录系统发生的各种操作。这个日志文件可以是一个独立的文件,在文件系统中被分配一个特定的位置。每当用户执行文件系统上的操作时,例如创建、修改或删除文件/目录,都将记录在这个日志文件中。 为了实现日志功能,我们还需要参考以下几个方面: 1. 日志记录格式:确定每条日志的格式,例如包含操作类型、操作时间、操作者、操作对象等信息。 2. 日志记录方式:确定如何将日志记录到日志文件中,可以使用追加的方式写入日志信息,或者使用定期写入的方式,根据需求进行选择。 3. 日志文件的维护:确定日志文件的大小限制,当日志文件达到一定大小时,可以使用滚动方式,创建新的日志文件,并将旧的日志文件进行备份。 4. 日志的查询和分析:设计相应的工具或接口,方便用户查询和分析日志信息,以便监控文件系统的使用情况,及时发现异常操作。 总的来说,在Linux下设计一个二级(或者树型结构文件系统,并实现日志功能,需要考虑文件系统结构和功能设计,以及日志的记录、维护和查询等方面。通过合理的设计和实现,可以提高文件系统的可用性和可靠性,并对文件系统的使用进行有效的监控和管理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值