广义表的实现-创新互联

广义表:非线性结构,是线性表的一种扩展,是有n个元素组成有限序列,是递归的,因为在表的描述中又得到了表,允许表中有表。

创新互联专注于企业网络营销推广、网站重做改版、阎良网站定制设计、自适应品牌网站建设、H5建站商城网站制作、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为阎良等各大城市提供网站开发制作服务。

广义表的实现

#include<cassert>      
#include<iostream>
using namespace std;

enum Type    //枚举节点的类型
{
	HEAD,  //头结点
	VALUE, //有数据成员的节点
	SUB,   //有子链的节点
};

template<class T>
struct GeneralizedNode   //定义节点
{
	Type _type;
	GeneralizedNode* _next;
	union            //运用联合体使得该数据成员只含有一种节点
	{
		T _value;
		GeneralizedNode* _sublink;
	};
	GeneralizedNode(Type type = HEAD, T value = 0)  //构造节点
		:_type(type)
		,_next(NULL)
	{
		if (_type == VALUE)
		{
			_value = value;
		}
		if (_type == SUB)
		{
			_sublink = NULL;
		}
	}
};

template<class T>
class Generalized
{
public:
	Generalized()
		:_head(NULL)
	{}
	Generalized(const char* str)    //构造函数
		:_head(NULL)
	{
		_head = _Creatlize(str);
	}


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

	}


	Generalized& operator=(const Generalized& g) //传统写法
	{
		if (this != &g)
		{
		 GeneralizedNode *temp=_Copy(g._head);
		 _Destroy(_head);
		 _head = temp;
		}
		return *this;
	}

	Generalized<T>& operator=(Generalized  g) //现代写法
	{
		swap(_head, g._head);
		return *this;
	}

	size_t Size()   //求表中的结点个数
	{
		return _size(_head);
	}

	size_t Depth()    //求深度
	{
		return _Depth(_head);
	}

	void print()    //打印节点
	{
		_print(_head);
	}

protected:
	bool ISValue(char m)
	{
		if (m >= 'a'&&m <= 'z' || m >= 'A'&&m <= 'Z' || m >= '0'&&m <= '9')
		{
			return true;
		}
		else
		{
			return false;
		}

	}
	void _print(GeneralizedNode<T>* head)  //打印节点 运用递归方式进行
	{
		assert(head);
		GeneralizedNode<T> *cur = head;
		while (cur)
		{
			if (cur->_type == HEAD)
			{
				cout << "(" << "";
				//cur = cur->_next;
			}
			else if (cur->_type == VALUE) //如果是VALUE,则打印该节点
			{
				cout << cur->_value;
				if (cur->_next == NULL)
				{
					cout << ")";
				}
				else
				{
					cout << ",";
				}
			}
			else if (cur->_type == SUB) //如果是SUB类型,则递归调用下一层
			{
				_print(cur->_sublink);
				if (cur->_next == NULL)
				{
					cout << ")";
				}
				else
				{
					cout << ",";
				}
				
			}
			else
			{
				
				cout << ")";
			}
			cur = cur->_next;
		}
		
	}

	size_t _size(GeneralizedNode<T>* p)
	{
		GeneralizedNode<T> *cur = p;
		int count = 0;
		while (cur)
		{
			if (cur->_type == VALUE) //如果是VALUE,则count++
			{
				++count;
			}
			else if (cur->_type == SUB) //如果是SUB,则调用下一层
			{
				count += _size(cur->_sublink);
			}
			cur = cur->_next;
		}
		return count;
	}

	int _Depth(GeneralizedNode<T>* head)
	{
		GeneralizedNode<T>* cur = head;
		int depth = 1;
		while (cur)
		{
			if (cur->_type == SUB)
			{
				int subdepth = _Depth(cur->_sublink);
				if (subdepth + 1 > depth)
				{
					depth = subdepth + 1;
				}
			}
			cur = cur->_next;
		}
		return depth;
	}

	GeneralizedNode<T>* _Creatlize(const char*& str)   //构造广义表
	{
		assert(*str == '(');
		while (*str)
		{
			if (*str == '(')
			{
				GeneralizedNode<T>* _head = new GeneralizedNode<T>(HEAD);
				GeneralizedNode<T>* cur = _head;
				++str;
				while (*str)
				{
					if (ISValue(*str))
					{
						GeneralizedNode<T> *temp = new GeneralizedNode<T>(VALUE);
						
						temp->_value = *str;
						cur->_next = temp;
						cur = cur->_next;
						++str;
					}
					else if (*str == '(')
					{
						GeneralizedNode<T>* sub = new GeneralizedNode<T>(SUB);
						sub->_sublink = _Creatlize(str);
						cur->_next = sub;
						cur = cur->_next;
					}
					else if (*str == ')')
					{
						++str;
						return _head;
					}
					else
					{
						++str;
					}
				}
				return _head;
			}
		}
		return _head;
	}
	GeneralizedNode<T>* _Copy(GeneralizedNode<T>* head)  //拷贝
	{
		GeneralizedNode<T>* newhead = new GeneralizedNode<T>(HEAD);
		GeneralizedNode<T>* cur = head->_next;
		GeneralizedNode<T>* newcur = newhead;
		while (cur)
		{
			if (cur->_type == VALUE)
			{
				newcur->_next = new GeneralizedNode<T>(VALUE, cur->_value);
				newcur = newcur->_next;
			}
			else if (cur->_type == SUB)
			{
				newcur->_next = new GeneralizedNode<T>(SUB);
				newcur = newcur->_next;
				newcur->_sublink = _Copy(cur->_sublink);
			}
			cur = cur->_next;
		}
		return newhead;
	}

protected:
	GeneralizedNode<T>* _head;

};

测试代码如下:

void test4()
{
	Generalized<char> a("(a,b)");
	Generalized<char> b("(a,(c,(f),d),b)");

	Generalized<char> c(a);
	Generalized<char> d(b);
	c.print();
	cout<< endl;
	d.print();
	cout << endl;
	cout << d.Depth()<<endl;
	cout << d.Size()<<endl;
}
int main()
{

	test4();
	system("pause");
	return 0;
}

运行结果如下:

广义表的实现

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。

网站栏目:广义表的实现-创新互联
新闻来源:https://www.cdcxhl.com/article26/dodicg.html

成都网站建设公司_创新互联,为您提供网站排名网页设计公司建站公司品牌网站制作微信公众号App开发

广告

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

搜索引擎优化