广义表的实现
来源:互联网 发布:美国大非农数据公布时间 编辑:程序博客网 时间:2024/06/12 12:38
广义表:非线性结构,是线性表的一种扩展,是有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;}
运行结果如下:
本文出自 “零点时光” 博客,请务必保留此出处http://10741764.blog.51cto.com/10731764/1772199
0 0
- 广义表的实现
- 广义表的实现
- 广义表的实现
- 广义表的实现
- 广义表的递归实现
- 广义表的递归实现
- 广义表的简单实现
- 广义表的简单实现
- 【数据结构】广义表的实现
- 广义表的递归实现
- 广义表的基本实现
- 广义表的实现(c++实现)
- 广义表(GeneralList)的c++类实现
- 广义表的C语言实现
- 广义表的C语言实现
- C++ 数据结构 *** 广义表的部分实现
- C++中广义表的实现
- 广义表的基本操作实现
- 盘吃蛇
- 线索化二叉树
- 哈希表
- 迷宫的实现
- c++统计程序运行时间的方法
- 广义表的实现
- 【虚拟现实】学习笔记
- 稀疏矩阵的转置
- irda-objs和irda-obj的差别,烧写一个字母的悲剧
- 常见的几种排序
- Vps部署ShadowScoks(ubuntu)
- linux下find命令和文件的三种时间
- android调试之top指令查看系统资源
- MySQLdb错误 编码问题 linux系统粘贴板 python取环境变量 2016.05.27回顾