Two stacks construct one queue | two queues construct one stack

Source: Internet
Author: User

This article is mainly used to understand the difference and connection between queues and stacks. The difference is that queues are FIFO while stacks) is an advanced post-output (FILO); similarly, both are linear storage structures. I. two stacks construct a Queue: Queue with two stacks (Stack 1: inbox, Stack 2: outbox) into the column operation (EnQueue) inbox pressure Stack. If the DeQueue determines that the inbox is empty, the InvalidOperationException is thrown. Otherwise, all inbox data is pushed out of the stack and into the outbox; the outbox is output once, and the data is saved to the Temporary Variable temp; press all the remaining data in outbox into inbox and return temp. For the above ideas, the C # code is attached as follows:

using System;  using System.Collections.Generic;  using System.Linq;  using System.Text;    namespace TestApp.QueueAndStack  {      /// <summary>      /// Construct a queue with two stacks: FIFO      /// </summary>      /// <typeparam name="T"></typeparam>      public class Queue2Ss<T>      {          private Stack<T> inbox = null;          private Stack<T> outbox = null;            /// <summary>          /// Initialize the two stacks          /// </summary>          public Queue2Ss()          {              inbox = new Stack<T>();              outbox = new Stack<T>();          }            /// <summary>          /// EnQueue          /// </summary>          /// <param name="t"></param>          public void EnQueue(T t)          {              inbox.Push(t);          }            /// <summary>          /// DeQueue          /// </summary>          /// <returns></returns>          public T DeQueue()          {              if (inbox.Count == 0)              {                  throw new InvalidOperationException("The queue is empty.");              }                // Pop the values of inbox to outbox              while (inbox.Count > 0)              {                  outbox.Push(inbox.Pop());              }                // Get the result              T t = outbox.Pop();                // Push to inbox with the values of outbox               while (outbox.Count>0)              {                  inbox.Push(outbox.Pop());              }                return t;          }            /// <summary>          /// Clear the whole queue's values          /// </summary>          public void Clear()          {              inbox.Clear();              outbox.Clear();          }            /// <summary>          /// Return the count of queue          /// </summary>          /// <returns></returns>          public int Count()          {              return (inbox.Count + outbox.Count);          }      }  }  

 

2. two queues construct a Stack: Stack with two queues (Queue 1: inbox, Queue 2: outbox) Push (Push) inbox into the column. An InvalidOperationException is thrown when an inbox is null in the Pop operation. Otherwise, the inbox data is output until the last element is left, and the data is written into the outbox; inbox is listed once, and the data is saved to the Temporary Variable temp; Swap (inbox, outbox); Return temp. The C # code is attached as follows:
using System;  using System.Collections.Generic;  using System.Linq;  using System.Text;    namespace TestApp.QueueAndStack  {      /// <summary>      /// Construct a stack with two queues: FILO      /// </summary>      public class Stack2Qs<T>      {          private Queue<T> inbox = null;          private Queue<T> outbox = null;            /// <summary>          /// Initialize the two queues          /// </summary>          public Stack2Qs()          {              inbox = new Queue<T>();              outbox = new Queue<T>();          }            /// <summary>          /// Push          /// </summary>          /// <param name="t"></param>          public void Push(T t)          {              inbox.Enqueue(t);          }            /// <summary>          /// Pop          /// </summary>          /// <returns></returns>          public T Pop()          {              if (inbox.Count == 0)              {                  throw new InvalidOperationException("The stack is empty.");              }                // Dequeue the inbox's values to outbox               // until the inbox has only 1 element left              while (inbox.Count > 1)              {                  outbox.Enqueue(inbox.Dequeue());              }                // Get the result              T t = inbox.Dequeue();                // Exchange the inbox and the outbox              Queue<T> temp = inbox;              inbox = outbox;              outbox = temp;                return t;          }            /// <summary>          /// Clear the whole stack's values          /// </summary>          public void Clear()          {              inbox.Clear();              outbox.Clear();          }            /// <summary>          /// Return the count of stack          /// </summary>          /// <returns></returns>          public int Count()          {              return (inbox.Count + outbox.Count);          }      }  }  

 

The above is based on the understanding of Stack and Queue, respectively, Stack and Queue implementation, of course, we can also use the Linear Linked List in C # To implement Stack and Queue, respectively, if you are interested, try so easy! Mom no longer needs to worry that I will not stack and queue !~ _~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.