广义表
2015-11-20 17:26
239 查看
GeneralList.h
#pragma once #include<iostream> using namespace std; enum NodeType { Head_Type, //头结点 Value_Type, //值节点 Sub_Type, //子表节点 }; struct GeneralListNode //广义表节点 { NodeType _type; //节点类型 GeneralListNode* _next; union { char _value; //节点的值 GeneralListNode* _subLink; }; GeneralListNode(NodeType type = Head_Type, char value = '\0') :_type(type) , _next(NULL) , _value(value) , _subLink(NULL) {} }; class GeneralList { public: GeneralList(const char* str) :_link(NULL) { _CreatGeneralList(_link,str); } GeneralList( GeneralList& gl) { _link = _CopyGeneralList(gl._link); } GeneralList& operator=( GeneralList& gl) { if (&gl != this) { _Destory(_link); _link = _CopyGeneralList(gl._link); } return *this; } ~GeneralList() { _Destory(_link); } int Size() //求广义表节点数 { return _Size(_link); } int Depth() //求广义表的深度 { return _Depth(_link); } void Print() { _Print(_link); cout << endl; } protected: void _CreatGeneralList(GeneralListNode*& link, const char*& str) { if (*str != '(' || *str == '\0') { cout << "Invaild GeneralList" << endl; return; } GeneralListNode* Head = new GeneralListNode(Head_Type); link = Head; GeneralListNode* begin = Head; str++; while (*str) { //值节点 if (*str >= 0 && *str <= 9 || *str >= 'a' && *str <= 'z' || *str >= 'A' && *str <= 'Z') { GeneralListNode* tmp = new GeneralListNode(Value_Type); tmp->_value = *str; begin->_next = tmp; begin = begin->_next; str++; } else if (*str == '(') { GeneralListNode* SubNode = new GeneralListNode(Sub_Type); begin->_next = SubNode; begin = begin->_next; _CreatGeneralList(begin->_subLink, str); } else if (*str == ')') { str++; return; } else //处理 ,空格 等情况 { str++; } } } int _Size(GeneralListNode*& link) { int size = 0; GeneralListNode* cur = link; while (cur) { if (cur->_type == Value_Type) { size++; } else if (cur->_type == Sub_Type) { size += _Size(cur->_subLink); } cur = cur->_next; } return size; } int _Depth(GeneralListNode*& link) { int depth = 1; GeneralListNode* cur = link; while (cur) { if (cur->_type == Sub_Type) { int subDep = _Depth(cur->_subLink); if (subDep + 1 > depth) { depth = subDep + 1; } } cur = cur->_next; } return depth; } void _Print(GeneralListNode*& link) { GeneralListNode* cur = link; cout << "("; while (cur) { if (cur->_type == Value_Type) { cout << cur->_value; if (cur->_next != NULL) { cout << ","; } } else if (cur->_type == Sub_Type) { _Print(cur->_subLink); if (cur->_next != NULL) { cout << ","; } } cur = cur->_next; } cout <<")"; } void _Destory(GeneralListNode*& link) { GeneralListNode* cur = link; while (cur) { GeneralListNode* del = cur; cur = cur->_next; delete del; if (cur == NULL) { return; } if (cur->_type == Sub_Type) { _Destory(cur->_subLink); } } } GeneralListNode*& _CopyGeneralList(GeneralListNode*& link) { GeneralListNode* Head = new GeneralListNode(Head_Type); GeneralListNode* CopyHead = Head; GeneralListNode* begin = CopyHead; while (link) { if (link->_type == Value_Type) { GeneralListNode* tmp = new GeneralListNode(Value_Type); tmp->_value = link->_value; begin->_next = tmp; begin = begin->_next; } else if (link->_type == Sub_Type) { GeneralListNode* SubNode = new GeneralListNode(Sub_Type); begin->_next = SubNode; begin = begin->_next; begin->_subLink = _CopyGeneralList(link->_subLink); } link = link->_next; } return CopyHead; } private: GeneralListNode* _link; };
main.cpp
#include"GeneralList.h" void Test() { char* str1 = "()"; GeneralList gl1(str1); int size1 = gl1.Size(); cout << "节点个数:" << size1 << endl; char* str2 = "(a, b)"; GeneralList gl2(str2); gl2.Print(); int size2 = gl2.Size(); cout << "节点个数:" << size2 << endl; char* str3 = "(a,(c,d))"; GeneralList gl3(str3); gl3.Print(); int size3 = gl3.Size(); cout << "节点个数:" << size3 << endl; char* str4 = "(a,(c,d),(e,(f),h))"; GeneralList gl4(str4); gl4.Print(); int depth4 = gl4.Depth(); cout << "深度:" << depth4 << endl; int size4 = gl4.Size(); cout << "节点个数:" << size4 << endl; char* str5 = "(((), ()))"; GeneralList gl5(str5); gl5.Print(); } void Test1() { char* str = "(a,(c,d),(e,(f),h))"; GeneralList gl(str); GeneralList gl1("(a, (c, d))"); gl = gl1; gl1.Print(); } int main() { Test1(); getchar(); return 0; }
相关文章推荐
- dos命令执行mysql的sql文件
- 利用strlwr()任意大写字母转小写
- Docker + Jenkins + Cordova建设移动端打包平台
- maven "Generating project in Batch mode"问题的解决
- angular源码分析:angular中脏活累活的承担者之$interpolate
- SSH HTTPS 公钥、秘钥、对称加密、非对称加密、 总结理解(有助于网络层理解)
- UVA 10200 Prime Time (打表)
- C++中关于strtok()函数的用法
- SQL 6种约束
- 提供向后的导航
- 安装 Hadoop 以及报错修复
- JavaSocket通信Demo
- Spring - bean的依赖关系(depends-on属性)
- 建设小政政自杀之路
- hdu 1561 树形dp 入门教学
- 字符串复制到指定空间
- canvas基础学习(一)
- 扫描二维码自动识别手机APP下载地址
- STL笔记之【map之移除元素】
- 进程结束后,进程的所有内存都将被释放,包括堆上的内存泄露的内存。