【代码】C++实现广义表及其测试用例
来源:互联网 发布:2345好压mac版 编辑:程序博客网 时间:2024/05/29 07:11
广义表是我第一次用递归接触链式的数据结构,其结构如下:
HEAD->VAL->VAL->LINK(->HEAD.....)->VAL->......
在这里,我们的头结点与link节点是不存储数据的,由此我们便可以定义出节点的数据结构:
typedef int DataType;enum NodeType//枚举类型定义节点类型{HEAD,VALUE,SUB,};struct GeneralizedNode{public:GeneralizedNode():_next(NULL), _link(NULL){}NodeType _type;GeneralizedNode* _next;union//因为link与value是不可能同时存在的,故用共同体节省空间{GeneralizedNode* _link;DataType _value;};};
因为广义表是链式的嵌套结构,所以我们必须用递归来进行遍历,这里面递归的方式有很多种,可以循环套递归,也可以递归套递归,也可以递归套循环,根据我们不同的需求,可以吧这三种方法都运用在合适的地方,这里,我简单的实现了,返回对象的层数,数据个数,以及一些常用的成员函数,其代码如下:
class Generalized{public:Generalized():_head(NULL){}Generalized(const char *str)//构造函数是用字符串来体现广义表如(1,2,(2,3)){ //这样可以更好的体现出广义表的结构_head = _CreatList(str);}~Generalized(){_Destory(_head);}void Print()//控制台打印广义表,也是打印出字符串类型{_Print(_head);}Generalized(const Generalized &g){_head = _Copy(g._head);}Generalized&operator = (Generalized g){swap(_head, g._head);return *this;}size_t Depth(){return _Depth(_head);}size_t Size(){size_t size = 0;_Size(_head, size);return size;}protected:size_t _Depth(GeneralizedNode* head){size_t depth = 1;GeneralizedNode* cur = head;while (cur){if (cur->_type == SUB){size_t newdepth = _Depth(cur->_link) + 1;depth = depth < newdepth ? newdepth : depth;}cur = cur->_next;}return depth;}void _Size(GeneralizedNode* head, size_t &size){GeneralizedNode* cur = head;while (cur){if (cur->_type == VALUE)++size;else if (cur->_type == SUB)_Size(cur->_link, size);cur = cur->_next;}}GeneralizedNode* _Copy(GeneralizedNode *head){GeneralizedNode* cur = head;GeneralizedNode* newHead = new GeneralizedNode;GeneralizedNode*tail = newHead;tail->_type = HEAD;while (cur){if (cur->_type == SUB){tail->_next = new GeneralizedNode;tail = tail->_next;tail->_type = SUB;tail->_link = _Copy(cur->_link);}else if (cur->_type == VALUE){tail->_next = new GeneralizedNode;tail = tail->_next;tail->_type = VALUE;tail->_value = cur->_value;}cur = cur->_next;}return newHead;}void _Print(GeneralizedNode* head){GeneralizedNode* cur = head;while (cur){if (cur->_type == HEAD){cout << "(";}else if (cur->_type == VALUE){cout << cur->_value;if (cur->_next!=NULL&&cur->_next->_type == VALUE)cout << ",";}else_Print(cur->_link);cur = cur->_next;}cout << ")";}GeneralizedNode* _CreatList(const char* &str){assert('(' == *str);GeneralizedNode* head = new GeneralizedNode;GeneralizedNode* cur = head;head->_type = HEAD;while (*++str){if (IsValue(str)){cur->_next = new GeneralizedNode;cur = cur->_next;cur->_type = VALUE;cur->_value = *str;str++;}else if (')' == *str){return head;}else if ('('){cur->_next = new GeneralizedNode;cur = cur->_next;cur->_type = SUB;cur->_link = _CreatList(str);}cur->_next = NULL;*str++;}return head;}bool IsValue(const char *str)//判断表内存储数据是否为所需数据类型{if (*str <= '9'&&*str >= '0')return true;return false;}void _Destory(GeneralizedNode*head){GeneralizedNode*cur = head;if (cur->_value == SUB)_Destory(cur->_link);else if (cur->_next != NULL)_Destory(cur->_next);delete cur;}protected:GeneralizedNode* _head;};
如有什么不足或疑问希望提出。
本文出自 “pawnsir的IT之路” 博客,请务必保留此出处http://10743407.blog.51cto.com/10733407/1765577
0 0
- 【代码】C++实现广义表及其测试用例
- 广义表及其Java代码实现
- 广义表及其实现
- c/c++实现广义表及其深度查找
- C++实现广义表及其遍历
- 广义表的C语言实现
- 广义表的C语言实现
- 广义表_2(C风格实现)
- 广义表C/C++实现详解
- 广义表的类定义及其操作的实现
- 快速排序算法实现及其测试代码
- 数据结构伪C代码:5.数组和广义表
- 【C++】广义表
- 广义表的实现
- 广义表的实现
- C++实现广义表
- 实现一个广义表
- 广义表的实现
- 【代码】模板动态线性表&类型萃取
- 【干货】share智能指针的模拟实现
- 【一张大大的图片】类和对象知识点总结
- 【智能指针的延伸】两种定制删除器的实现方法
- 【干货】容器适配器实现两个栈模拟队列
- 【代码】C++实现广义表及其测试用例
- 【代码】C++实现二叉树基本操作及测试用例
- 二叉树的线索化算法思想详解
- 【代码】c++堆的简单实现
- 堆排序算法思路详解
- 【代码】key-value模式下的哈希二次探测与简单的哈希类的实现
- 【代码】稀疏矩阵的压缩存储与转置算法
- LINUX下的几个常见的环境变量
- 【干货】C++哈希桶(开链法解决哈希冲突)类的实现