码迷,mamicode.com
首页 > 其他好文 > 详细

广义表

时间:2016-04-22 10:44:06      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:广义表   节点   深度  

广义表是非线性的结构,是线性表的一种扩展,是有n个元素组成有限序列。
广义表的定义是递归的,因为在表的描述中又得到了表,允许表中有表
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
#include <assert.h>
enum Type
{
	HEAD,  //头节点
	VALUE, //值节点
	SUB,   //子表节点
};

struct GeneralizedNode
{
	Type _type;  //类型
	GeneralizedNode* _next;  //一个节点下一节点可能是值节点,也可能是子表节点
	union
	{
		char _value;    //有效值
		GeneralizedNode* _subLink;  //指向子表的指针
	};
	GeneralizedNode(Type type = HEAD, char value = ‘0‘)
		:_value(value)
		,_type(type)
		,_next(NULL)
	{
		if (_type == SUB)
		{
			_subLink = NULL;
		}
	}
};

class Generalized
{
public:
	Generalized();//无参的构造函数,建立空的广义表
	Generalized(const char* str);//建造广义表,有参数的构造函数
	void Print();//打印广义表
	size_t Amount();//获取值节点的个数
	size_t Depth();//获取广义表的深度
	Generalized(const Generalized& g);//拷贝构造
	Generalized& operator=(const Generalized& g);// 赋值运算符的重载
	~Generalized();

protected:
	void _Print(GeneralizedNode* head);
	GeneralizedNode* _CreatList(const char*& str);
	size_t _Amount(GeneralizedNode* head);
	size_t _Depth(GeneralizedNode* head);
	GeneralizedNode* _Copy(GeneralizedNode* head);
	void _Destory(GeneralizedNode* head);

	GeneralizedNode* _head;//记录广义表头指针
};

Generalized::~Generalized()
{
	this->_Destory(this->_head);
}

Generalized::Generalized(const char* str)
{
	_head = _CreatList(str);
}

void Generalized::Print()
{
	this->_Print(this->_head);
}

size_t Generalized::Amount()
{
	return this->_Amount(this->_head);
}

size_t Generalized::Depth()
{
	return this->_Depth(this->_head);
}

Generalized::Generalized(const Generalized& g)
{
	this->_head = this->_Copy(g._head);
}


GeneralizedNode* Generalized::_Copy(GeneralizedNode* head)
{
	assert(head);
	GeneralizedNode* cur = head;
	GeneralizedNode* retHead = NULL;
	GeneralizedNode* tmp = NULL;
	while (cur)
	{
		if (cur->_type == HEAD)
		{
			retHead = new GeneralizedNode(HEAD);
			tmp = retHead;
		}
		else if (cur->_type == VALUE)
		{
			tmp->_next = new GeneralizedNode(VALUE, cur->_value);
			tmp = tmp->_next;
		}
		else if (cur->_type == SUB)
		{
			tmp->_next = new GeneralizedNode(SUB);
			tmp->_next->_subLink = _Copy(cur->_subLink);
			tmp = tmp->_next;
		}
		cur = cur->_next;
	}
	return retHead;
}

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

GeneralizedNode* Generalized::_CreatList(const char*& str)//建立广义表
{
	assert(*str == ‘(‘);
	GeneralizedNode* head = new GeneralizedNode(HEAD, ‘0‘);
	GeneralizedNode* cur = head;
	str++;
	while (str != ‘\0‘)
	{
		if ((*str >= ‘0‘&&*str <= ‘9‘) || (*str >= ‘a‘&&*str <= ‘z‘) || (*str >= ‘A‘&&*str <= ‘Z‘))
		{
			cur->_next = new GeneralizedNode(VALUE, *str);;
			cur = cur->_next;
		}
		else if (*str == ‘(‘)
		{
			cur->_next = new GeneralizedNode(SUB);
			cur = cur->_next;
			cur->_subLink = _CreatList(str);
		}
		else if (*str == ‘)‘)
		{
			return head;
		}
		str++;
	}
	return head;
}

void Generalized::_Print(GeneralizedNode* head)//打印广义表
{
	if (head == NULL)
	{
		cout << "Generalized table is NULL" << endl;
		return;
	}
	GeneralizedNode* cur = head;
	while (cur)
	{
		if (cur->_type == HEAD)
		{
			cout << ‘(‘;
		}
		else if (cur->_type == VALUE)
		{
			cout << cur->_value;
			if (cur->_next)
			{
				cout << ‘,‘;
			}
		}
		else if (cur->_type == SUB)
		{
			_Print(cur->_subLink);
			if (cur->_next)
			{
				cout << ‘,‘;
			}
		}
		cur = cur->_next;
	}
	cout << ‘)‘;
}

size_t Generalized::_Amount(GeneralizedNode* head)//节点的个数
{
	GeneralizedNode* begin = head;
	size_t count = 0;
	while (begin)
	{
		if (begin->_type == VALUE)
		{
			count++;
		}
		if (begin->_type == SUB)
		{
			count += _Amount(begin->_subLink);
		}
		begin = begin->_next;
	}
	return count;
}

size_t Generalized::_Depth(GeneralizedNode* head)//广义表的深度
{
	if (head == NULL)
	{
		return 0;
	}
	size_t dp = 0;
	GeneralizedNode* cur = head;
	size_t max = 0;
	while (cur)
	{
		if (cur->_type == SUB)
		{
			dp = _Depth(cur->_subLink);
			if (max < dp)
			{
				max = dp;
			}
		}
		cur = cur->_next;
	}
	return max + 1;
}

void Generalized::_Destory(GeneralizedNode* head)//销毁广义表
{
	if (head == NULL)
	{
		return;
	}
	while (head)
	{
		GeneralizedNode* begin = head->_next;
		if (head->_type == SUB)
		{
			_Destory(head->_subLink);
		}
		delete head;
		head = begin;
	}
}

void Test()
{


	Generalized A("()");
	Generalized B("(a,b,(c,d))");
	Generalized C("(a,b,(c,(d),e))");
	Generalized D(C);
	A.Print();
	cout << endl;
	B.Print();
	cout << endl;

	C.Print();
	cout << endl;

	D.Print();
	cout << endl;

	size_t size = D.Amount();
	cout << "D大小:" << size << endl;
	int depth = D.Depth();
	cout << "D深度:" << depth << endl;
}

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


本文出自 “顺势而为” 博客,请务必保留此出处http://lk123456.blog.51cto.com/10831443/1766527

广义表

标签:广义表   节点   深度  

原文地址:http://lk123456.blog.51cto.com/10831443/1766527

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!