标签:广义表
广义表(Lists,又称列表)是一种非线性的数据结构,是线性表的一种推广。即广义表中放松对表元素的原子限制,容许它们具有其自身结构。
#include<cassert> #include<iostream> using namespace std; enum Type { HEAD, VALUE, SUB, }; struct GeneralizedNode { Type _type; GeneralizedNode* _next; union { int _value; GeneralizedNode* _sublink; }; GeneralizedNode(Type type = HEAD, int value = 0) :_type(type) ,_next(NULL) { if (_type == VALUE) { _value = value; } if (_type == SUB) { _sublink = NULL; } } }; class Generalized { public: Generalized() :_head(NULL) {} Generalized(const char* str) :_head(NULL) { _head = _Creatlize(str); } Generalized(const Generalized& g) { } //Generalized& operator=(const Generalized& g)//传统写法 //{ // if (this != &g) // { // GeneralizedNode *temp=_Copy(g._head); // _Destroy(_head); // _head = temp; // } // return *this; //} Generalized& operator=(Generalized g)//现代写法 { swap(_head, g._head); return *this; } ~Generalized() {} size_t Size() { return _size(_head); } size_t Depth() { return _Depth(_head); } void print() { _print(_head); } protected: bool ISValue(char m) //判断是否为有效值 { if (m >= ‘a‘&&m <= ‘z‘ || m >= ‘A‘&&m <= ‘Z‘ || m >= ‘0‘&&m <= ‘9‘) { return true; } else { return false; } } void _print(GeneralizedNode* head) //打印广义表 { assert(head); GeneralizedNode *cur = head; while (cur) { if (cur->_type == HEAD) { cout << "(" << ""; //cur = cur->_next; } else if (cur->_type == VALUE) { cout << cur->_value; if (cur->_next == NULL) { cout << ")"; } else { cout << ","; } } else if (cur->_type==SUB) { _print(cur->_sublink); cout << ","; } else { // cout << ")"; } cur = cur->_next; } //cout << endl; } size_t _size(GeneralizedNode* p) //求广义表的size { GeneralizedNode *cur = p; int count = 0; while (cur) { if (cur->_type == VALUE) { ++count; } else if (cur->_type == SUB) { count += _size(cur->_sublink); } cur = cur->_next; } return count; } int _Depth(GeneralizedNode* head) //求广义表深度 { GeneralizedNode* cur = head; int depth = 1; while (cur) { if (cur->_type == SUB) { int subdepth = _Depth(cur->_sublink); if (subdepth + 1 > depth) { depth = subdepth + 1; } } cur = cur->_next; } return depth; } GeneralizedNode* _Creatlize(const char*& str)//创建节点 ---方法1 { while (*str) { if (*str == ‘(‘) { GeneralizedNode* _head = new GeneralizedNode(HEAD,‘0‘); // _head->_type = HEAD; GeneralizedNode* cur = _head; //cur->_type = HEAD; ++str; while (*str) { if (ISValue(*str)) { GeneralizedNode *temp = new GeneralizedNode(VALUE,*str); //temp->_type = VALUE; //temp->_value = *str; cur->_next = temp; cur = cur->_next; ++str; } else if (*str == ‘(‘) { GeneralizedNode* sub = new GeneralizedNode; sub->_type = SUB; sub->_sublink = _Creatlize(str); cur->_next = sub; cur = cur->_next; } else if (*str == ‘)‘) { ++str; return _head; } else { ++str; } } return _head; } } return _head; } //GeneralizedNode* _Creatlize(const char*& str) ///---------方法2 //{ // assert(*str == ‘(‘); // GeneralizedNode* head = new GeneralizedNode(HEAD, ‘0‘); // GeneralizedNode* cur = head; // str++; // while (str != ‘\0‘) // { // if ((*str >= ‘0‘&&*str <= ‘9‘) || (*str >= ‘a‘&&*str <= ‘z‘) || (*str >= ‘A‘&&*str <= ‘Z‘)) // { // cur->_next = new GeneralizedNode(VALUE, *str); // cur = cur->_next; // } // else if (*str == ‘(‘) // { // cur->_next = new GeneralizedNode(SUB); // cur = cur->_next; // cur->_sublink = _Creatlize(str); // } // else if (*str == ‘)‘) // { // return head; // } // str++; // } // return head; //} GeneralizedNode* _Copy(GeneralizedNode* head) { GeneralizedNode* newhead = new GeneralizedNode(HEAD); GeneralizedNode* cur = head->_next; GeneralizedNode* newcur = newhead; while (cur) { if (cur->_type == VALUE) { newcur->_next = new GeneralizedNode(VALUE, cur->_value); newcur = newcur->_next; } else if (cur->_type == SUB) { newcur->_next = new GeneralizedNode(SUB); newcur = newcur->_next; newcur->_sublink = _Copy(cur->_sublink); } cur = cur->_next; } } protected: GeneralizedNode* _head; }; void test() //测试 { Generalized a("(a,b)"); Generalized b("(a,(c,(f),d),b)"); b.print(); cout << endl; int m = a.Depth(); int n = b.Size(); cout << n<< endl; }
本文出自 “菜鸟的经历” 博客,请务必保留此出处http://10738432.blog.51cto.com/10728432/1765330
标签:广义表
原文地址:http://10738432.blog.51cto.com/10728432/1765330