[c#]用两个栈实现一个队列&用两个队列实现一个栈

代码:

static void Main(string[] args)
        {
            queueClass queue = new queueClass();
            queue.push(1);
            queue.push(2);
            queue.push(3);
            Console.WriteLine("队列输出:");
            Console.WriteLine(queue.pop());
            queue.push(4);
            int count = queue.count();
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine(queue.pop());
            }
            stackClass stack = new stackClass();
            stack.push(1);
            stack.push(2);
            stack.push(3);
            Console.WriteLine("堆栈输出:");
            Console.WriteLine(stack.pop());
            stack.push(4);
            int count2 = stack.count();
            for (int i = 0; i < count2; i++)
            {
                Console.WriteLine(stack.pop());
            }
            Console.ReadKey();
        }
        class queueClass {
            Stack<int> s1 = new Stack<int>();
            Stack<int> s2 = new Stack<int>();
            public int count() 
            {
                return s1.Count + s2.Count;
            }
            
            public void push(int node)
            {
                s1.Push(node);
            }
            public int pop()
            {
                if (s2.Count == 0)
                {
                    while (s1.Count != 0)
                    {
                        s2.Push(s1.Pop());
                    }
                }
                return s2.Pop();
            }
        }
        class stackClass {
            Queue<int> q1 = new Queue<int>();
            Queue<int> q2 = new Queue<int>();
            public int count()
            {
                return q1.Count + q2.Count;
            }
            public void push(int num)
            {
                if (q1.Count > 0)
                {
                    q1.Enqueue(num);
                }
                else
                {
                    q2.Enqueue(num);
                }
            }
            public int pop()
            {
                int ret = 0;
                if (q1.Count > 0)
                {
                    int num = q1.Count;
                    while (num > 1)
                    {
                        q2.Enqueue(q1.Dequeue());
                        --num;
                    }
                    ret = q1.Peek();
                    q1.Dequeue();
                }
                else
                {
                    int num = q2.Count;
                    while (num > 1)
                    {
                        q1.Enqueue(q2.Dequeue());
                        --num;
                    }
                    ret = q2.Peek();
                    q2.Dequeue();
                }
                return ret;
            }
        }

效果:

参考网址:https://blog.csdn.net/cherrydreamsover/article/details/80466781


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值