您好,登錄后才能下訂單哦!
廣義表:非線性結構,是線性表的一種擴展,是有n個元素組成有限序列,是遞歸的,因為在表的描述中又得到了表,允許表中有表。
#include<cassert> #include<iostream> using namespace std; enum Type //枚舉節點的類型 { HEAD, //頭結點 VALUE, //有數據成員的節點 SUB, //有子鏈的節點 }; template<class T> struct GeneralizedNode //定義節點 { Type _type; GeneralizedNode* _next; union //運用聯合體使得該數據成員只含有一種節點 { T _value; GeneralizedNode* _sublink; }; GeneralizedNode(Type type = HEAD, T value = 0) //構造節點 :_type(type) ,_next(NULL) { if (_type == VALUE) { _value = value; } if (_type == SUB) { _sublink = NULL; } } }; template<class T> class Generalized { public: Generalized() :_head(NULL) {} Generalized(const char* str) //構造函數 :_head(NULL) { _head = _Creatlize(str); } Generalized(const Generalized& g) //拷貝構造 { _head = _Copy(g._head); } Generalized& operator=(const Generalized& g) //傳統寫法 { if (this != &g) { GeneralizedNode *temp=_Copy(g._head); _Destroy(_head); _head = temp; } return *this; } Generalized<T>& operator=(Generalized g) //現代寫法 { swap(_head, g._head); return *this; } 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<T>* head) //打印節點 運用遞歸方式進行 { assert(head); GeneralizedNode<T> *cur = head; while (cur) { if (cur->_type == HEAD) { cout << "(" << ""; //cur = cur->_next; } else if (cur->_type == VALUE) //如果是VALUE,則打印該節點 { cout << cur->_value; if (cur->_next == NULL) { cout << ")"; } else { cout << ","; } } else if (cur->_type == SUB) //如果是SUB類型,則遞歸調用下一層 { _print(cur->_sublink); if (cur->_next == NULL) { cout << ")"; } else { cout << ","; } } else { cout << ")"; } cur = cur->_next; } } size_t _size(GeneralizedNode<T>* p) { GeneralizedNode<T> *cur = p; int count = 0; while (cur) { if (cur->_type == VALUE) //如果是VALUE,則count++ { ++count; } else if (cur->_type == SUB) //如果是SUB,則調用下一層 { count += _size(cur->_sublink); } cur = cur->_next; } return count; } int _Depth(GeneralizedNode<T>* head) { GeneralizedNode<T>* 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<T>* _Creatlize(const char*& str) //構造廣義表 { assert(*str == '('); while (*str) { if (*str == '(') { GeneralizedNode<T>* _head = new GeneralizedNode<T>(HEAD); GeneralizedNode<T>* cur = _head; ++str; while (*str) { if (ISValue(*str)) { GeneralizedNode<T> *temp = new GeneralizedNode<T>(VALUE); temp->_value = *str; cur->_next = temp; cur = cur->_next; ++str; } else if (*str == '(') { GeneralizedNode<T>* sub = new GeneralizedNode<T>(SUB); sub->_sublink = _Creatlize(str); cur->_next = sub; cur = cur->_next; } else if (*str == ')') { ++str; return _head; } else { ++str; } } return _head; } } return _head; } GeneralizedNode<T>* _Copy(GeneralizedNode<T>* head) //拷貝 { GeneralizedNode<T>* newhead = new GeneralizedNode<T>(HEAD); GeneralizedNode<T>* cur = head->_next; GeneralizedNode<T>* newcur = newhead; while (cur) { if (cur->_type == VALUE) { newcur->_next = new GeneralizedNode<T>(VALUE, cur->_value); newcur = newcur->_next; } else if (cur->_type == SUB) { newcur->_next = new GeneralizedNode<T>(SUB); newcur = newcur->_next; newcur->_sublink = _Copy(cur->_sublink); } cur = cur->_next; } return newhead; } protected: GeneralizedNode<T>* _head; };
測試代碼如下:
void test4() { Generalized<char> a("(a,b)"); Generalized<char> b("(a,(c,(f),d),b)"); Generalized<char> c(a); Generalized<char> d(b); c.print(); cout<< endl; d.print(); cout << endl; cout << d.Depth()<<endl; cout << d.Size()<<endl; } int main() { test4(); system("pause"); return 0; }
運行結果如下:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。