GeneralList.hpp            

#pragma once

#include<iostream>
#include<string>
using namespace std;

enum NodeType{               //结点类型
	HEAD_TYPE,
	VALUE_TYPE,
	SUB_TYPE,
};

struct GeneralListNode{            //结点的结构体
	NodeType _type;
	GeneralListNode* _next;

	union{
		char _value;
		GeneralListNode* _subLink;
	};
	GeneralListNode(NodeType type = HEAD_TYPE, char value = '\0')
		:_type(type)
		, _next(NULL){
		if (type == VALUE_TYPE){
			_value = value;
		}
		else if (type == SUB_TYPE)
		{
			_subLink = NULL;
		}
	}
};

class GeneralList{
public:
	GeneralList(const char* str) :_link(NULL){      //构造函数
		_CreateGeneralList(_link, str);
	}
	GeneralList(const GeneralList& gl){             //拷贝构造函数
			_link = _Copy(gl._link);
	}
	GeneralList& operator=(const GeneralList& gl){   //重载函数
		if (this != &gl){
		        _Delete(_link);
			 _Copy(gl._link);
		}
		else
		    return *this;
	}  
	~GeneralList(){                                 //析构函数
		 _Delete(_link);
	}
	
	void Print()                                   //显示函数
	{
		_Print(_link);
		cout << endl;
	}

	int Size(){                        //计算广义表的大小,最外层的元素个数
		GeneralListNode* begin = _link;
		int size = 0;

		if (begin->_type == HEAD_TYPE)
			return 1;
		else{
			while (begin){
				size++;
				begin = begin->_next;
			}
		}
		return size;
	}

	int Depth(){					//计算广义表的深度
		return _Depth(_link);
	}
protected:
	const char* _CreateGeneralList(GeneralListNode*& link, const char*str){  //构造函数的实现
		GeneralListNode* begin = NULL;
		char* compareStr = "()";     //定义检查是否为()的字符串

		while (isspace(*str))          //检查是否有空格
			str++;
		if (*str != '('){            //检查输入是否合法
			cout << "input illeagle!" << endl;
			exit(-1);
		}

		if (strcmp(str, compareStr) == 0){                    //判断是否为head_type
			GeneralListNode* head = new GeneralListNode(HEAD_TYPE);   //创建结点
			link = head;
			begin = head;
		}

		str++;          //从第一个左括号后开始
		//判断类型,为其创建结点
		while (*str != '\0'){
			if (*str == ',')
				str++;
			while (isspace(*str))          //检查是否有空格
				str++;
			if (*str == '(')    {         //如果是sub_type
				GeneralListNode* subNode = new GeneralListNode(SUB_TYPE);
				if (begin == NULL){
					begin = subNode;
					link = begin;
					 str = _CreateGeneralList(begin->_subLink, str);   //递归建立子表
				}
				else{
					begin->_next = subNode;
					begin = begin->_next;
					str = _CreateGeneralList(begin->_subLink, str);
				}
			}
			else if (*str == ')' && *++str != '\0'){               //递归结束条件
				return str;
			}
			else if (*str != ')' && *str != '\0'){         //如果是value_type
				GeneralListNode* valueNode = new GeneralListNode(VALUE_TYPE, *str);
				if (begin == NULL){
					begin = valueNode;
					link = begin;
				}
				else{
					begin->_next = valueNode;
					begin = begin->_next;
				}
			}
			str++;
		}
	}

	GeneralListNode* _Copy(GeneralListNode* link){               //拷贝构造的实现
		GeneralListNode* _new = NULL;

		if (link){
			_new = new GeneralListNode(link->_type);             //创建头结点
			_new->_next = link->_next;
			switch (_new->_type)
			{
			case HEAD_TYPE:                                 
				_new->_value = link->_value;
				break;
			case VALUE_TYPE:
				_new->_value = link->_value;
				break;
			case SUB_TYPE:
				_new->_subLink = link->_subLink;
				break;
			default:
				break;
			}
			_new->_next = _Copy(link->_next);                  //递归创建其余结点
		}
		return _new;
	}

	GeneralListNode*  _Delete(GeneralListNode* link){     //析构函数的实现从右到左,从下到上
		GeneralListNode* begin = link;

		if (begin){
			switch (begin->_type){
			case HEAD_TYPE:
				break;
			case VALUE_TYPE:
				_Delete(begin->_next);
				break;
			case SUB_TYPE:
				_Delete(begin->_next);
				_Delete(begin->_subLink);
				break;
			default:
				break;
			}
			delete begin;
		}
		return begin;
	}

	void _Print(GeneralListNode* link){                   //显示函数的实现
		GeneralListNode* begin = link;
		cout << "(";
		while (begin){
			if (begin->_type == HEAD_TYPE){
				cout << ")";
			}
			else if (begin->_type == VALUE_TYPE){
				cout << begin->_value;
				if (begin->_next){
					cout << ",";
				}
				else{
					cout << ")";
				}
			}
			else if (begin->_type == SUB_TYPE){
				_Print(begin->_subLink);
				if (begin->_next){
					cout << ",";
				}
			}
			begin = begin->_next;
		}
	}

	int _Depth(GeneralListNode* link){             //深度的计算,找到子表个数最多
		GeneralListNode* begin = link;
		int depth = 0;
		int max = 0;

		while (begin){
		if (begin->_type == HEAD_TYPE)
			return 1;
		else if (begin->_type == VALUE_TYPE){
			begin = begin->_next;
		}
		else if (begin->_type == SUB_TYPE){
			depth = _Depth(begin->_subLink);
			if (depth > max)
				max = depth;
			begin = begin->_next;
		}
		}
		return max+1;
	}
private:
	GeneralListNode* _link;
};