广义表

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

#include<iostream>
using namespace std;

#include <assert.h>

enum Type
{
	HEAD,
	VALUE,
	SUB,
};

struct GeneralListNode
{
	Type _type;
	GeneralListNode* _next;

	union{
		char _value;
		GeneralListNode* _sublink;
	};

	GeneralListNode(Type type = VALUE, char value = 0)
		:_type(type)
		, _next(NULL)
	{
		if (_type == VALUE)
		{
			_value = value;
		}
		else if (_type == SUB)
		{
			_sublink = NULL;
		}
	}
};

class GeneralList
{
public:
	GeneralList()
		:_head(NULL)
	{}

	GeneralList(char *s)
		:_head(NULL)
	{
		_head = _CreateGeneralList(s);
	}

	GeneralList(const GeneralList& g)
	{
		_head = _Copy(g._head);
	}

	GeneralList& operator=(const GeneralList& g)
	{
		if (this != &g)
		{
			this->_Destory(_head);
			_head = _Copy(g._head);
		}
		return *this;
	}

	//GeneralList& operator=(GeneralList g)
	//{
	//	swap(_head, g._head);
	//	return *this;
	//}

	~GeneralList()
	{
		_Destory(_head);
	}

	size_t Size()
	{
		return _size(_head);
	}

	size_t Depth()
	{
		return _depth(_head);
	}

	void Print()
	{
		_Print(_head);
		cout << endl;
	}

protected:
	GeneralListNode* _CreateGeneralList(char*& s)
	{
		assert(*s == '(');
		GeneralListNode *head = new GeneralListNode(HEAD);
		++s;
		GeneralListNode *cur = head;
		while (*s)
		{
			if (*s == '(')
			{
				GeneralListNode *subnode = new GeneralListNode(SUB);
				cur->_next = subnode;
				cur = cur->_next;
				subnode->_sublink = _CreateGeneralList(s);
			}
			else if (*s == ')')
			{
				++s;
				break;
			}
			else if (IsValue(*s))
			{
				GeneralListNode* valuenode = new GeneralListNode(VALUE, *s);
				cur->_next = valuenode;
				cur = cur->_next;
				++s;
			}
			else
			{
				++s;
			}
		}
		return head;
	}

	GeneralListNode* _Copy(GeneralListNode *head)
	{
		assert(head);
		GeneralListNode *newhead = new GeneralListNode(HEAD);
		GeneralListNode *cur = head->_next;
		GeneralListNode *newcur = newhead;
		while (cur)
		{
			if (cur->_type == VALUE)
			{
				newcur->_next = new GeneralListNode(VALUE, cur->_value);
				newcur = newcur->_next;
			}
			else if (cur->_type = SUB)
			{
				newcur->_next = new GeneralListNode(SUB);
				newcur = newcur->_next;
				newcur->_sublink = _Copy(cur->_sublink);
			}
			cur = cur->_next;
		}
		return newhead;
	}
	
	void _Print(GeneralListNode* head)
	{
		GeneralListNode *cur = head;
		while (cur)
		{
			if (cur->_type == HEAD)
			{
				cout << "(";
			}
			else if (cur->_type == VALUE)
			{
				cout << cur->_value;
				if (cur->_next)
				{
					cout << ",";
				}
			}
			else
			{
				_Print(cur->_sublink);
				if (cur->_next)
				{
					cout << ",";
				}
			}
			cur = cur->_next;
		}
		cout << ")";
	}

	bool IsValue(char c)
	{
		if ((c >= '0' && c <= '9')
			|| (c >= 'a' && c <= 'z')
			|| (c >= 'A' && c <= 'Z'))
		{
			return true;
		}
		return false;
	}

	size_t _size(GeneralListNode *head)
	{
		GeneralListNode *cur = head;
		size_t size = 0;
		while (cur)
		{
			if (cur->_type == VALUE)
			{
				size++;
			}
			else if (cur->_type == SUB)
			{
				size += _size(cur->_sublink);
			}
			cur = cur->_next;
		}
		return size;
	}

	size_t _depth(GeneralListNode *head)
	{
		size_t depth = 1;
		GeneralListNode *cur = head;  
		while (cur)
		{
			if (cur->_type == SUB)
			{
				size_t subdepth = _depth(cur->_sublink);
				if (subdepth + 1 > depth)
				{
					depth = subdepth + 1;
				}
			}
			cur = cur->_next;
		}
		return depth;
	}

	void _Destory(GeneralListNode *head)
	{
		GeneralListNode *cur = head;
		while (cur)
		{
			GeneralListNode* del = cur;
			cur = cur->_next;

			if (del->_type == SUB)
			{
				_Destory(del->_sublink);
			}
			delete del;
		}
	}

private:
	GeneralListNode* _head;
};

int main()
{
	GeneralList g1("()");
	GeneralList g2("(a,b)");
	GeneralList g3("(a,b,(c,d))");
	GeneralList g4("(a,b,(c,d),(e,(f),h))");
	GeneralList g5(g4);
	g3 = g4;
	g3.Print();
	cout<<g5.Depth()<<endl;
	cout<<g5.Size()<<endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值