《剑指offer》:[7]两个队列实现栈的C++代码实现

//==============以下为自己实现====================

#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <queue>


using namespace std;


queue<int> q1;
queue<int> q2;


void inStack(int data)
{
    if(q1.empty())
    {
        q2.push(data); 
        printf("inStack %d\n", data);
    }
    else
    {
        q1.push(data); 
        printf("inStack %d\n", data);
    }



void outStack()
{
    if(q1.empty())
    {
        while(q2.size()>1)
        {
            q1.push(q2.front());
            q2.pop();
        }
        printf("outStack %d\n", q2.front());
        q2.pop();
        
        
    }
    else
    {
        while(q1.size()>1)
        {
            q2.push(q1.front());
            q1.pop();
        }
        printf("outStack %d\n", q1.front());
        q1.pop();
    }
}


bool isEmpty()
{
    if(q1.empty() && q2.empty())
    {
        return true;
    }
    else
    {
        return false;
    }
}




int main(int argc, char *argv[])
{
    int array[]={1,2,3};
for(int i=0;i<3;i++)
{
inStack(array[i]); //全部入栈:1,2,3;
}
for(int i=0;i<3;i++)
{
if(isEmpty()) //判断是否为NULL;
return 0;
outStack();        //全部出栈:3,2,1;
}


  system("PAUSE");
  return 0;
}

//==============以下为转载实现======================

https://blog.csdn.net/gogokongyin/article/details/51505555


问题:两个队列实现栈。

    因为队列的特点是先进先出,而栈式先进后出。所以具体的实现步骤如下:

(1)判断是否为NULL;如果queue1和queue2都为NULL,则该栈为NULL;
(2)如果queue1不为NULL,而queue2为NULL;则queue1出队,进队到queue2,如果queue1的元素个数只剩一个的时候就出队输出,也就是出栈;反之亦然;

(3)若果那个队列不为NULL,则入队那个队列,也即入栈;

示意图如下:


扫描二维码关注公众号,回复: 1852111 查看本文章
          总之,原则就是:因为我们操作以后一定是有一个queue为NULL。所以,入栈,则是找不为NULL的队列直接入队;出栈,则是将不为NULL的queue里的数据出队到另一个queue中,直至出队队列中剩下最后一个元素后便直接输出出栈。
利用容器具体代码实现如下:

  1. #include<iostream>
  2. #include <algorithm>
  3. #include <queue>
  4. using namespace std;
  5. queue< int> q1;
  6. queue< int> q2;
  7. void InStack(int data)
  8. {
  9. if(q1.empty()) //刚开始默认从q2入队;
  10. {
  11. q2.push(data);
  12. cout<< "InStack:"<<data<< endl;
  13. }
  14. else
  15. {
  16. q1.push(data);
  17. cout<< "InStack:"<<data<< endl;
  18. }
  19. }
  20. void OutStack()
  21. {
  22. if(q1.empty())
  23. {
  24. while(q2.size()> 1)
  25. {
  26. q1.push(q2.front());
  27. q2.pop();
  28. }
  29. cout<< "OutStack:"<<q2.front()<< endl;
  30. q2.pop();
  31. }
  32. else
  33. {
  34. while(q1.size()> 1)
  35. {
  36. q2.push(q1.front());
  37. q1.pop();
  38. }
  39. cout<< "OutStack:"<<q1.front()<< endl;
  40. q1.pop();
  41. }
  42. }
  43. bool IsEmpty()
  44. {
  45. if(q1.empty() && q2.empty())
  46. return 1;
  47. else
  48. return 0;
  49. }
  50. int main()
  51. {
  52. int array[]={ 1, 2, 3};
  53. for( int i= 0;i< 3;i++)
  54. {
  55. InStack( array[i]); //全部入栈:1,2,3;
  56. }
  57. for( int i= 0;i< 3;i++)
  58. {
  59. if(IsEmpty()) //判断是否为NULL;
  60. return NULL;
  61. OutStack(); //全部出栈:3,2,1;
  62. }
  63. system( "pause");
  64. return 0;
  65. }
运行结果:




猜你喜欢

转载自blog.csdn.net/qq_20398345/article/details/80882409
今日推荐