//求深度的那儿得好好推敲 #include<iostream> #include<assert.h> using namespace std; typedef char elemType; enum NodeType { HEAD, VALUE, SUB }; struct GNode { NodeType _type; GNode *_next; union { elemType _value; GNode *_subList; }; GNode(NodeType type=HEAD,elemType value=0) :_type(type) ,_next(NULL) { if(_type==VALUE) _value=value; else if(_type==SUB) _subList=NULL; } }; class Generalizedlist { public: Generalizedlist(const elemType* str) { _head=_CreateList(str); } Generalizedlist(Generalizedlist& g) { _head=_Copy(g._head); } Generalizedlist& operator=(Generalizedlist& g) { _head=_Copy(g._head); return *this; } ~Generalizedlist() { Clear(); } void Clear() { _Clear(_head); _head=NULL; } void Print() { _Print(_head); cout<<endl; } int NodeSize()//节点数目,包括head,sub,value { return _NodeSize(_head); } int Size()//类型为value的节点数目 { return _Size(_head); } int Depth()/// { return _Depth(_head); } protected: GNode* _head; GNode* _CreateList(const elemType* &str) { assert(*str==‘(‘); GNode* head=new GNode(HEAD);// GNode* cur=head; ++str; while(*str!=‘\0‘) { if(*str==‘(‘)//子表 //为什么只有‘(‘,不用str++?? //递归里str++了 { GNode* sub=new GNode(SUB); cur->_next=sub; cur=cur->_next; sub->_subList=_CreateList(str);//递归创建子表并链接到子表节点后面 } else if(*str==‘)‘) { ++str; return head;//>>?? //递归返回条件 } else if(_IsValue(*str)) { GNode* node=new GNode(VALUE,*str); cur->_next=node; cur=cur->_next; ++str; } else { ++str; } } return head; } bool _IsValue(elemType value) { if((value>=‘0‘&&value<=‘9‘)||(value>=‘A‘&&value<=‘Z‘)||(value>=‘a‘&&value<=‘z‘)) return true; else return false; } GNode* _Print(GNode* head) { GNode* cur=head; while(cur) { if(cur->_type==HEAD) { cout<<"("; } else if(cur->_type==VALUE) { cout<<cur->_value; if(cur->_next!=NULL) { cout<<","; } } else if(cur->_type==SUB) { _Print(cur->_subList); if(cur->_next!=NULL)// { cout<<","; } } cur=cur->_next; } cout<<")"; return head; } void _Clear(GNode* head) { GNode* cur=head; while(cur) { if(cur->_type==HEAD||cur->_type==VALUE) { GNode* del=cur; cur=cur->_next; delete del; } else//有问题 { GNode* del=cur; cur=cur->_next; _Clear(del->_subList); delete del; } } } int _NodeSize(GNode* head) { GNode* cur=head; int size=0; while(cur) { /*if(cur->_type==HEAD||cur->_type==VALUE) { cur=cur->_next; size++; }*/ if(cur->_type==SUB) { size+=_NodeSize(cur->_subList); } cur=cur->_next; size++; } return size; } int _Size(GNode* head) { int size=0; GNode* cur=head; while(cur) { if(cur->_type==VALUE) { size++; } else if(cur->_type==SUB) { size+=_Size(cur->_subList); } cur=cur->_next; } return size; } int _Depth(GNode* head) { GNode* cur=head; int max=1; int depth=0; while(cur) { if(cur->_type==SUB) { depth=_Depth(cur->_subList); depth++; } if((depth+1)>max) { max=depth; } cur=cur->_next; } return max; } GNode* _Copy(GNode* head)/////// { GNode* cur=head; cur=cur->_next; GNode* newHead=new GNode(HEAD); GNode* newCur=newHead; while(cur) { if(cur->_type==VALUE) { GNode* node=new GNode(VALUE,cur->_value); newCur->_next=node; newCur=newCur->_next; } else if(cur->_type==SUB) { GNode* node=new GNode(SUB); newCur->_next=node; newCur=newCur->_next; newCur->_subList=_Copy(cur->_subList); } cur=cur->_next; } return newHead; } }; void Test1() { char* str1="()"; Generalizedlist gl1(str1); gl1.Print(); char* str2="(a,b)"; Generalizedlist gl2(str2); gl2.Print(); char* str3="(a,(b,c),d)"; Generalizedlist gl3(str3); gl3.Print(); char* str4="(a,b,(c,d,(e)),(f))"; Generalizedlist gl4(str4); gl4.Print(); /*gl1.Clear(); gl1.Print(); gl2.Clear(); gl2.Print(); gl3.Clear(); gl3.Print(); gl4.Clear(); gl4.Print();*/ /*cout<<gl1.NodeSize()<<endl; cout<<gl2.NodeSize()<<endl; cout<<gl3.NodeSize()<<endl; cout<<gl4.NodeSize()<<endl; cout<<gl1.Size()<<endl; cout<<gl2.Size()<<endl; cout<<gl3.Size()<<endl; cout<<gl4.Size()<<endl;*/ cout<<gl1.Depth()<<endl; cout<<gl2.Depth()<<endl; cout<<gl3.Depth()<<endl; cout<<gl4.Depth()<<endl; } void Test2() { char* str1="()"; Generalizedlist gl1(str1); gl1.Print(); Generalizedlist g1(gl1); g1.Print(); Generalizedlist G1=gl1; G1.Print(); char* str2="(a,b)"; Generalizedlist gl2(str2); gl2.Print(); Generalizedlist g2(gl2); g2.Print(); Generalizedlist G2=gl2; G2.Print(); char* str3="(a,(b,c),d)"; Generalizedlist gl3(str3); gl3.Print(); Generalizedlist g3(gl3); g3.Print(); Generalizedlist G3=gl3; G3.Print(); char* str4="(a,b,(c,d,(e)),(f))"; Generalizedlist gl4(str4); gl4.Print(); Generalizedlist g4(gl4); g4.Print(); Generalizedlist G4=gl4; G4.Print(); } int main() { //Test1(); Test2(); system("pause"); return 0; }
本文出自 “sunshine225” 博客,请务必保留此出处http://10707460.blog.51cto.com/10697460/1757307
原文地址:http://10707460.blog.51cto.com/10697460/1757307