c++实现广义表-创新互联

非线性的结构,是线性表的一种扩展,是有n个元素组成有限序列。

为乌鲁木齐等地区用户提供了全套网页设计制作服务,及乌鲁木齐网站建设行业解决方案。主营业务为成都网站制作、成都网站建设、乌鲁木齐网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

广义表的定义是递归的,因为在表的描述中又得到了表,允许表中有表。

<4> D = (a,b,(c,d),(e,(f),h))

<5> E = (((),()))

如下图所示:

c++实现广义表

广义表主要使用递归实现,表与表之间使用链式结构来存储。下面就来看看代码怎样实现的:

#pragma once

#include <iostream>
#include <assert.h>
using namespace std;

//节点类型
enum Type
{
	HEAD,//头节点
	VALUE,//数据项
	SUB,//子表的头节点
};

//广义表结构
struct GeneralizedNode
{
public:
	GeneralizedNode()//无参构造函数
		:_type(HEAD)
		, _next(NULL)
	{}

	GeneralizedNode(Type type, char ch = 0)
		:_type(type)
		,_next(NULL)
	{
		if (_type == VALUE)
		{
			_value = ch;//数据节点
		}
		if (_type == SUB)
		{
			_sublink = NULL;//子表节点
		}
	}

public:
	Type _type;//节点类型
	GeneralizedNode * _next;
	union
	{
		char _value;//数据
		GeneralizedNode * _sublink;//子表的头指针
	};
};

//广义表类
class Generalized
{
public:
	Generalized()//无参构造函数
		:_head(new GeneralizedNode(HEAD))
	{}
	Generalized(const char* str)//构造函数
		:_head(NULL)
	{
		_head = _CreateList(str);
	}

	Generalized(const Generalized & g)//拷贝构造
	{
		_head = _CopyList(g._head);
	}

	Generalized& operator=(Generalized g)//赋值运算符的重载
	{
		swap(_head, g._head);//现代写法
		return *this;
	}

	~Generalized()//析构函数
	{
		_Delete(_head);//递归析构
	}

public:
	void print()//打印
	{
		_Print(_head);
	}

	size_t size()//元素个数
	{
		size_t ret=_Size(_head);
		return ret;
	}

	size_t depth()//广义表的深度
	{
		size_t ret=_Depth(_head);
		return ret;
	}

public:
	GeneralizedNode * _CreateList(const char* &str)//创建广义表
	{
		assert(str&&*str == '(');
		//判断传入的广义表是否正确
		str++;//跳过'('

		GeneralizedNode* head = new GeneralizedNode();//创建头节点
		GeneralizedNode* cur = head;
		while (*str)
		{
			if (_IsVaild(*str))
			{
				cur->_next = new GeneralizedNode(VALUE, *str);//创建数据节点
				cur = cur->_next;//节点后移
				str++;//指针后移
			}
			else if (*str == '(')//子表
			{
				GeneralizedNode* SubNode = new GeneralizedNode(SUB);//创建字表的头节点
				SubNode->_sublink = _CreateList(str);//递归调用_CreateList函数
				cur->_next = SubNode;
				cur = cur->_next;
			}
			else if (*str == ')')//表的结束
			{
				str++;//跳过')'
				return head;//返回头节点
			}
			else//其他字符(' '或者',')
			{
				str++;
			}
		}
		assert(false);//强制判断程序是否出错
		return head;
	}

	bool _IsVaild(const char ch)//判断输入是否合理
	{
		if ((ch >= '0'&&ch <= '9')
			|| (ch >= 'a'&&ch <= 'z')
			|| (ch >= 'A'&&ch <= 'Z'))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	GeneralizedNode* _CopyList(GeneralizedNode* head)//复制
	{
		GeneralizedNode* cur = head;
		//创建新的头节点
		GeneralizedNode* newHead = new GeneralizedNode();
		GeneralizedNode* tmp = newHead;
		while (cur)
		{
			if (cur->_type == VALUE)//数据节点
			{
				tmp->_next = new GeneralizedNode(VALUE, cur->_value);
				tmp = tmp->_next;
			}
			else if (cur->_type == SUB)//子表节点
			{
				GeneralizedNode* SubNode = new GeneralizedNode(SUB);
				tmp->_next = SubNode;
				SubNode->_sublink = _CopyList(cur->_sublink);//递归调用
				
				tmp = tmp->_next;
			}
			cur = cur->_next;
		}
		return newHead;
	}

	void _Delete(GeneralizedNode * head)
	{
		GeneralizedNode* cur = head;
		while (cur)
		{
			GeneralizedNode* del = cur;
			if (cur->_type == SUB)//子表节点时递归析构
			{
				_Delete(cur->_sublink);
			}
			cur = cur->_next;
			delete del;
		}
	}

	void _Print(GeneralizedNode* head)//打印
	{
		GeneralizedNode* cur = head;
		if (cur == NULL)
		{
			return;
		}
		while (cur)
		{
			if (cur->_type == HEAD)
			{
				cout << "(";
			}
			else if (cur->_type == VALUE&&cur->_next)
			{
				cout << cur->_value<<",";
			}
			else if (cur->_type == VALUE&&cur->_next == NULL)
			{
				cout << cur->_value;
			}
			else if (cur->_type == SUB)
			{
				_Print(cur->_sublink);
				if (cur->_next)
				{
					cout << ",";
				}
			}
			cur = cur->_next;
		}
		if (cur == NULL)
		{
			cout << ")";
			return;
		}
	}

	size_t _Size(GeneralizedNode* head)//元素的个数
	{
		GeneralizedNode* cur = head;
		size_t count = 0;
		while (cur)
		{
			if (cur->_type == VALUE)
			{
				count++;
			}
			else if (cur->_type == SUB)
			{
				count += _Size(cur->_sublink);
			}
			cur = cur->_next;
		}
		return count;
	}

	size_t _Depth(GeneralizedNode* head)//广义表的深度
	{
		GeneralizedNode* cur = head;
		size_t depth = 1;
		while (cur)
		{
			if (cur->_type == VALUE)
			{
				depth++;
			}
			else if (cur->_type == SUB)
			{
				size_t newdepth = _Depth(cur->_sublink);
				if (newdepth++ > depth)//当后面子表的深度比现在的深时,更新深度
				{
					depth = newdepth++;
				}
			}
			cur = cur->_next;
		}
		return depth;
	}

private:
	GeneralizedNode * _head;
};

测试代码和结果如下:

void Test()
{
	Generalized g1("(a,b(c,d),(e,(f),h))");
	Generalized g2;
	g1.print();
	cout << endl;
	cout << "g1.size()="<< g1.size() << endl << endl;
	cout << "g1.depth()=" << g1.depth() << endl << endl;

	g2 = g1;
	g2.print();
	cout << endl;
	cout << "g2.size()=" << g1.size() << endl << endl;
	cout << "g2.depth()=" << g1.depth() << endl << endl;
}

c++实现广义表

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

分享题目:c++实现广义表-创新互联
文章分享:https://www.cdcxhl.com/article32/ppdsc.html

成都网站建设公司_创新互联,为您提供网站收录定制网站商城网站ChatGPT静态网站域名注册

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都定制网站网页设计