C++ GeneralizedTable 广义表的实现

结点

////Generalized_node.h
#include<iostream>
#include<assert.h>
enum Nodetype{
	HEAD,
	VALUE,
	SUB,
};
struct Generalized_node {
	Nodetype _type;
	Generalized_node *_next;

	union {
		char _value;
		Generalized_node *_sub;
	};
	Generalized_node(Nodetype type, const char value=' ') :_type(type),_next(NULL){
		if (type==VALUE||type==HEAD) {
			_value = value;
		}
		else if (_type == SUB) {
			_sub = NULL;
		}
		else 
			assert(false);
	}
};

广义表


//Generalized_list.h
#include"Generalized_node.h"
/*
enum Nodetype{
HEAD,
VALUE,
SUB,
};
*/
class Generalized_list {
private:
	typedef Generalized_node Node;
	Node* _head;

	Node* createlist(const char *&str);
	//判断传入数据中字符是否有效
	inline bool useful(const char s) {
		if ((s >= '0'&&s <= '9')
			|| (s >= 'a'&&s <= 'z')
			|| (s >= 'A'&&s <= 'Z')) {
			return true;
		}
		return false;
	}
	void _print(Node * node);
	void _destory(Node *_head);
	size_t _depth(Node* _head);
public:
	Generalized_list():_head(NULL) {};
	Generalized_list(const char* str) {
		_head = createlist(str);
	};
	~Generalized_list()
	{
		_destory(_head);
	}
	//递归打印广义表
	void print() {
		_print(_head);
	}
	size_t depth()
	{
		size_t dep=_depth(_head);
		return dep;
	}

};
//构造函数
Generalized_node* Generalized_list::createlist(const char *&str) {
	assert(*str == '(');
	Node* head = new Node(HEAD, *str);
	Node* prev = head;
	head->_type = HEAD;
	++str;
	while (*str)
	{
		if (useful(*str))
		{
			Node* node = new Node(VALUE, *str);
			prev->_next = node;
			prev = prev->_next;
			++str;
		}
		else if (*str == '(') {
			Node *node = new Node(SUB, *str);
			prev->_next = node;
			prev = prev->_next;
			node->_sub = createlist(str);
			++str;
		}
		else if (*str == ')') {
			prev->_next = NULL;
			str++;
			return head;
		}
		else
		{
			++str;
		}
	}
	return head;
}
//打印广义表
void Generalized_list::_print(Node * node) {
	assert(node);
	Node *cur = node;
	while (cur) {
		if (cur->_type == VALUE)
		{
			std::cout << cur->_value;
			if (cur->_next != NULL)
				std::cout << ',';
			cur = cur->_next;
		}
		else if (cur->_type == SUB)
		{
			_print(cur->_sub);
			if (cur->_next != NULL)
				std::cout << ',';
			cur = cur->_next;
		}
		else
		{
			std::cout << '(';
			cur = cur->_next;
		}
	}
	std::cout << ')';
}
//销毁广义表
void Generalized_list::_destory(Node *_head)
{
	Node* cur = _head;
	while (cur)
	{
		Node* del = cur;
		if (cur->_type == SUB)
		{
			_destory(cur->_sub);
		}
		cur = cur->_next;
		delete[] del;
	}
}
//广义表深度
size_t Generalized_list::_depth(Node* _head)
{
	Node* cur = _head;
	size_t maxdep = 1;
	while (cur)
	{
		size_t dep = 1;
		if (cur->_type == SUB)
		{
			dep+=_depth(cur->_sub);
			if (dep > maxdep)
				maxdep = dep;
		}
		cur = cur->_next;
	}
	return maxdep;
}

测试运行结果

//test.cpp
#include"Generalized_list.h"
using namespace std;
int main() {
	const char *test = "(a,b,(c,d),(e,(f),h))";
	Generalized_list gl1(test);
	gl1.print();
	cout << "\n该表深度为" << gl1.depth();
	cout << endl;
	system("pause");
	return 0;
}

在这里插入图片描述

代码:
https://github.com/ChristmasError/Data_Structure/tree/master/广义表 Generalized table

猜你喜欢

转载自blog.csdn.net/qq_43265890/article/details/84536311