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

#include #include#include

using namespacestd;

typedefstruct node //节点

{char *data; //文件名

int dir_file; //文件夹或者文本文件

struct node *first_child,*next_sibling;

} tree;//多叉树

typedefstruct //存放树节点的队列

{

tree*data;intpre;

} Box;

typedefstruct{

Box data[100];intfront,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;returnchild;

}

tree*create() //创建头节点

{

tree*root = (tree *)malloc(sizeof(tree));

root->data = "root";

root->dir_file = 1;

root->first_child =NULL;

root->next_sibling =NULL;returnroot;

}void insert(Queue q,int index,int tj) //插入函数

{char *name;

name= new char [10];booljudge;if(q.data[index].data->dir_file == 0) //文本文件

{

cout<data<

cout<

cin>>name;

cout<

cin>>judge;

tree*node =insertTree(name,NULL,q.data[index].data,judge);

}else if(q.data[index].data->dir_file == 1 && tj == 1) //要在同一目录下创建另一文件

{

cout<data<

cout<

cin>>name;

cout<

cin>>judge;

tree*node =insertTree(name,NULL,q.data[index].data,judge);

}else //在某一目录下创建文件

{

cout<data<

cout<

cin>>name;

cout<

cin>>judge;

tree*node =insertTree(name,q.data[index].data,NULL,judge);

}

}intmain()

{int mark = 0;int num = 1;int tj = 0;int temp[100];

tree*root =create();

height(root);while(mark != -1)

{

cout<

cin>>num;

cout<

cin>>tj;if(tj == 1)

tj= 1;elsetj= 0;

insert(que,num-1,tj);

height(root);

cout<

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<data;

}

cout<data<

}

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值