zoukankan      html  css  js  c++  java
  • 两个由栈组成的队列和两个由队列组成的栈

    今天看《算法导论》看到两个如题的两个习题,遂做之。没有做参数检查以及异常处理,线程安全等。

    class DoubleStackQueue
        {
            private System.Collections.Stack stackA, stackB;

            public DoubleStackQueue()
            {
                stackA = new System.Collections.Stack();
                stackB = new System.Collections.Stack();
            }

            public void Enqueue(object obj)
            {
                stackA.Push(obj);
            }

            public object Dequeue()
            {
                while (stackA.Count != 0)
                    stackB.Push(stackA.Pop());

                return stackB.Pop();
                }
        }
     
    class DoubleQueueStack
        {
            private System.Collections.Queue queueA, queueB;

            public DoubleQueueStack()
            {
                queueA = new System.Collections.Queue();
                queueB = new System.Collections.Queue();
            }

            public void Push(object obj)
            {
                while (queueA.Count != 0)
                    queueB.Enqueue(queueA.Dequeue());
                queueA.Enqueue(obj);
                while (queueB.Count != 0)
                    queueA.Enqueue(queueB.Dequeue()); 
            }

            public object Pop()
            {
                return queueA.Dequeue();
            }
        }

    改进的:DoubleQueueStack

    class DoubleQueueStack
        {
            private System.Collections.Queue queueA, queueB;

            public DoubleQueueStack()
            {
                queueA = new System.Collections.Queue();
                queueB = new System.Collections.Queue();
            }

            public void Push(object obj)
            {
                if (queueA.Count == 0 && queueB.Count == 0)
                    queueA.Enqueue(obj);
                else
                {
                    if (queueA.Count == 0) queueB.Enqueue(obj);
                    else queueA.Enqueue(obj);
                }
            }

            public object Pop()
            {
                if (queueA.Count == 0)
                {
                    while (queueB.Count != 1)
                        queueA.Enqueue(queueB.Dequeue());
                    return queueB.Dequeue();
                }
                else if (queueB.Count == 0)
                {
                    while (queueA.Count != 1)
                        queueB.Enqueue(queueA.Dequeue());
                    return queueA.Dequeue();
                }
                return null;
            }
        }

  • 相关阅读:
    基于javascript引擎封装实现算术表达式计算工具类
    Windows 服务入门指南
    参数化查询为什么能够防止SQL注入
    Control 的DraggerHelper, 拖动控件从此变得很简单。。。
    书籍推荐记这几年看的书
    使用“using” 的 “Cursor”
    多线程,silverlight_Rest_WCF,dynamic 索引帖
    关于静态事件 static event 的二三事
    探讨 .NET 4 新增的 SortedSet 类
    我们需要莱特希尔报告
  • 原文地址:https://www.cnblogs.com/diggingdeeply/p/DoubleStackQueue_and_DoubleQueueStack.html
Copyright © 2011-2022 走看看