数据结构课程设计--广义表(链式实现)

广义表,依旧是泛型编程。可以编译通过。

    ///广义表
    interface iGList<T>
    {

        void InitGList();

        // void CreateGList(T[] S);

        void DestroyGList();

        //void CopyGList(iGList<T> Src);

        int GListLength();

        //int GListDepth();

        bool GListEmpty();

        T GetHead();

        T GetTail();

        void InsertFirst(T elem);

        T DeleteFirst();

        bool Traverse(visit<T> Visit);
    }
    ///广义表
    ///广义表的实现
    enum GListType
    {
        ATOM,
        LIST
    }
    class GLNode<T>
    {
        public GListType type;
        public GList<T> list;
        public T data;
        public GLNode<T> next;
    }
    class GList<T> : iGList<T>
    {
        GLNode<T> head;
        int length;
        public void InitGList()
        {
            length = 0;
            head = new GLNode<T>();
        }


        public void DestroyGList()
        {
            head = null;
        }

        public GLNode<T> getData()
        {
            return head;
        }
        public void CopyGList(GList<T> Src)
        {
            head = Src.getData();
        }

        public int GListLength()
        {
            return length;
        }

        public int GListDepth(GLNode<T> forward)
        {
            if (head != null)
            {
                if (head.type == GListType.LIST)
                {
                    if (head != null)
                    {
                        GListDepth(head);
                    }
                }
                else
                {
                    if (head != null)
                    {
                        GListDepth(head.next);
                    }
                }
                return 1;
            }
            else
            {
                return 0;
            }
        }

        public bool GListEmpty()
        {
            if (null == head)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public T GetHead()
        {
            return head.data;
        }

        public T GetTail()
        {
            GLNode<T> pH = head;
            while (pH != null)
            {
                if (pH.next.next == null)
                {
                    return pH.data;
                }
                pH = pH.next;
            }
            return default(T);
        }

        public void InsertFirst(T elem)
        {
            GLNode<T> pHead = new GLNode<T>();
            pHead.type = GListType.ATOM;
            pHead.data = elem;
            pHead.list = null;
            pHead.next = head;
            head = pHead;
            ++length;
        }
        public void InsertFirst(GList<T> elem)
        {
            GLNode<T> pHead = new GLNode<T>();
            pHead.type = GListType.LIST;
            pHead.data = default(T);
            pHead.list = elem;
            pHead.next = head;
            head = pHead;
            ++length;
        }


        public T DeleteFirst()
        {
            T res = head.data;

            head = head.next;
            --length;
            return res;
        }

        public bool Traverse(visit<T> Visit)
        {
            GLNode<T> p = head;
            while (p != null)
            {
                if (!Visit(p.data))
                {
                    return false;
                }
                p = p.next;
            }
            return true;
        }
    }
    ///广义表的实现

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值