数据结构-广义表(GeneralizedList)实现
2016-09-28 22:16
218 查看
广义表
广义表是非线性的数据结构,是线性表的一种推广,由N个序列组成的有序序列;
广义表在表的描述中又得到了表,即允许表中有表,简而言之,广义表的定义是递归的。
广义表的简单表示:
(1)A=();
(2)B=(a,b);
(3)C=(c,(a,b));
(4)D=(d,(c,(a,b)));
示意图:
![](http://img.blog.csdn.net/20160928215803582)
代码实现:
广义表是非线性的数据结构,是线性表的一种推广,由N个序列组成的有序序列;
广义表在表的描述中又得到了表,即允许表中有表,简而言之,广义表的定义是递归的。
广义表的简单表示:
(1)A=();
(2)B=(a,b);
(3)C=(c,(a,b));
(4)D=(d,(c,(a,b)));
示意图:
代码实现:
//GeneralizedList.h #pragma once #include <iostream> #include <assert.h> using namespace std; enum Type { HEAD_TYPE, VALUE_TYPE, SUB_TYPE, }; struct GeneralizedNode { Type _type; GeneralizedNode* _next; union { char _value; GeneralizedNode* _subLink; }; GeneralizedNode(Type type = HEAD_TYPE, char value = 0) :_type(type) ,_next(NULL) { if (_type == VALUE_TYPE) { _value = value; } else if (_type == SUB_TYPE) { _subLink = NULL; } } }; class Generalized { typedef GeneralizedNode Node; public: Generalized() :_head(new Node(HEAD_TYPE)) {} Generalized(const char* str) { _head = _CreateLized(str); } Generalized(const Generalized& g) { _head = _Copy(g._head); } //赋值运算符重载函数现代写法 Generalized& operator= (Generalized g) { swap(this->_head, g._head); return *this; } ~Generalized() { _Destory(_head); _head = NULL; } //打印广义表 void Print() { _Print(_head); cout<<endl; } //求广义表元素个数 size_t Size() { return _Size(_head); } //求广义表深度 size_t Depth() { return _Depth(_head); } protected: //对已有广义表复制,便于实现拷贝构造函数和赋值运算符重载函数 Node* _Copy(Node* head) { Node* newHead = new Node(HEAD_TYPE); assert(head->_type == HEAD_TYPE); Node* cur = head->_next; Node* newCur = newHead; while (cur) { if (cur->_type == VALUE_TYPE) { newCur->_next = new Node(VALUE_TYPE, cur->_value); newCur = newCur->_next; } else if (cur->_type == SUB_TYPE) { newCur->_next = new Node(SUB_TYPE); newCur = newCur->_next; newCur->_subLink = _Copy(cur->_subLink); } cur = cur->_next; } return newHead; } //对广义表销毁,便于完成析构函数 void _Destory(Node* head) { Node* cur = head; while (cur) { Node*del = cur; cur = cur->_next; if (del->_type == SUB_TYPE) { _Destory(del->_subLink); } delete del; } } bool _IsValue(char ch) { if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { return true; } else { return false; } } //创建广义表(递归实现) Node* _CreateLized(const char* & str) { assert(str && *str == '('); ++str; Node* head = new Node(HEAD_TYPE); Node* cur = head; while (*str) { if (_IsValue(*str)) { cur->_next = new Node(VALUE_TYPE, *str); cur = cur->_next; ++str; } else if (*str == '(') { cur->_next = new Node(SUB_TYPE); cur = cur->_next; cur->_subLink = _CreateLized(str); } else if (*str == ')') { ++str; return head; } else { ++str; } } assert(false); return head; } void _Print( Node* head) { Node* cur = head; while(cur) { if (cur->_type == HEAD_TYPE) { cout<<"("; } else if (cur->_type == VALUE_TYPE) { cout<<cur->_value; if (cur->_next) { cout<<","; } } else { _Print(cur->_subLink); if (cur->_next) { cout<<","; } } cur = cur->_next; } cout<<")"; } size_t _Size(Node* head) { size_t size = 0; Node* cur = head; while (cur) { if (cur->_type == VALUE_TYPE) { ++size; } else if (cur->_type == SUB_TYPE) { size += _Size(cur->_subLink); } cur = cur->_next; } return size; } size_t _Depth(Node* head) { int depth = 1; Node* cur = head; while (cur) { if (cur->_type == SUB_TYPE) { int subDepth = _Depth(cur->_subLink); if (subDepth+1 > depth) { depth = subDepth+1; } } cur = cur->_next; } return depth; } protected: Node* _head; }; //test.cpp #include "GeneralizedList.h" void test() { //Generalized g1("(a,b,(c,d),(e,(f),h))"); Generalized g1("(a,b,(c,d))"); cout<<"g1:"; g1.Print(); cout<<"g1 Of Size Is:"<<g1.Size()<<endl; cout<<"g1 Of Depth Is:"<<g1.Depth()<<endl; Generalized g2("(a,b,(c,d),(e,(f),h))"); cout<<"g2:"; g2.Print(); cout<<"g2 Of Size Is:"<<g2.Size()<<endl; cout<<"g2 Of Depth Is:"<<g2.Depth()<<endl; Generalized g3(g2); cout<<"g3:"; g3.Print(); g2=g1; cout<<"g2:"; g2.Print(); } int main() { test(); getchar(); return 0; }
相关文章推荐
- 数据结构 链表的lua实现 仿照C++中list 实现
- 数据结构复习:用自己的方式实现List<T>(4/1晨更新)
- 自己动手,实现一种类似List<T>的数据结构(一)
- List数据结构的实现
- C++的标准模板库STL中实现的数据结构之链表std::list的分析与使用
- General List 广义表实现
- 数据结构: Java中LinkedList的简单实现
- JAVA绝对干货——List、Set、Map并发数据结构对比实现
- 【数据结构】 List 简单实现
- Java基础课程学习总结,使用LinkedList简单模拟队列数据结构和堆栈数据结构的实现
- java 利用 LinkedList类实现 数据结构 栈.......
- 黑马程序员——用LinkedList实现队列和堆栈数据结构
- 常用数据结构之顺序结构List实现
- 广义表的递归数据结构的表示与实现--自己写数据结构
- javascript实现数据结构:广义表
- 数据结构(LinkedList的java实现)
- 数据结构 c语言实现 顺序表(Sequential List)
- 数据结构之list(用数组实现)
- 数据结构模版----单链表SimpleLinkList[不带头结点&&伪OO](C语言实现)
- 数据结构 PAT 02-线性结构2 Reversing Linked List 单链表实现