//广义表
//广义表是非线性结构,是线性表的一种扩展,是有N个元素组成的有限序列。
//广义表的定义是递归的,因为在表的描述中又得到了表,允许表中有表。
//<1>A = ();
//<2>B = (a, b);
//<3>C = (a, b, (c, d));
//<4>D = (a, b, (c, d),(e, (f), h))
//<5>E = (((),()))
#define _CRT_SECURE_NO_WARNINGS
#include<assert.h>
#include<iostream>
using namespace std;
enum Type
{
 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;
  }
  else if (_type==SUB)
  {
   _subLink = NULL;
  }
 }
};
class Generalized
{
public:
 Generalized()
  :_head(new GeneralizedNode(HEAD))
 {}
 Generalized(const char* str)
  :_head(NULL)
 {
  _head = _CreateList(str);
 }
 Generalized(const Generalized& g)
 {
  _head=_Copy(g._head);
 }
 Generalized& operator=(const Generalized& g)
 {
  if (_head != g._head)
  {
   GeneralizedNode* cur = _head;
   _Destory(_head);
   _head = _Copy(g._head);
   return *this;
  }
 }
 ~Generalized()
 {
  _Destory(_head);
 }
 
 void Print()
 {
  _Print(_head);
 }
 size_t Size()  //求表的大小
 {
  size_t count = _Size(_head);
  return count;
 }
 size_t Depth()  //求表的深度
 {
  size_t dep = _Depth(_head);
  return dep;
 }
protected:
 //创建表
 GeneralizedNode* _CreateList(const char*& str)
 {
  assert(str && *str=='(');  //判断当前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;
    continue;
   }
   else if (*str=='(')  //遇到子表
   {
    cur->_next = new GeneralizedNode(SUB);
    cur = cur->_next;
    cur->_subLink = _CreateList(str);  //递归创建子表
    continue;
   }
   else if (*str==')')   //一个表结束
   {
    ++str;
    return head;
    continue;
   }
   else
   {
    ++str;
    continue;
   }
   assert(false);  //强制判断是否错误,防止括号不匹配 
  }
  return head;
 }

 //判断当前值是否有效
 bool _IsValue(char ch)
 {
  if ((ch>='0'&&ch<='9')||(ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
  {
   return true;
  }
  else
  {
   return false;
  }
 }

    //打印表
 void _Print(GeneralizedNode* head)
 {
  GeneralizedNode* cur = head;
  while (cur)
  {
   if (cur->_type == HEAD)
   {
    cout << "("<<" ";
    cur = cur->_next;
    continue;
   }
   else if ((cur->_type == VALUE) && (cur->_next != NULL))
   {
    cout << cur->_value << " "<<",";
    cur = cur->_next;
    continue;
   }
   else if ((cur->_type == VALUE) && (cur->_next == NULL))//表的最后一个节点
   {
    cout << cur->_value<<" ";
    cur = cur->_next;
    continue;
   }
   else if (cur->_type==SUB)
   {
    _Print(cur->_subLink);  //递归打印表
    cur = cur->_next;
    if (cur!= NULL)
    {
     cout << ",";
    }
    continue;
   }
  }
  if (cur == NULL)
  {
   cout << ")";
   return;
  }
 }

 //拷贝表
 GeneralizedNode* _Copy(GeneralizedNode* head)
 {
  GeneralizedNode* Head = new GeneralizedNode(HEAD);
  //_head = Head;
  GeneralizedNode* cur = head->_next;
  GeneralizedNode* tmp = Head;
  while (cur)
  {
   if (cur->_type == VALUE)
   {
    tmp->_next = new GeneralizedNode(VALUE, cur->_value);
    cur = cur->_next;
    tmp = tmp->_next;
   }
   else if (cur->_type == SUB)
   {
    tmp->_next = new GeneralizedNode(SUB);
    //cur = cur->_next;
    tmp = tmp->_next;
    tmp->_subLink = _Copy(cur->_subLink);  //进入拷贝表的递归
    cur = cur->_next;
   }
  }
  return Head;
 }

 //
 size_t _Size(GeneralizedNode* head)
 {
  size_t count = 0;
  GeneralizedNode* cur = head;
  while (cur)
  {
   if (cur->_type == VALUE)
   {
    count++;
    cur = cur->_next;
    continue;
   }
   if (cur->_type == SUB)
   {
    count += _Size(cur->_subLink); //进入递归
    cur = cur->_next;
    continue;
   }
   cur = cur->_next;
  }
  return count;
 }

 //求表的深度
 size_t _Depth(GeneralizedNode* head)
 {
  assert(head);
  size_t Dep = 0;
  size_t dep = 1;
  GeneralizedNode* cur = head;
  while (cur)
  {
   if (cur->_type == SUB)
   {
    dep += _DepthSUB(cur->_subLink);
   }
   cur = cur->_next;
   if (Dep < dep)   //用Dep来保存最深的深度
   {
    Dep = dep;
    dep = 1;
   }
  }

  return Dep;
 }

 //求子表深度
 size_t _DepthSUB(GeneralizedNode* sub)
 {
  GeneralizedNode* cur = sub;
  size_t dep = 1;
  while (cur)
  {
   if (cur->_type == SUB)
   {
    dep = dep + _DepthSUB(cur->_subLink);
   }
   cur = cur->_next;
  }
  return dep;
 }

 //销毁表
 void _Destory(GeneralizedNode* head)
 {
  GeneralizedNode* cur = head;
  while (cur)
  {
   if (cur->_type == SUB)
   {
    _Destory(cur->_subLink);  //递归销毁子表
   }
   GeneralizedNode* del = cur;
   cur = cur->_next;
   delete del;
  }
  return;
 }
protected:
 GeneralizedNode* _head;
};
int main()
{
 char ch[] = "(a,b,(c,d))";
 Generalized g1(ch);
 g1.Print();
 cout << endl;
 cout<<g1.Size()<<endl;
 cout<<g1.Depth()<<endl;
 getchar();
 return 0;
}