实现简单文件树
来源:互联网 发布:linux启动vnc 编辑:程序博客网 时间:2024/05/22 15:55
如何实现cmd 里的 “dir” 和“cd..”命令呢,当我们在cmd 里面输入dir时,cmd会显示出当前目录的子文件;当我们输入 “cd.."时,cmd会返回当前目录的根目录,这要怎么实现呢?这时,我们会很容易想到树,看代码把:
struct Treefile{string Name;struct Treefile* child;struct Treefile* parent;struct Treefile* next; };
定义一个string 来存放文件的名字,三个指针 :child 指向第一个儿子节点,另一个next 指向 兄弟节点,一个parent 指向父亲节点。为什么要定义一个指向父亲的指针呢?因为这样方便指针回退,这是为 cd 命令服务,当你想要返回包含当前目录的根目录时,通过这个指针,就可以获取根目录。用容器vector<string> vs来顺序存放各个根文件名字,比如,你访问 "A\\B\\C\\D"文件,则A,B,C,D依次push入容器。
把一个节点设为孩子的函数为:
void BeChild(struct Treefile* tParent,struct Treefile* tChild){//struct Treefile* pt=tParent->child;if(!tParent->child){ tParent->child=tChild; tChild->parent=tParent;}else{tChild->next=tParent->child;tParent->child=tChild;tChild->parent=tParent;} }
输出一个目录下的所有文件的函数为:
void OutputTree(struct Treefile* tParent){ struct Treefile* pt=tParent->child; while(pt){ cout<<pt->Name<<' '; pt=pt->next; }}
然后,这里有一个问题,如何从一棵树的根,搜索到我们需要的目录,即搜索到特定路径,把路径里的所有节点都存起来,这里也用容器vector 来存,定义vecotor<Treefile*> vt 来存入节点指针。
查找路径的函数为:
void FindTree(vector<string>& vs,vector<Treefile*>&vt,Treefile *t,int len,int pos){ if(len==0 ) return; if(vs[pos]!=t->Name){ if(t->next==NULL) return; else{ FindTree(vs,vt,t->next,len,pos); } } if(vs[pos]==t->Name){ vt.push_back(t); FindTree(vs,vt,t->child,len-1,pos+1); }}
这个函数递归查找,直到查找到文件,或查找失败,在查找的同时,把节点指针存入vecotor<Treefile*> vt 。
/* 该树用于存储文件的名字,没有儿子的节点可能是 文件夹 或者 文件, 存储路径时可以用一个vector来存储各个文件夹,例如“C:\AA\BB\CC”, 可以用vector把AA、BB、CC按序存储,如果要返回,则调用vector的pop 操作,要进入一个文件夹,则调用vector的push操作,把文件夹的名字 存入vector。 2013-11-25 22:04 by JackWoo 以下代码建立如下图的文件树: A / \B C / \ D E / \ F G*/#include "stdafx.h"#include<iostream>#include<string>#include<vector>using namespace std;/* child 用于存储一个儿子节点,parent用于存储父亲节点 next用于存储儿子节点的兄弟*/struct Treefile{string Name;struct Treefile* child;struct Treefile* parent;struct Treefile* next; };void BeChild(struct Treefile* tParent,struct Treefile* tChild){//struct Treefile* pt=tParent->child;if(!tParent->child){ tParent->child=tChild; tChild->parent=tParent;}else{tChild->next=tParent->child;tParent->child=tChild;tChild->parent=tParent;} }void OutputTree(struct Treefile* tParent){ struct Treefile* pt=tParent->child; while(pt){ cout<<pt->Name<<' '; pt=pt->next; }}void DeleteNode(struct Treefile* tTree){//struct Treefile* pt=tTree->parent;if(tTree->parent->child==tTree){tTree->parent->child=tTree->next;delete tTree;}else{struct Treefile* pt=tTree->parent->child;while(pt&&pt->next!=tTree){pt=pt->next;}if(pt==NULL){return;}else{pt->next=tTree->next;delete tTree;}}}void FindTree(vector<string>& vs,vector<Treefile*>&vt,Treefile *t,int len,int pos){//vs存了路径中包含的文件名,vt用来存入的树中与vs包含的文件名相同的节点指针//len 表示vs中的个数,pos表示从vs中的第几个节点开始查找(从0开始) if(len==0 ) return; if(vs[pos]!=t->Name){ if(t->next==NULL) return; else{ FindTree(vs,vt,t->next,len,pos); } } if(vs[pos]==t->Name){ vt.push_back(t); FindTree(vs,vt,t->child,len-1,pos+1); }}int _tmain(int argc, _TCHAR* argv[]){system("title TreeFile");vector<string> vs;//用于存放根文件名字,即目录中涉及到的所有文件名vector<Treefile*> vt;struct Treefile* A=new Treefile;struct Treefile* B=new Treefile;struct Treefile* C=new Treefile;struct Treefile* D=new Treefile;struct Treefile* E=new Treefile;struct Treefile* F=new Treefile;struct Treefile* G=new Treefile;string sA="A",sB="B",sC="C",sD="D",sE="E",sF="F",sG="G";//定义各个节点的名字A->Name=sA;B->Name=sB;C->Name=sC;D->Name=sD;E->Name=sE;F->Name=sF;G->Name=sG;//构造树BeChild(A,B);BeChild(A,C);BeChild(B,D);BeChild(B,E);BeChild(E,F);BeChild(E,G);//把目录涉及到的文件名字顺序存入容器:存入A,B,E,Gvs.push_back(sA);vs.push_back(sB);vs.push_back(sE);vs.push_back(sG);//验证代码FindTree(vs,vt,A,4,0);cout<<"vt's size is "<<vt.size()<<endl;for(int i=0;i<vt.size();++i)cout<<vt[i]->Name<<endl;return 0;}以上代码是为了验证FindTree()的,输出结果应该为"A,B,E,G"。输出结果:
- 实现简单文件树
- 文件上传简单实现
- java 实现文件简单比较
- 文件上传原理简单实现
- java简单实现复制文件
- VC 简单实现文件监控
- 文件归并排序简单实现
- php上传文件简单实现
- 加密文件简单的实现
- js简单实现文件下载
- [Java] 简单文件数据库实现
- 简单实现文件的读写
- python简单实现文件下载
- 简单实现Windows和Linux文件共享
- 简单的Sturts上传文件实现
- 简单实现Windows和Linux文件共享
- 实现简单的struts,hibernate上传文件
- java实现的简单文件管理系统
- HaarTraining训练分类器
- Parsing $_SERVER['PATH_INFO']
- visual studio 中集成对vim的调用
- SAP 库存盘点 步骤--(详细)
- C++ STL 智能指针(二)
- 实现简单文件树
- office中快捷键的设置
- (转) Bitmap用法总结
- 学习Java需要达到的25个目标
- hdu 1890 Robotic Sort (Splay树)
- 一写多读一致性保证算法
- ubuntu 开机 自动挂载 airport time capsule
- 标准C知识04
- Oracle 维护redo日志