zoukankan      html  css  js  c++  java
  • 广义表的实现

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

    #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();
    }

  • 相关阅读:
    4.9版本的linux内核中实时时钟芯片pt7c4338的驱动源码在哪里
    4.9版本的linux内核中eeprom存储芯片at24c512的驱动源码在哪里
    4.9版本的linux内核中温度传感器adt7461的驱动源码在哪里
    4.9版本linux内核的ina220电流检测芯片源码在哪里
    nxp的layerscape系列芯片中的rcw指定了一些什么信息
    openwrt的编译系统是如何制作根文件系统的
    openwrt的编译系统在哪里对程序进行开机自动启动配置
    makefile中的word函数作用是什么
    jquery 动态增加table行,动态删除table行
    使用jQuery创建可删除添加行的动态表格,超级简单实用的方法
  • 原文地址:https://www.cnblogs.com/qingjiaowoxiaoxioashou/p/5918183.html
Copyright © 2011-2022 走看看