其中包括廣義表的創建、輸出、拷貝構造、賦值運算符重載、析構、有效數據個數以及廣義表深度
#pragma once #include<iostream> #include<assert.h> #include<ctype.h> 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; } } }; class GeneralizedList { public: GeneralizedList() :_head(NULL) {} GeneralizedList(const char* str) //創建表 { _head = _CreateList(str); } //拷貝構造 GeneralizedList(const GeneralizedList& s) { _head = _copy(s._head); } //賦值運算符重載 GeneralizedNode* operator=(GeneralizedList s) { swap(_head, s._head); return _head; } //析構函數 ~GeneralizedList() { _Dele(_head); } void PrintList()//輸出 { _PrintNode(_head); } size_t size() { size_t num = 0; _size(_head, num); return num; } size_t Depth() { return _depth(_head); } private: GeneralizedNode* _CreateList(const char*& str) { // GeneralizedList b2("(a,(b,c))"); assert('(' == *str); ++str; //跳過'(' GeneralizedNode* head = new GeneralizedNode(HEAD); GeneralizedNode* cur = head; //除數字字符與')'其他字符都不需要考慮,直接跳過就可以 while (')' != *str) { if ((*str >= '0'&&*str <= '9') || /*也可以使用數字字符判別函數 isdigit(*str)==-1*/ (*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, *str); cur->_next->_subLink = _CreateList(str); cur = cur->_next; } else { str++; } } str++;//跳過 ')' return head; } GeneralizedNode* _copy(GeneralizedNode* head) { GeneralizedNode* dst = new GeneralizedNode(HEAD); GeneralizedNode* cur = dst; if (head == NULL) return NULL; while (head != NULL) { if (head->_type == VALUE) { cur->_next = new GeneralizedNode(VALUE, head->_value); cur = cur->_next; } else if (head->_type == SUB) { cur->_next = new GeneralizedNode(SUB); cur->_next->_subLink = _copy(head->_subLink); cur = cur->_next; } head = head->_next; //cur->_next = new GeneralizedNode(VALUE, head->_value); } return dst; } void _PrintNode(GeneralizedNode* head) { // GeneralizedList b2("(a,(b,c))"); GeneralizedNode* tmp = head; while (tmp != NULL) { if (HEAD == tmp->_type) { cout << "("; tmp = tmp->_next; } else if (VALUE == tmp->_type) { cout << (char)tmp->_value; tmp = tmp->_next; if (tmp != NULL) { cout << ", "; } } else { _PrintNode(tmp->_subLink); if (tmp->_next != NULL) { cout << ", "; } tmp = tmp->_next; } } cout << ")"; } void _size(GeneralizedNode* head, size_t& size) { GeneralizedNode* tmp = head; while (tmp != NULL) { if (HEAD == tmp->_type) { tmp = tmp->_next; } else if (VALUE == tmp->_type) { ++size; tmp = tmp->_next; } else { _size(tmp->_subLink, size); tmp = tmp->_next; } } } size_t _depth(GeneralizedNode* head) { size_t depth = 1; size_t count = 0; GeneralizedNode* tmp = head; while (tmp != NULL) { if (SUB == tmp->_type) { size_t subdep = _depth(tmp->_subLink); if (subdep + 1 > depth) { depth = subdep + 1; } } tmp = tmp->_next; } return depth; } void _Dele(GeneralizedNode* head) { if (head == NULL) return; else if (head->_type == SUB) { _Dele(head->_subLink); _Dele(head->_next);//注意 在刪除SUB結點後緊接着刪除下一結點 //cout << "dele" << " "; delete head; } else { _Dele(head->_next); //cout << "dele" << " "; delete head; } } private: GeneralizedNode* _head; }; #include"GeneralizedList.h" void test() { GeneralizedList b1("(a,b)"); b1.PrintList(); cout << endl; GeneralizedList b2("(a,(b,c))"); b2.PrintList(); cout << endl; GeneralizedList b3("(a,(b,c),d)"); b3.PrintList(); cout << endl; GeneralizedList b4("(a,(b,c),(d,(e),f),(h,i))"); b4.PrintList(); cout << "\n" << endl; cout << "b1.size = " << b1.size() << endl; cout << "b2.size = " << b2.size() << endl; cout << "b3.size = " << b3.size() << endl; cout << "b4.size = " << b4.size() << endl; cout << "b1.Depth = " << b1.Depth() << endl; cout << "b2.Depth = " << b2.Depth() << endl; cout << "b3.Depth = " << b3.Depth() << endl; cout << "b4.Depth = " << b4.Depth() << endl; GeneralizedList b5("(a,(b,c),(h,i),(d,(e),f))"); cout << "b5.Depth = " << b5.Depth() << endl; GeneralizedList b6(b2); b6.PrintList(); cout << endl; GeneralizedList b7(b3); b7.PrintList(); GeneralizedList b8; b8 = b7; b8.PrintList(); b7.PrintList(); } int main() { test(); system("pause"); return 0; }