• 广义表的实现


    广义表是一种非线性表的数据结构,是线性表的一种推广。他放松了对原子的控制,容许原子有自身的结构。其实现如下:

    #include<iostream>
    using namespace std;
    #include<assert.h>

    enum Type      //原子类型有三种:头结点,子表节点和值节点
    {
        HEAD,
        SUB,
        VALUE
    };
    template<class T>
    struct GeneralListNode       //广义表节点
    {
        Type _type;           //节点类型
        GeneralListNode* _next;    //节点的下一个
        union                  //当节点的类型不是值节点就是子表节点
        {
            T _value;
            GeneralListNode* _SubLink;
        };

        GeneralListNode(Type type, char s)   //当节点值节点时的构造函数
            :_type(type)
            , _value(s)
            , _next(NULL)
        {}

        GeneralListNode(Type type)   //当节点为子表节点或头结点时的构造函数
            :_type(type)
            , _next(NULL)
            , _SubLink(NULL)
        {}
    };

    template<class T>
    class GeneralList
    {
    public:
        GeneralList()       //构造函数
            :_head(NULL)
        {}

        GeneralList(char* s)    //构造函数
        {
            _head = _CreateList(s);
        }

        ~GeneralList()   //析构函数
        {
            _Destory(_head);
        }

        GeneralList(const GeneralList<T>& t)   //拷贝构造函数
        {
            _head = _Copy(t._head);
        }

        GeneralList& operator=(GeneralList<T> t)    //赋值运算符的重载
        {
            swap(t._head, _head);
            return *this;
        }

        void Print()   //打印函数
        {
            _Print(_head);
            cout << endl;
        }

        size_t Size()   //求广义表节点的个数
        {
            return _Size(_head);
            cout << endl;
        }

        size_t Depth()   //求广义表的深度
        {
            return _Depth(_head);
        }
    protected:
        size_t _Depth(GeneralListNode<T>* head)
        {
            GeneralListNode<T>* cur = head;
            size_t Maxdepth = 1;
            while (cur)
            {
                if (cur->_type == SUB)
                {
                    size_t depth = _Depth(cur->_SubLink);
                    if (depth+1>Maxdepth)
                    {
                        Maxdepth = depth+1;
                    }
                }
                cur = cur->_next;
            }
            return Maxdepth;
        }

        GeneralListNode<T>* _Copy(GeneralListNode<T>* head)
        {
            assert(head);
            GeneralListNode<T>* Newhead = new GeneralListNode<T>(HEAD);
            GeneralListNode<T>* cur = head->_next;
            GeneralListNode<T>* newcur = Newhead;
            while (cur)
            {
                if (cur->_type == VALUE)
                {
                    newcur->_next = new GeneralListNode<T>(VALUE, cur->_value);
                    newcur = newcur->_next;
                }
                if (cur->_type == SUB)
                {
                    newcur->_next = new GeneralListNode<T>(SUB);
                    newcur = newcur->_next;
                    newcur->_SubLink = _Copy(cur->_SubLink);
                }
                cur = cur->_next;
            }
            return Newhead;
        }

        int _Size(GeneralListNode<T>* head)
        {
            GeneralListNode<T>* cur = head;
            size_t Count=0;
            while (cur)
            {
                if (cur->_type==VALUE)
                {
                    Count++;
                }
                if (cur->_type == SUB)
                {
                    Count += _Size(cur->_SubLink);
                }
                cur = cur->_next;
            }
            return Count;
        }

        void _Print(GeneralListNode<T>* head)
        {
            GeneralListNode<T>* cur = head;
            while (cur)
            {
                if (cur->_type == HEAD)
                {
                    cout << '(';
                }
                else if (cur->_type == VALUE)
                {
                    cout << cur->_value << " ";
                    if (cur->_next)
                    {
                        cout << ',';
                    }
                }
                else  //cur->_type==SUB
                {
                    _Print(cur->_SubLink);
                    if (cur->_next)
                    {
                        cout << ')';
                    }
                }
                cur = cur->_next;
            }
            cout << ')';
        }

        bool Isvalue(char ch)
        {
            return (ch >= '0'&&ch <= '9') || (ch >= 'a'&&ch <= 'z') || (ch >= 'A'&&ch <= 'Z');
        }

        GeneralListNode<T>* _CreateList(char*& s)
        {
            assert(*s == '(');
            GeneralListNode<T>* head = new GeneralListNode<T>(HEAD);
            ++s;
            GeneralListNode<T>* cur = head;
            while (*s)
            {
                if (*s == '(')
                {
                    GeneralListNode<T>* SubNode = new GeneralListNode<T>(SUB);
                    cur->_next = SubNode;
                    cur = cur->_next;

                    SubNode->_SubLink = _CreateList(s);
                }
                else if (*s == ')')
                {
                    ++s;
                    break;
                }
                else if ( Isvalue(*s))
                {
                    GeneralListNode<T>* ValueNode = new GeneralListNode<T>(VALUE, *s);
                    cur->_next = ValueNode;
                     cur = cur->_next;
                    ++s;
                }
                else
                {
                    ++s;
                }
            }
            return head;
        }

        void _Destory(GeneralListNode<T>* head)
        {
            GeneralListNode<T>* cur = head;
            while (cur)
            {
                GeneralListNode<T>* del = cur;
                cur = cur->_next;
                if (del->_type ==SUB)
                {
                    _Destory(del->_SubLink);
                }
                delete del;
            }
        }
    private:
        GeneralListNode<T>* _head;
    };

    /////////////////////////////////////////////////////////////////////

    主函数:

    #include"GeneralList.h"
    void main()
    {
            GeneralList<char> g1("()");
            GeneralList<char> g2("(a,b)");
            GeneralList<char> g3("(a,b,(c,d))");
            GeneralList<char> g4("(a,b,(c,d),(e,(f),h))");
            GeneralList<char> g5;
            g5 = g4;

            g4.Print();
            cout << g4.Size() << endl;
            cout << g4.Depth() << endl;
            g5.Print();
    }

  • 相关阅读:
    iOS开发-文件管理
    MagicalRecord
    NSPredicate的
    Objective-C文件和目录操作,IOS文件操作,NSFileManager使用文件操作
    iOS 开发者必不可少的 75 个工具
    UITableView 删除cell
    手势
    随机数
    PPT2016同时播放多个视频
    MATLAB运行时,弹出图片框影响电脑使用
  • 原文地址:https://www.cnblogs.com/qingjiaowoxiaoxioashou/p/5918183.html
Copyright © 2020-2023  润新知