初试->广义表

广义表:又称列表)是一种非线性的数据结构,是线性表的一种推广。即广义表中放松对表元素的原子限制,容许它们具有其自身结构。它被广泛的应用于人工智能等领域的表处理语言LISP语言中。在LISP语言中,广义表是一种最基本的数据结构,就连LISP 语言的程序也表示为一系列的广义表。

     广义表是n (n>=0)个元素a1,a2,a3,…,an的有限序列,其中ai或者是原子项,或者是一个广义表。通常记作LS=a1,a2,a3,…,an)LS是广义表的名字,n为它的长度。若ai是广义表,则称它为LS的子表。

普通数据类型的广义表的定义如下:

节点:

#define Type int  //类型重定义
enum
{
	HEAD,   //这是头节点
	VALUE,   //这是数据节点
	SUB      //这是子表地址节点
};

struct GeneralizedNode    //广义表的节点
{
	Type _type;   // 存节点的类型
	GeneralizedNode *_next;  //下一节点地址
	union
	{
		char _value;   //存数据
		GeneralizedNode *_subLink;
	};

	GeneralizedNode(Type type = HEAD, char value = 0)   //节点的构造函数
		:_type(type),
		_next(NULL)
	{
		if (_type == VALUE)
		{
			_value = value;
		}
		if (_type == SUB)
		{
			//_value = _subLink;
			_subLink = NULL;
		}
	}
};

广义表:

class Generalized
{
public:
	Generalized()  //无参构造函数
		:_head(NULL)
	{}

	Generalized(const char* str)  //有参构造函数
		:_head(NULL)
	{
		_head = _Generalist(str);
	}

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



	Generalized & operator=(Generalized g)   //赋值运算符重载
	{
		swap(*this, g);
		return *this;
	}

	bool _IsValue(char ch)   //判断是否为正常数据
	{
		if ((ch >= '0'&&ch <= '9')
			|| (ch >= 'a'&&ch <= 'z')
			|| (ch >= 'A'&&ch <= 'Z'))
		{
			return true;
		}
		else
			return false;
	}

	void size()  //求长度
	{
		cout << _size(_head);
	}

	void Deps()  //求深度
	{
		cout << _Deps(_head);
	}

	void Desplay()  //输出节点
	{
		_printGeneralist(_head);
		cout << endl;
	}


	~Generalized()  //析构函数u
	{
		_Destory(_head);
	}

protected:
	GeneralizedNode *_Generalist(const char*&str) //构造函数内部实现函数
	{
		assert(*str == '(');
		++str;
		GeneralizedNode* head = new GeneralizedNode(HEAD);
		GeneralizedNode* cur = head;
		while (*str)
		{
			if (_IsValue(*str))
			{
				cur->_next = new GeneralizedNode(VALUE, *str);
				cur = cur->_next;
				++str;
			}
			else if (*str == '(')
			{
				GeneralizedNode* subNode = new GeneralizedNode(SUB);
				cur->_next = subNode;
				cur = cur->_next;

				subNode->_subLink = _Generalist(str);
			}
			else if (*str == ')')
			{
				++str;
				return head;
			}
			else
			{
				++str;
			}
		}
		cout << "广义表字符串错误" << endl;
		assert(false);
		return head;
	}

	void _printGeneralist(GeneralizedNode* head)  //输出函数,内部实现函数
	{
		GeneralizedNode* cur = head;
		while (cur)
		{
			if (cur->_type == HEAD)
			{
				cout << "(";
			}
			else if (cur->_type == VALUE)
			{
				cout << cur->_value;
				if (cur->_next != NULL)
					cout << ", ";
			}
			else
			{
				printGeneralist(cur->_subLink);
				if (cur->_next != NULL)
					cout << ", ";
			}
			cur = cur->_next;
		}
		cout << ")";
	}

	size_t _size(GeneralizedNode* g)  //求长度内部实现函数
	{
		int n = 0;
		while (g)
		{
			if (g->_type == HEAD)
			{
				g = g->_next;
			}
			else if (g->_type == VALUE)
			{
				g = g->_next;
				n++;
			}
			else
			{
				n += size(g->_subLink);
				g = g->_next;
			}
		}
		return n;
	}

	size_t _Deps(GeneralizedNode *head)   //求深度内部实现函数
	{
		size_t deps = 1;
		GeneralizedNode *cur = head;
		while (cur)
		{
			if (cur->_type == SUB)
			{
				size_t subDeps = Deps(cur->_subLink);
				if (subDeps + 1 > deps)
				{
					deps = subDeps + 1;
				}
			}
			cur = cur->_next;
		}
		return deps;
	}

	GeneralizedNode *_Copy(GeneralizedNode* head)  //拷贝构造内部实现函数
	{
		GeneralizedNode *newHead = new GeneralizedNode(HEAD);
		GeneralizedNode *newCur = newHead;
		GeneralizedNode *cur = head->_next;
		while (cur)
		{
			if (cur->_type == VALUE)
			{
				newCur->_next = new GeneralizedNode(VALUE, cur->_value);
			}
			else if (cur->_type == SUB)
			{
				newCur->_next = new GeneralizedNode(SUB);
				newCur->_next->_type = SUB;
				newCur->_next->_subLink = _Copy(cur->_subLink);
			}
			cur = cur->_next;
			newCur = newCur->_next;
		}
		return newHead;
	}

	void _Destory(GeneralizedNode* head)  //析构函数内部实现函数
	{
		GeneralizedNode* cur = head;
		while (cur)
		{
			GeneralizedNode* temp = cur;
			if (cur->_type == HEAD)
			{
				cur = cur->_next;
			}
			else if (cur->_type == VALUE)
			{
				cur = cur->_next;
			}
			else
			{
				_Destory(cur->_subLink);
				cur = cur->_next;
			}
			delete[]temp;
		}
	}

protected:

	GeneralizedNode* _head;  //节点
};

#endif;

上述函数均保存于 Generalized.h中

测试用例:

#include<iostream>
using namespace std;
#include<assert.h>
#include"Generalist.h"
void test1()
{
	Generalized g1("()");
	Generalized g2("(a,b)");
	Generalized g3("(a,b,(c,d))");	
	Generalized g4("(a,b,(c,d),(e,(f),h))");

	cout <<endl<< " g1: ";
	g1.Desplay();
	g1.Deps();
	g1.size();
	cout << endl << " g2: ";
	g2.Desplay();
	g2.Deps();
	g2.size();
	cout << endl << " g3: ";
	g3.Desplay();
	g3.Deps();
	g3.size();
	Generalized g5(g4);
	cout << endl << "g5:";
	g4.Desplay();
	g5.Desplay();
	cout << endl;
	g5.Deps();
	g5.size();
	cout << endl;

}

int main()
{
	test1();
	system("pause");
	return 0;
}

测试结果:

wKiom1cWPMvDnpTiAAAfaYcZEdU477.png


欢迎各位大神批评指正!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值