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

typedef enum{ ATOM, LIST } ElemTag;      //ATOM == 0:原子,LIST == 1:子表

typedef const char* AtomType;                   //定义AtomType为常量字符指针型

typedef struct GLNode{
    ElemTag tag;                         //公共部分,用于区分原子结点和表结点
    union{                                 //原子结点和表结点的公共部分
        AtomType atom;                     //atom是原子结点的值域,AtomType由用户自己定义
        struct{                        
            GLNode *hp,*tp;
            }ptr;                         //ptr是表结点的指针域,ptr.hp和ptr.tp分别指向表头和表尾
    };
}*GList;

string getHead( string s )                                //s为广义表脱去了最外层的括号后的一个                                
{                                                        //字符串
    int n = s.length();
    int i = 0;
    int k = 0;
    while( i < n && ( s[i] != ',' || k != 0 ) )
    {
        if( s[i] == '(' )                                //计算表头的括号
            ++k;
        if( s[i] == ')' )
            --k;
        i++;
    }
    if( i < n )                                            //i为表头后的第一个","的位置
        return s.substr( 0, i );                        //如果表头存在括号,取表头
    else
        return s;                                        //i>=n说明表头后没有",",直接返回即为表头
}                                                        //字符串

string getTail( string s )                                //s为广义表脱去了最外层的括号后的一个                            
{                                                        //字符串
    int n = s.length();
    int i = 0;
    int k = 0;
    while( i < n && ( s[i] != ',' || k != 0 ) )
    {
        if( s[i] == '(' )                                
            ++k;
        if( s[i] == ')' )
            --k;
        i++;
    }
    if( i < n )
        return "(" + s.substr( i + 1, n - i - 1 ) + ")"; //返回表尾的字符串
    else
        return "()";
}

void creatList( GList &L, string s )
{
    string emp = "()";
    if( !s.compare( emp ))                                //和"()"比较如果相等返回一个不为零的数
        L = NULL;
    else
    {
        L = new GLNode;
        if( !L )
        {
            cout<<"Allocation Failture!";
            exit( 1 );                                    //如果分配内存失败,则立即终止程序
        }
        if( s.length() == 1 )
        {
            L->tag = ATOM;
            L->atom = s.c_str();
            cout<<L->atom;
        }
        else if( s.length() >= 2 && s[ 0 ] != '(' && s[ s.length() - 1 ] != ')' )
        {
            L->tag = ATOM;
            L->atom = s.c_str();
            cout<<L->atom;
        }
        else
        {
            L->tag = LIST;
            string sub = s.substr( 1, s.length() - 2 ); //脱去最外层的两个一对括号
            creatList( L->ptr.hp, getHead( sub ) );        //递归构造链表的表头
            creatList( L->ptr.tp, getTail( sub ) );        //递归构造链表的表尾
        }
    }
}

void main()
{
    string s = "(ab)";
    GList L;
          creatList( L, s );
    cout<<(L->ptr.hp)->atom;
}


构建玩广义表的链表后,为什么L->atom回是空的??
请各位高手指教