zoukankan      html  css  js  c++  java
  • 广义表

    广义表是非线性的结构,是线性表的一种扩展,是有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;
    }
    广义表
  • 相关阅读:
    chrome浏览器解析xml
    CuteEditor报错 空引用错误
    猫哥网络编程系列:HTTP PEM 万能调试法
    猫哥网络编程系列:详解 BAT 面试题
    全新 Mac 安装指南(编程篇)(环境变量、Shell 终端、SSH 远程连接)
    全新 Mac 安装指南(通用篇)(推荐设置、软件安装、推荐软件)
    魅族手机浏览器兼容性调优最佳实践
    使用 nvm 管理不同版本的 node 与 npm
    一种让 IE6/7/8 支持 media query 响应式设计的方法
    排列组合算法的javascript实现
  • 原文地址:https://www.cnblogs.com/shihaochangeworld/p/5645627.html
Copyright © 2011-2022 走看看