首页 > 编程知识 正文

c++实例化对象,qtc0008

时间:2023-05-04 17:55:20 阅读:156499 作者:4776

树数据结构是一种重要的非线性数据结构。 树的数据结构可以表示数据表元素之间的一对多的关系。 其中树和二叉树最为常用,直观上,树是由分支关系定义的层次结构。 树的数据结构广泛存在于客观世界中,例如人类社会谱系和各种社会组织机构可以用树的数据结构来形象化。

由于树结构具有无限扩展的特性,最适合组织节点深度不定的数据,基本实现方法是通过对象保持父对象与子对象的指针来实现A-B-C。

实施步骤:

1 .定义基类

class BaseNode{private: //指向父节点的指针BaseNode*m_parent_node=nullptr; //子节点们QListBaseNode*m_child_nodes; //节点名称QString m_name; //子节点数int m_childNum=0; 由public://name和parent构建,root节点parent=nullptrbasenode (qstring name,BaseNode*parent=nullptr ) :m_parent m_name(name ) voidsetparentnode ) basenode*parent ) { m_parent_node=parent; }将}voidaddcildnode(basenode*node ) /子节点设置为parent node-setparent node (this ); m_child_nodes.push_back(node; m_childNum =1; }voidsetname(qstringname ) { m_name=name; } QString getName () { return m_name; } QListBaseNode* childs () { return m_child_nodes; } int childNum () { return m_childNum; }; 2 .子类继承

class step 3360 publicbasenode { public : step (qstring name,Step*parent=nullptr ) :basenode ) name,parent

intmain(intargc,char * argv [ ] ) (step * step _ main=new step (' program ' ) ); step*step_1=newstep(step_1); step*step_2=newstep(step_2); step*step_3=newstep(step_3); step*step_4=newstep(step_4); step_main-addcildnode(step_1; step_1-addcildnode(step_2; step_1-addcildnode(step_3; step_1-addcildnode(step_4; step _3- addcildnode (new step (step _3_1) ); step _3- addcildnode (new step (step _3_1) ); step _3- addcildnode (new step (step _3_1) ); step _3- addcildnode (new step (step _3_1) ); } 4.使用树结构生成XML的方法:递归生成

voidbuildnodetoxml (base node * node,QDomDocumentm_domDocument,QDomElementparent_element ) { for } inti=0; i node-childNum (; I//XML节点qdomelementthis _ element=m _ DOM document.createelement (node-childs ([ I ]-getname ) ); //将刚创建的节点添加到父节点parent _ element parent _ element.appendchild (this _ element )中的qdebug(node-childs ) [ I ] //递归调用buildnodetoxml (node-childs ).at (I ),m_domDocument,this_element ); }boolwrite(qiodevice*device,QDomDocument document ) { const int IndentSize=4; qtextstreamout(device; document.save(out,IndentSize; 返回真; }intmain(intargc,char *argv[] ) step * step _ main=new step (' program ' ); step*step_1=newstep(step_1); step*step_2=newstep(step_2); step*step_3=newstep(step_3); step*step_4=newstep(step_4); step_main-addcildnode(step_1; step_1-addcildnode(step_2; step_1-addcildnode(step_3; step_1-addcildnode(step_4; step _3- addcildnode (new step (step _3_1) ); step _3- addcildnode (new step (step _3_1) ); step _3- addcildnode (new step (step _3_1) ); step _3- addcildnode (new step (step _3_1) ); //Qt的XML文件操作对象QDomDocument m_domDocument; //root节点qdomelementelement=m _ DOM document.createelement (' program ); //将根节点添加到最外层的m _ DOM document.appendchild (element ); //从树结构的开头开始构建XMLbuildnodetoxml(step_main,m_domDocument,element )的qtringfilename=' d :\ op tiger2. XML qfilefile (文件名称; file.open (q file :3360只写一次| q file :3360文本); write(file,m_domDocument; q debug (m _ DOM document.child nodes ) ).size ); } 后记

1.Step可以实现更复杂的数据结构;

2 .可以添加析构函数,以便在用delete去掉step_Main之后,下面的后代去掉delete

~BaseNode () for ) intI=0; i m_childNum; I ) { delete m_child_nodes[i]; qdebug(m_name'isdeleted(n ' ); }3.生成的XML文件

program step _1step _2/step _3_1/step _3_1/step _3_1///step _3///step _3/1/step。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。