广义表

广义表是非线性的结构,是线性表的一种扩展,是有n个元素组成有限序列。 广义表的定义是递归的,因为在表的描述中又得到了表,允许表中有表。  

 <1> A = ()

<2> B = (a,b)

<3> C = (a,b,(c,d))

<4> D = (a,b,(c,d),(e,(f),h))

<5> E = (((),()))

 

/*节点的类型*/
enum TypeSign
{
    HEAD,
    VALUE,
    SUB
};

/*广义表的节点结构*/
struct Node
{
    Node(TypeSign type, char value = '0')
    :_typesign(type), _next(NULL)
    {
        _typesign = type;
        switch (type)
        {
        case HEAD:
            break;
        case VALUE:
            _value = value;
            break;
        case SUB:
            _generalList = NULL;
            break;
        }
    }

    Node* _next;
    TypeSign _typesign;
    union 
    {
        char _value;
        Node* _generalList;
    };
};

/*广义表*/
class GeneralList
{
public:
    GeneralList(const char* p)
    {
        _head = _GetHead(p);
    }
public:
    Node* _GetHead(const char* &p);
    Node* _ShowHead()
    {
        return _head;
    }
    Node* _CopyGeneralLink(Node* head)const;
    void _Destory(Node* head)const;
    void _Show(Node* head)const;
    size_t _GetCount(Node* head)const;
    size_t _GetDepth(Node* head)const;
private:
    Node* _head;
};

Node* GeneralList::_GetHead(const char* &p)
{
    Node* head = new Node(HEAD);
    Node* cur = head;
    ++p;
    while ( *p != ')')
    {
        if (*p >= '0' && *p <= '9')
        {
            Node* tmp = new Node(VALUE, *p++);
            cur->_next = tmp;
            cur = cur->_next;
        }

        else if (*p == '(')
        {
            Node* tmp = new Node(SUB);
            cur->_next = tmp;
            cur = cur->_next;
            tmp->_generalList = _GetHead(p);
            ++p;
        }

        else
            ++p;
    } 

    return head;
}

void GeneralList::_Show(Node* head)const
{
    if (head)
    {
        Node* cur = head;
        while (cur)
        {
            if (cur->_typesign == HEAD)
                cout << "(";
            else if (cur->_typesign == VALUE)
            {
                cout << cur->_value;
                if (cur->_next)
                    cout << ",";
            }
            else
            {
                _Show(cur->_generalList);
                if (cur->_next)
                    cout << ",";
            }
            cur = cur->_next;
        }
        cout << ")";
    }
    return;
}

size_t GeneralList::_GetCount(Node* head)const
{
    size_t count = 0;
    Node* cur = head;
    if (head)
    {
        while (cur)
        {
            if (cur->_typesign == VALUE)
                count++;
            if (cur->_typesign == SUB)
                count += _GetCount(cur->_generalList);
            cur = cur->_next;
        }
        return count;
    }
    return 0;
}

size_t GeneralList::_GetDepth(Node* head)const
{
    Node* cur = head;
    int depth = 1;
    if (head)
    {
        while (cur)
        {
            if (cur->_typesign == SUB)
            {
                size_t tmp = _GetDepth(cur->_generalList);
                depth = depth > (tmp + 1) ? depth : (tmp + 1);
            }
            cur = cur->_next;
        }
        return depth;
    }
    return 0;
}
广义表

 

posted @ 2016-07-06 07:54  _in_the_way  阅读(288)  评论(0编辑  收藏  举报