关于文件管理系统的数据结构模拟

windows操作系统中的文件夹的数据结构貌似树,不是二叉树,而是普通数.
可以如下设计,不过只是框架,很多都没有考虑到

程序代码:

template <class T>
class DIRSTRUCT
{    
    DIRSTRUCT * lpParentDir;
    vector<DIRSTRUCT*> SubDir;
    int m_nSubDir;
    T file;
private:
    void DisPlay(const DIRSTRUCT<T>* lpDir)
    {        
        cout<<lpDir->file<<endl;
        for(int i=0;i<lpDir->m_nSubDir;i++)
        {
            DisPlay(lpDir->SubDir[i]);
        }
    }
    void Delete(DIRSTRUCT<T> *lpDir)
    {
        if(lpDir->m_nSubDir)
        {
            for(int i=lpDir->m_nSubDir-1;i>=0;i--)
            {
                Delete(lpDir->GetChild(i));
                delete lpDir->GetChild(i);
                lpDir->SubDir.pop_back();
            }
        }
        lpDir->lpParentDir=NULL;
        lpDir->m_nSubDir=0;
    }
    void Clone(DIRSTRUCT<T> *lp,const DIRSTRUCT<T> *lpDir)
    {
        for(int i=0;i<lpDir->m_nSubDir;i++)
        {
            lp->AddChild(lpDir->SubDir[i]->file);
            Clone(lp->SubDir[i],lpDir->SubDir[i]);
        }
    }
public:
    DIRSTRUCT<T>()
    {
        file=0;
        m_nSubDir=0;
        lpParentDir=NULL;
    }
    DIRSTRUCT<T>(T t,DIRSTRUCT* lpParent=NULL)
    {
        m_nSubDir=0;
        lpParentDir=lpParent;
        file=t;
    }
    DIRSTRUCT(const DIRSTRUCT<T>& dir)
    {
        Copy(&dir);
    }
    DIRSTRUCT<T> &operator=(const DIRSTRUCT<T>& dir)
    {
        Copy(&dir);
        return *this;
    }
    ~DIRSTRUCT<T>()
    {
        remove();
    }
    void Copy(const DIRSTRUCT<T>* lpDir)
    {
        remove();
        file=lpDir->file;
        Clone(this,lpDir);
    }
public:
    void AddChild(T t)
    {
        m_nSubDir++;
        DIRSTRUCT* lpDir=new DIRSTRUCT<T>(t,this);
        SubDir.push_back(lpDir);
    }
    int GetCount()
    {
        return SubDir.size();
    }
    DIRSTRUCT<T>* GetChild(int i)
    {
        return SubDir.at(i);
    }
    DIRSTRUCT<T>* operator[](int i)
    {
        return SubDir.at(i);
    }
    DIRSTRUCT<T>* GetParent()
    {
        return lpParentDir;
    }
    bool IsChild()
    {
        return lpParentDir;
    }
    void show()
    {
        DisPlay(this);
    }
    void remove()
    {
        Delete(this);
    }
};



测试代码,主要是针对几个构造函数,'='的测试。

程序代码:


void main()
{
    DIRSTRUCT<int> dir;
    dir.AddChild(1);
    dir.AddChild(2);
    dir[0]->AddChild(11);
    DIRSTRUCT<int> dir2;
    dir.show();
    cout<<endl;
    dir2.show();
    cout<<endl;
    dir2.Copy(&dir);
    dir2.show();
    dir.remove();
    cout<<endl;
    dir2.show();
    cout<<endl;
    dir.show();
    dir.Copy(&dir2);
    cout<<endl;
    dir.show();
    DIRSTRUCT<int> dir3(dir);
    cout<<endl;
    dir3.show();
    DIRSTRUCT<int> dir4;
    dir4=dir3;
    cout<<endl;
    dir4.show();
    cout<<endl;
    dir4.GetChild(0)->show();
    cout<<endl;
    dir4.GetChild(0)->GetParent()->show();
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值