数据结构之广义表
来源:互联网 发布:sql stuff用法 编辑:程序博客网 时间:2024/05/16 12:36
广义表>
广义表是非线性的结构,是线性表的一种扩展,是有n个元素组成的有限序列.
广义表的定义是递归的,因为在表的描述中又得到了表,允许表中有表.
eg:
(1).空表套空表((()))--深度为3(2).未出现子表(a,b)--深度为1(3).表中有表(a,b,(c,d))--深度为2(4).多层子表的嵌套(a,b,(c,d),(e,(f),g))--深度3(5).空表()--深度为1
在求一个广义表的深度的时候我们要求的深度是所有子表中的最大值而不是所有的以'('开头的表的总和.例如上述的实例(4),它的深度不是4而是3.
通过观察我们发现有三种类型-头结点类型,值类型和子表类型,在广义表的结点中下一个结点可能是值也可能是子表,并且只能是其中的一个所以用共用体来封装数据项和子表项,而且该广义表的结点成员中还必须存在指向下一个结点的指针域.
enum Type{HEAD, //头类型VALUE, //值类型SUB, //子表类型};struct GeneralNode{GeneralNode(Type type):_type(type),_next(NULL){}GeneralNode(Type type,const char& value):_type(type),_next(NULL),_value(value){}Type _type; //类型GeneralNode *_next; //指向同层的下一个结点union {//下一个结点可能是值也可能是子表char _value;GeneralNode *_sublink; //指向子表的指针};};
程序源代码>
GeneralList.h
#define _CRT_SECURE_NO_WARNINGS 1#pragma once#include <iostream>#include <cassert>using namespace std;enum Type{HEAD, //头类型VALUE, //值类型SUB, //子表类型};struct GeneralNode{GeneralNode(Type type):_type(type),_next(NULL){}GeneralNode(Type type,const char& value):_type(type),_next(NULL),_value(value){}Type _type; //类型GeneralNode *_next; //指向同层的下一个结点union {//下一个结点可能是值也可能是子表char _value;GeneralNode *_sublink; //指向子表的指针};};class GeneralList{public:GeneralList():_head(NULL){}GeneralList(char *str){_head=_CreatList(str);}GeneralList(const GeneralList& g){_head=_Copy(g._head);}////传统的写法//GeneralList& operator=(const GeneralList& g)//{//if (this != &g) //防止自赋值//{//GeneralNode *head=_Copy(g._head);//_Destroy(_head);//_head=head;//}//return *this;//}//改进的现代写法GeneralList& operator=(const GeneralList& g){if (this != &g){GeneralList tmp(g);std::swap(_head,tmp._head);}return *this;}~GeneralList(){_Destroy(_head);}public:size_t Size(){size_t size=_Size(_head);return size;}size_t Depth(){size_t depth=_Depth(_head);return depth;}void Display(){_Display(_head);cout<<endl;}protected:bool IsValue(char str){if (str >= 'A' && str <= 'Z'||str >= 'a' && str <= 'z'||str >= '0' && str <= '9'){return true;}else{return false;}}GeneralNode *_CreatList(char *&str){assert(*str=='(');++str;GeneralNode *head=new GeneralNode(HEAD);GeneralNode *cur=head;while(*str){if (IsValue(*str)){//是有效数据cur->_next=new GeneralNode(VALUE,*str);cur=cur->_next;cur->_value=*str;str++;cur->_type=VALUE;}else if (*str == ')'){//子表的结束标志str++;return head;}else if (*str == '('){//表中有表cur->_next=new GeneralNode(SUB);cur=cur->_next;cur->_sublink=_CreatList(str);++str;cur->_type=SUB;}else{//逗号或者其他的分隔符str++;}}return head;}GeneralNode* _Copy(GeneralNode *head){//phead,pcur为要拷贝的对象结点//head,cur为未拷贝的对象结点GeneralNode *phead=new GeneralNode(HEAD);GeneralNode *pcur=phead;GeneralNode *cur=head;while (cur){if (cur->_type == VALUE){pcur->_next=new GeneralNode(VALUE,cur->_value);pcur=pcur->_next;pcur->_value=cur->_value;cur=cur->_next;}else if (cur->_type == SUB){pcur->_next=new GeneralNode(SUB);pcur=pcur->_next;pcur->_type=cur->_type;pcur->_sublink=_Copy(cur->_sublink);cur=cur->_next;}else{cur=cur->_next;}}return phead;}void _Destroy(GeneralNode *head){GeneralNode *cur=head;while (cur){GeneralNode *del=cur;cur=cur->_next;if (del->_type == SUB){_Destroy(del->_sublink);}else{delete del;del=NULL;}}}protected:void _Display(GeneralNode *head){GeneralNode *cur=head;while (cur){if (cur->_type == HEAD){cout<<"(";}else if (cur->_type == VALUE){cout<<cur->_value;if (cur->_next != NULL){cout<<","; }}else{_Display(cur->_sublink);if (cur->_next != NULL){cout<<",";}}cur=cur->_next;}cout<<")";}size_t _Size(GeneralNode *head){size_t count=0;GeneralNode *cur=head;while (cur){if (cur->_type == VALUE){count++;}else if (cur->_type == SUB){count += _Size(cur->_sublink);}cur=cur->_next;}return count;}size_t _Depth(GeneralNode *head){size_t maxDepth=1;GeneralNode *cur=head;while (cur){if (cur->_type == SUB){size_t depth=_Depth(cur->_sublink)+1;if (depth > maxDepth){//更新maxDepthmaxDepth=depth;}}cur=cur->_next;}return maxDepth;}protected:GeneralNode *_head;};
test.cpp
#define _CRT_SECURE_NO_WARNINGS 1#include "GeneralList.h"void testGeneralList(){GeneralList g1("((()))");GeneralList g2("(a,b)");GeneralList g3("(a,b,(c,d))");GeneralList g4("(a,b,(c,d),(e,(f),g))");g1.Display();g2.Display();g3.Display();g4.Display();cout<<g1.Size()<<endl; //0cout<<g2.Size()<<endl; //2cout<<g3.Size()<<endl; //4cout<<g4.Size()<<endl; //7cout<<g1.Depth()<<endl; //3cout<<g2.Depth()<<endl; //1cout<<g3.Depth()<<endl; //2cout<<g4.Depth()<<endl; //3GeneralList g5(g4);g5.Display();GeneralList g6;g6=g5;g6.Display();}int main(){testGeneralList();system("pause");return 0;}
运行结果展示>
0 0
- 数据结构之广义表
- 8.数据结构之广义表
- c++数据结构之广义表
- 浅析数据结构之广义表
- 数据结构之广义表模拟
- 数据结构之【数组和广义表】复习题
- 算法与数据结构之广义表
- 数据结构之数组和广义表
- 数据结构之广义表和二叉树
- 数据结构之数组和广义表
- 31. 数据结构笔记之三十一广义表
- 数据结构---->广义表
- 数据结构 广义表
- 数据结构广义表实验
- 数据结构--广义表
- 数据结构--广义表
- [数据结构复习]广义表
- 数据结构 - 广义表
- 国内 maven服务地址 修改setting
- 统计数字问题 字典序问题 最多约数问题
- Google最新VR(sdk的诞生)
- python 画图
- 勾股定理一日一证连载113
- 数据结构之广义表
- ISO 8583 Tutorial – Introduction for Beginners
- 关于jsp页面上无法使用EL表达式的问题
- 构造函数中调用虚函数能否实现多态
- HDU 1506 Largest Rectangle in a Histogram 分治法 -
- Tree107BinaryTreeLevelOrderTraversal2
- 各种排序
- [BZOJ4530][Bjoi2014]大融合(树链剖分+并查集)
- Dll嵌入到exe中