数据结构-广义表
2016-04-19 17:47
393 查看
广义表(Lists,又称列表)是一种非线性的数据结构,是线性表的一种推广。
它被广泛的应用于人工智能等领域的表处理语言LISP语言中。在LISP语言中,广义表是一种最基本的数据结构,就连LISP
语言的程序也表示为一系列的广义表。
因为表中有表,所以可以用递归进行解决广义表的问题。
它被广泛的应用于人工智能等领域的表处理语言LISP语言中。在LISP语言中,广义表是一种最基本的数据结构,就连LISP
语言的程序也表示为一系列的广义表。
因为表中有表,所以可以用递归进行解决广义表的问题。
#pragma once #include<assert.h> enum Type//用枚举形式来定义广义表中三种节点类型 { HEAD, //头类型 VALUE,//值类型 SUB,//子表类型 }; struct GeneralizedNode { Type _type;//类型 GeneralizedNode* _next; union { int _value; GeneralizedNode* _sublink; }; GeneralizedNode(Type type=HEAD,char value=0) :_type(type) , _next(NULL) { if (_type == VALUE) _value = value; else if (_type == SUB) _sublink = NULL; } }; class Generalized { public: Generalized() :_head(NULL) {} ~Generalized() { _Destroy(_head); } Generalized(const char* str) { _head = _CreateList(str); } Generalized(const Generalized& g) { _head = _Copy(g._head); } Generalized& operator=(const Generalized& g) { if (this != &g) { GeneralizedNode* tmp = _Copy(g._head); _Destroy(_head); _head = tmp; } return *this; } void Print() { _Print(_head); cout << endl; } size_t Size() { return _Size(_head); } size_t Depth() { return _Depth(_head); } protected: void _Destroy(GeneralizedNode* head) { GeneralizedNode* cur = head; while (cur) { GeneralizedNode* del = cur; cur = cur->_next; if (del->_type == SUB) { _Destroy(del->_sublink); } delete del; } } 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); GeneralizedNode* newSublink= new GeneralizedNode(SUB); newcur->_next = newSublink; newcur = newcur->_next; newcur->_sublink = _Copy(cur->_sublink); } cur = cur->_next; } return newHead; } GeneralizedNode* _CreateList(const char*&str) { assert(*str == '('); ++str; GeneralizedNode* head = new GeneralizedNode(HEAD); GeneralizedNode* cur = head; while (*str) { if (isStr(*str)) { cur->_next = new GeneralizedNode(VALUE, *str); cur = cur->_next; ++str; //attention } else if (*str == '(') { GeneralizedNode* SubNode = new GeneralizedNode(SUB); cur->_next = SubNode; cur = cur->_next; SubNode->_sublink = _CreateList(str); } else if (*str == ')') { str++; return head; } else { str++; } } cout << "广义表出错!" << endl; assert(false); return head; } size_t _Depth(GeneralizedNode* head) { size_t depth = 1; GeneralizedNode* cur = head; while (cur) { if (cur->_type == SUB) { size_t subdepth= _Depth(cur->_sublink)+1; if (subdepth > depth) { depth = subdepth; } } cur = cur->_next; } return depth; } size_t _Size(GeneralizedNode* head) { size_t size = 0; if (head == NULL) return 0; GeneralizedNode* cur = head; while (cur) { if (cur->_type == VALUE) { ++size; } else if (cur->_type == SUB) { //+= size+=_Size(cur->_sublink); } cur = cur->_next; } return size; } void _Print(GeneralizedNode* head) { GeneralizedNode* cur = head; while (cur) { if (cur->_type == HEAD) { cout << "("; } else if (cur->_type == VALUE) { cout << (char)(cur->_value); if (cur->_next) { cout << ","; } } else if (cur->_type == SUB) { _Print(cur->_sublink); if (cur->_next) { cout << ","; } } cur = cur->_next; } cout << ")"; } bool isStr( char str) { if ((str >= 'a'&&str <= 'z') || (str >= 0&&str <= 9)|| (str >= 'A'&&str <= 'Z')) { return true; } else { return false; } } protected: GeneralizedNode* _head; }; void Test() { Generalized g1("()"); Generalized g2("(a,b,(c,d))"); Generalized g3("(a,b,(c,d),(e,(f),h)) "); Generalized g4(g2); g4.Print(); Generalized g5; g5 = g3; g5.Print(); //g1.Print(); //cout << endl; //g2.Print(); //cout << endl; //g3.Print(); //cout << endl; //cout << "g3:size:" << g3.Size() << endl; //cout << "g1:depth:" << g1.Depth() << endl; //cout << "g2:depth:" << g2.Depth() << endl; //cout << "g3:depth:" << g3.Depth() << endl; }
相关文章推荐
- 数据结构,堆的学习
- 浙江大学MooC数据结构OJ——线性结构
- 冒泡排序的另一种写法
- java 常见数据结构(Array)
- 数据结构和算法设计专题之---二分查找(Java版)
- 数据结构和算法设计专题之---八大内部排序
- 算法Sedgewick第四版-第1章基础-009一链表与数组的比较及其他数据结构
- 【数据结构与算法】线段树篇二
- JAVA数据结构-LinkedList,ArrayList,Tree,
- JAVA数据结构-LinkedList,ArrayList,Tree,
- HDU 5371 Manacher
- 数据结构--双向循环链表C实现
- 数据结构--迭代归并
- Java实现:数据结构之排序
- 数据结构-位运算的使用
- 数据结构之二叉树总篇(Java)
- HDU 3613 Manacher应用
- 数据结构绪论
- 数据结构之队列
- 数据结构算法——顺序表的应用