您好,登錄后才能下訂單哦!
廣義表(Lists,又稱列表)是一種非線性的數據結構,是線性表的一種推廣。即廣義表中放松對表元素的原子限制,容許它們具有其自身結構。
思想:廣義表就類似下圖的結構,他的大體(下圖第一行)相當于一個帶頭結點的鏈表,
代碼思想,首先要有一個頭結點為HEAD類型,每一個廣義表有且只有一個HEAD,而后每個節點如果有分支則把它定義為SUB類型,SUB便是它分支的一個新頭他有一個sublink指針指向他的第一個元素,如果沒有則是VALUE類型。
#pragma once #include<iostream> #include<assert.h> using namespace std; enum Type { HEAD, VALUE, SUB }; struct generalizelistNode { generalizelistNode * _next; Type _type; union { char _value; generalizelistNode *sublink; }; generalizelistNode(Type type=HEAD,char value=0) :_type(type), _value(value), _next(NULL) { } }; class Generalizelist { protected: bool isvalue(char c) { if (c >= 'a'&&c<='z' || c>='A'&&c <= 'Z' || c>='0'&&c <= '9') { return true; } else { return false; } } public: Generalizelist(char *&str) { _head = generallist(str); } ~Generalizelist() { Empty(_head); } void Print() { _Print(_head); } Generalizelist& operator=(Generalizelist g) { swap(_head, g._head); } Generalizelist(Generalizelist &g) { _head = copy(g._head); } int Size() { return _Size(_head); } int Depth() { return _Depth(_head); } protected: generalizelistNode* generallist(char *&str) { assert(*str == '('); generalizelistNode *head = new generalizelistNode(HEAD); generalizelistNode *cur = head; str++; while (*str) { if (isvalue(*str)) { generalizelistNode *Node = new generalizelistNode(VALUE,*str); cur->_next = Node; cur = Node; str++; } else if (*str == '(') { generalizelistNode *sub = new generalizelistNode(SUB); cur->_next = sub; cur = sub; sub->sublink = generallist(str); } else if (*str==')') { ++str; return head; } else { str++; } } } void _Print(generalizelistNode *head) { generalizelistNode *cur = head; while (cur) { if (cur->_type == HEAD) { cout << '('; } else if (cur->_type == VALUE) { cout << cur->_value; if (cur->_next) { cout << ","; } } else { _Print(cur->sublink); } cur = cur->_next; } cout << ")"; } generalizelistNode * copy(generalizelistNode *&_cur) { generalizelistNode *cur = _cur; generalizelistNode *newhead = new generalizelistNode(HEAD); generalizelistNode *tmp = newhead; cur = cur->_next; while (cur) { if (cur->_type == VALUE) { generalizelistNode *node = new generalizelistNode(VALUE, cur->_value); tmp->_next = node; tmp = node; cur = cur->_next; } else { generalizelistNode *node = new generalizelistNode(SUB); tmp->_next = node; tmp = node; tmp->sublink = copy(cur->sublink); cur = cur->_next; } } return newhead; } void Empty(generalizelistNode *&head) { generalizelistNode *tmp = head; head = head->_next; delete tmp; while (head) { if (head->_type == VALUE) { generalizelistNode *tmp = head; head = head->_next; delete tmp; } else if (head->_type == SUB) { generalizelistNode *tmp = head; Empty(tmp->sublink); head = head->_next; } } } int _Size(generalizelistNode *&cur) { int count = 0; generalizelistNode *tmp = cur; while (tmp) { if (tmp->_type == VALUE) { count++; tmp = tmp->_next; } else if (tmp->_type == SUB) { count += _Size(tmp->sublink); tmp = tmp->_next; } else { tmp = tmp->_next; } } return count; } int _Depth(generalizelistNode *&cur) { generalizelistNode *tmp = cur; int depth = 1; while (tmp) { int sub_depth=1; if (tmp->_type == SUB) { sub_depth = _Depth(tmp->sublink); tmp = tmp->_next; if (sub_depth + 1 > depth) { depth += sub_depth; } } else { tmp = tmp->_next; } } return depth; } private: generalizelistNode *_head; }; void Test() { char *str1 = "(a,b,(c,d),e,f)"; Generalizelist T1(str1); T1.Print(); Generalizelist T2(T1); cout <<endl; T2.Print(); cout << endl; Generalizelist T3 = T2; T3.Print(); cout << endl; cout << T3.Size() << endl; cout << T3.Depth() << endl; } #include"GeneralizeList.h" int main() { Test(); return 0; }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。