廣義表:非線性結(jié)構(gòu),是線性表的一種擴(kuò)展,是有n個(gè)元素組成有限序列,是遞歸的,因?yàn)樵诒淼拿枋鲋杏值玫搅吮?,允許表中有表。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名與空間、雅安服務(wù)器托管、營(yíng)銷軟件、網(wǎng)站建設(shè)、岑鞏網(wǎng)站維護(hù)、網(wǎng)站推廣。
#include#include using namespace std; enum Type //枚舉節(jié)點(diǎn)的類型 { HEAD, //頭結(jié)點(diǎn) VALUE, //有數(shù)據(jù)成員的節(jié)點(diǎn) SUB, //有子鏈的節(jié)點(diǎn) }; template struct GeneralizedNode //定義節(jié)點(diǎn) { Type _type; GeneralizedNode* _next; union //運(yùn)用聯(lián)合體使得該數(shù)據(jù)成員只含有一種節(jié)點(diǎn) { T _value; GeneralizedNode* _sublink; }; GeneralizedNode(Type type = HEAD, T value = 0) //構(gòu)造節(jié)點(diǎn) :_type(type) ,_next(NULL) { if (_type == VALUE) { _value = value; } if (_type == SUB) { _sublink = NULL; } } }; template class Generalized { public: Generalized() :_head(NULL) {} Generalized(const char* str) //構(gòu)造函數(shù) :_head(NULL) { _head = _Creatlize(str); } Generalized(const Generalized& g) //拷貝構(gòu)造 { _head = _Copy(g._head); } Generalized& operator=(const Generalized& g) //傳統(tǒng)寫法 { if (this != &g) { GeneralizedNode *temp=_Copy(g._head); _Destroy(_head); _head = temp; } return *this; } Generalized & operator=(Generalized g) //現(xiàn)代寫法 { swap(_head, g._head); return *this; } size_t Size() //求表中的結(jié)點(diǎn)個(gè)數(shù) { return _size(_head); } size_t Depth() //求深度 { return _Depth(_head); } void print() //打印節(jié)點(diǎn) { _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) //打印節(jié)點(diǎn) 運(yùn)用遞歸方式進(jìn)行 { assert(head); GeneralizedNode *cur = head; while (cur) { if (cur->_type == HEAD) { cout << "(" << ""; //cur = cur->_next; } else if (cur->_type == VALUE) //如果是VALUE,則打印該節(jié)點(diǎn) { cout << cur->_value; if (cur->_next == NULL) { cout << ")"; } else { cout << ","; } } else if (cur->_type == SUB) //如果是SUB類型,則遞歸調(diào)用下一層 { _print(cur->_sublink); if (cur->_next == NULL) { cout << ")"; } else { cout << ","; } } else { cout << ")"; } cur = cur->_next; } } size_t _size(GeneralizedNode * p) { GeneralizedNode *cur = p; int count = 0; while (cur) { if (cur->_type == VALUE) //如果是VALUE,則count++ { ++count; } else if (cur->_type == SUB) //如果是SUB,則調(diào)用下一層 { 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) //構(gòu)造廣義表 { assert(*str == '('); while (*str) { if (*str == '(') { GeneralizedNode * _head = new GeneralizedNode (HEAD); GeneralizedNode * cur = _head; ++str; while (*str) { if (ISValue(*str)) { GeneralizedNode *temp = new GeneralizedNode (VALUE); temp->_value = *str; cur->_next = temp; cur = cur->_next; ++str; } else if (*str == '(') { GeneralizedNode * sub = new GeneralizedNode (SUB); sub->_sublink = _Creatlize(str); cur->_next = sub; cur = cur->_next; } else if (*str == ')') { ++str; return _head; } else { ++str; } } return _head; } } 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; } return newhead; } protected: GeneralizedNode * _head; };
測(cè)試代碼如下:
void test4() { Generalizeda("(a,b)"); Generalized b("(a,(c,(f),d),b)"); Generalized c(a); Generalized d(b); c.print(); cout<< endl; d.print(); cout << endl; cout << d.Depth()< 運(yùn)行結(jié)果如下:
新聞名稱:廣義表的實(shí)現(xiàn)
分享URL:http://weahome.cn/article/ijpjjp.html