数据结构课设--栈(顺序栈,链式栈)

接口是按照课本上的ADT定义编写的,其余部分自己编写,泛型编程,这样的话可以有更加广泛的用途。

程序可以编译用过。

    ///链表
    class singleLinkedList<T>
    {
        public T data;
        public singleLinkedList<T> next;
        public singleLinkedList()
        {
            data = default(T);
            next = null;
        }
    }

    ///栈与队列
    ///栈
    interface iStack<T>
    {

        void InitStack();

        void DestoryStack();

        void ClearStack();

        bool StackEmpty();

        int StackLength();

        void GetTop(ref T value);

        void Push(T value);

        void Pop(ref T value);

        bool StackTraverse(visit<T> tra);
    }

    ///顺序栈
    class SqStack<T> : iStack<T>
    {
        static int initSize = 50;
        static int stackInr = 50;
        T[] data;
        int curSize;
        int top;
        int limit;
        public void InitStack()
        {
            curSize = 0;
            limit = initSize;
            data = new T[initSize];
        }

        public void DestoryStack()
        {
            data = null;
            curSize = 0;
            top = 0;
        }

        public void ClearStack()
        {
            curSize = 0;
            top = 0;
        }

        public bool StackEmpty()
        {
            if (curSize == 0)
            {
                return true;
            }
            return false;
        }

        public int StackLength()
        {
            return curSize;
        }

        public void GetTop(ref T value)
        {
            value = data[top];
            --top;
        }

        public void Push(T value)
        {
            if (top < limit - 5)
            {
                data[top + 1] = value;
                ++top;
                curSize++;
            }
            else
            {
                limit += stackInr;
                data[top + 1] = value;
                ++top;
                curSize++;
            }

        }

        public void Pop(ref T value)
        {
            if (top > 0)
            {
                value = data[top];
                data[top] = default(T);
                --top;
            }
            else
            {
                value = default(T);
            }
        }

        public bool StackTraverse(visit<T> tra)
        {
            for (int i = 0; i < top; i++)
            {
                if (tra(data[i]))
                {
                    return false;
                }
            }
            return true;
        }
    }
    ///顺序栈
    ///链式栈
    class LinkedStack<T> : iStack<T>
    {
        singleLinkedList<T> head;
        int curSize;
        singleLinkedList<T> top;

        public void InitStack()
        {
            head = new singleLinkedList<T>();
            top = head;
            curSize = 0;
        }

        public void DestoryStack()
        {
            head = null;
        }

        public void ClearStack()
        {
            head = null;
        }

        public bool StackEmpty()
        {
            if (0 == curSize)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int StackLength()
        {
            return curSize;
        }

        public void GetTop(ref T value)
        {
            singleLinkedList<T> p = head;
            if (p.Equals(top))
            {
                value = top.data;
            }
            else
            {
                while (p.next != null)
                {
                    if (p.next.Equals(top))
                    {
                        value = p.next.data;
                    }
                    else
                    {
                        p = p.next;
                    }
                }

            }
        }

        public void Push(T value)
        {
            singleLinkedList<T> tmp = new singleLinkedList<T>();
            top.next = tmp;
            curSize++;
        }

        public void Pop(ref T value)
        {
            singleLinkedList<T> p = head;
            if (p.Equals(top))
            {
                value = top.data;
            }
            else
            {
                while (p.next != null)
                {
                    if (p.next.Equals(top))
                    {
                        value = p.next.data;
                        --curSize;
                        top = p;
                    }
                    else
                    {
                        p = p.next;
                    }
                }
            }
        }

        public bool StackTraverse(visit<T> tra)
        {
            singleLinkedList<T> p = head;
            while (p != null && p.next != null)
            {
                if (tra(p.data))
                {
                    return false;
                }
                p = p.next;
            }
            return true;
        }
    }
    ///链式栈

    ///栈


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值