数据结构-广义表

广义表(Lists,又称列表)是一种非线性的数据结构,是线性表的一种推广。

它被广泛的应用于人工智能等领域的表处理语言LISP语言中。在LISP语言中,广义表是一种最基本的数据结构,就连LISP 语言的程序也表示为一系列的广义表。


因为表中有表,所以可以用递归进行解决广义表的问题。




#pragma once 
#include<assert.h>

enum Type//用枚举形式来定义广义表中三种节点类型
{
	HEAD, //头类型
	VALUE,//值类型
	SUB,//子表类型
};

struct GeneralizedNode
{
	Type _type;//类型
	GeneralizedNode* _next;
	union
	{
		int _value;
		GeneralizedNode* _sublink;
	};

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



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

	~Generalized()
	{
		_Destroy(_head);
	}

	Generalized(const char* str)
	{
		_head = _CreateList(str);
	}
	Generalized(const Generalized& g)
	{
		_head = _Copy(g._head);

	}

	Generalized& operator=(const Generalized& g)
	{
		if (this != &g)
		{
			GeneralizedNode* tmp = _Copy(g._head);
			_Destroy(_head);
			_head = tmp;
		}
		return *this;
	}

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

	size_t Size()
	{
		return _Size(_head);
	}
	size_t Depth()
	{
		return _Depth(_head);
	}

protected:

	void _Destroy(GeneralizedNode* head)
	{
		GeneralizedNode* cur = head;
		while (cur)
		{
			GeneralizedNode* del = cur;
			cur = cur->_next;
			if (del->_type == SUB)
			{
				_Destroy(del->_sublink);
			}
			delete del;
		}
	}

	GeneralizedNode*  _Copy(GeneralizedNode* head)
	{
		GeneralizedNode* newHead = new GeneralizedNode(HEAD);
		GeneralizedNode* cur = head->_next;

		GeneralizedNode* newcur = newHead;
		while (cur)
		{
			if (cur->_type == VALUE)
			{
				newcur->_next = new GeneralizedNode(VALUE, cur->_value);
				newcur = newcur->_next;
			}
			else if (cur->_type == SUB)
			{
				//newcur->_next = new GeneralizedNode(SUB);

				GeneralizedNode* newSublink= new GeneralizedNode(SUB);
				newcur->_next = newSublink;
				newcur = newcur->_next;
				newcur->_sublink = _Copy(cur->_sublink);
			}
			
			cur = cur->_next;
		}
		return newHead;
	}

	GeneralizedNode* _CreateList(const char*&str)
	{
		assert(*str == '(');
		++str;
		GeneralizedNode* head = new GeneralizedNode(HEAD);
		GeneralizedNode* cur = head;
		while (*str)
		{
			if (isStr(*str))
			{
				cur->_next = new GeneralizedNode(VALUE, *str);
				cur = cur->_next;
				++str;  //attention
			}
			else if (*str == '(')
			{
				GeneralizedNode* SubNode = new GeneralizedNode(SUB);
				cur->_next = SubNode;
				cur = cur->_next;
				SubNode->_sublink = _CreateList(str);
			}
			else if (*str == ')')
			{
				str++;
				return head;
			}
			else
			{
				str++;
			}
		}
		cout << "广义表出错!" << endl;
		assert(false);
		return head;
	}


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


	size_t _Size(GeneralizedNode* head)
	{
		size_t size = 0;
		if (head == NULL)
			return 0;
		GeneralizedNode* cur = head;
		while (cur)
		{
			if (cur->_type == VALUE)
			{
				++size;
			}
			else if (cur->_type == SUB)
			{
				//+=
				size+=_Size(cur->_sublink);
			}
			cur = cur->_next;
		}
		return size;
	}
	void _Print(GeneralizedNode* head)
	{
		GeneralizedNode* cur = head;
		while (cur)
		{
			if (cur->_type == HEAD)
			{
				cout << "(";
			}
			else if (cur->_type == VALUE)
			{
				cout << (char)(cur->_value);
				if (cur->_next)
				{
					cout << ",";
				}
			}
			else if (cur->_type == SUB)
			{
				_Print(cur->_sublink);
				if (cur->_next)
				{
					cout << ",";
				}
			}
			cur = cur->_next;
		}
		cout << ")";
	}

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


protected:
	GeneralizedNode* _head;

};



void Test()
{
	Generalized g1("()");
	Generalized g2("(a,b,(c,d))");
	Generalized g3("(a,b,(c,d),(e,(f),h)) ");
	Generalized g4(g2);
	g4.Print();


	Generalized g5;
	g5 = g3;
	g5.Print();


	//g1.Print();
	//cout << endl;
	//g2.Print();
	//cout << endl;

	//g3.Print();
	//cout << endl;

	//cout << "g3:size:" << g3.Size() << endl;
	//cout << "g1:depth:" << g1.Depth() << endl;
	//cout << "g2:depth:" << g2.Depth() << endl;
	//cout << "g3:depth:" << g3.Depth() << endl;
}





你可能感兴趣的:(数据结构-广义表)