标签:广义表的实现
广义表是非线性的结构,是线性表的一种扩展,是有n个元素组成有限序列。
广义表的定义是递归的,因为在表的描述中又得到了表,允许表中有表。
eg:
<1> A = ()
<2> B = (a,b)
<3> C = (a,b,(c,d))
<4> D = (a,b,(c,d),(e,(f),h))
<5> E = (((),()))
#ifndef GENERALIZEDLIST__H_ #define GENERALIZEDLIST__H_ #include<iostream> #include<ctype.h> #include<assert.h> using namespace std; enum NodeType { HEAD, VALUE, SUB }; struct GeneralizedNode { NodeType _type; GeneralizedNode* _next; union { char _value; GeneralizedNode* _subLink; }; GeneralizedNode(NodeType type=HEAD,char value=0) :_type(type) ,_next(NULL) { if(_type==VALUE) _value=value; else if(_type==SUB) _subLink=NULL; } }; class GeneralizedList { protected: GeneralizedNode* _head; protected: bool _Valid(char c) { //isalpha()不是字母返回0,大写返回1,小写返回2 //isdigit()为宏,不是函数,如果是阿拉伯数字返回true,否则返回NULL(0) if(0!=isalpha(c)||isdigit(c)) return true; return false; } GeneralizedNode* _CreateList(const char*& str) { assert(*str==‘(‘); GeneralizedNode* head=new GeneralizedNode(HEAD); GeneralizedNode* cur=head; ++str; while(*str!=‘\0‘) { if(*str==‘(‘) { GeneralizedNode* sub=new GeneralizedNode(SUB); cur->_next=sub; cur=cur->_next; cur->_subLink=_CreateList(str); } else if(*str==‘)‘) { ++str; return head; } else if(_Valid(*str)) { GeneralizedNode* node=new GeneralizedNode(VALUE,*str); cur->_next=node; cur=cur->_next; ++str; } else { ++str; } } return head; } int _Size(GeneralizedNode* g) { GeneralizedNode* cur=g; int size=0; while(cur) { if(cur->_type==VALUE) ++size; else if(cur->_type==SUB) size+=_Size(cur->_subLink); cur=cur->_next; } return size; } void _Display(GeneralizedNode* g) { GeneralizedNode* cur=g; while(cur) { if(cur->_type==HEAD) cout<<‘(‘; else if(cur->_type==VALUE) { cout << cur->_value; if (cur->_next) cout << ‘,‘; } else { _Display(cur->_subLink); if (cur->_next) cout << ‘,‘; } cur=cur->_next; } cout<<‘)‘; } //递归:在同一层次找出子表深度最大的 int _Depth(GeneralizedNode* head) { int depth = 1; GeneralizedNode* cur = head; while (cur) { if (cur->_type == SUB) { int subDepth = _Depth(cur->_subLink); if (subDepth + 1 > depth) depth = subDepth + 1; } cur = cur->_next; } return depth; } void _Clear(GeneralizedNode* g) { GeneralizedNode* cur = g; while (cur) { if (cur->_type == SUB) _Clear(cur->_subLink); GeneralizedNode* del = cur; cur = cur->_next; delete del; } } GeneralizedNode* _Copy(GeneralizedNode* g) { GeneralizedNode* cur = g; GeneralizedNode* head =NULL; GeneralizedNode* newCur = head; while (cur) { if (cur->_type == HEAD) { head = new GeneralizedNode(HEAD); newCur = head; } else if (cur->_type == VALUE) { GeneralizedNode* node = new GeneralizedNode(VALUE, cur->_value); newCur->_next = node; newCur = newCur->_next; } else { newCur->_next = new GeneralizedNode(SUB); newCur = newCur->_next; newCur->_subLink = _Copy(cur->_subLink); } cur = cur->_next; } return head; } public: GeneralizedList(const char* str) :_head(_CreateList(str)) {} GeneralizedList(const GeneralizedList& g) { _head = _Copy(g._head); } GeneralizedList& operator=(GeneralizedList g) { swap(g._head, _head); return *this; } ~GeneralizedList() { Clear(); _head = NULL; } int Size() { return _Size(_head); } int Depth() { return _Depth(_head); } void Clear() { _Clear(_head); } void Print() { _Display(_head); cout<<endl; } }; #endif #include"GeneralizedList.h" int main() { char* str1 = "()"; char* str2 = "(a,b)"; char* str3 = "(a,b,(c,d))"; char* str4 = "(a,b,(c,d),(e,(f),g))"; GeneralizedList g1(str1); GeneralizedList g2(str2); GeneralizedList g3(str3); GeneralizedList g4(str4); g1.Print(); g2.Print(); g3.Print(); g4.Print(); cout << g1.Size() << " " << g2.Size() << " " << g3.Size() << " " << g4.Size() << endl; cout << g1.Depth() << " " << g2.Depth() << " " << g3.Depth() << " " << g4.Depth() << endl; GeneralizedList g5(g4); g5.Print(); g5 = g2; g5.Print(); system("pause"); return 0; }
本文出自 “小止” 博客,请务必保留此出处http://10541556.blog.51cto.com/10531556/1748094
标签:广义表的实现
原文地址:http://10541556.blog.51cto.com/10531556/1748094