频道栏目
首页 > 资讯 > 其他 > 正文

数据结构之广义表

16-09-27        来源:[db:作者]  
收藏   我要投稿

广义表>

广义表是非线性的结构,是线性表的一种扩展,是有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 
#include 
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<= '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<_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)
				{
					//更新maxDepth
					maxDepth=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<<>

运行结果展示>

相关TAG标签
上一篇:Github项目解析(十三)--)使用Kotlin实现UC头条ViewPager左右滑动效果
下一篇:Android 源码系列之(九)从源码的角度深入理解Activity的launchModel特性
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站