首页 > 其他 > 详细

用两个栈实现队列

时间:2018-04-07 20:50:54      阅读:190      评论:0      收藏:0      [点我收藏+]
/*
 * 用两个栈实现队列.cpp
 *
 *  Created on: 2018年4月7日
 *      Author: soyo
 */
#include<iostream>
#include<stack>
using namespace std;
int main()
{
    void MakeQueue(int a[],int m);
    int a[]={1,2,3,4,5};
    int num;
    num=sizeof(a)/sizeof(int);
    cout<<"生成的队列最后输出为:"<<endl;
   MakeQueue(a,num);
}
void MakeQueue(int a[],int m)
{
      stack<int>s1;
      stack<int>s2;
        int i,temp;
        for(i=0;i<m;i++)
            {

            s1.push(a[i]);
            }
        for(i=0;i<m;i++)
        {
              temp=s1.top();
              s2.push(temp);
              s1.pop();
        }
        for(i=0;i<m;i++)
        {
            temp=s2.top();
            cout<<temp<<" ";
            s2.pop();
        }
}

结果:

生成的队列最后输出为:
1 2 3 4 5 

 

用两个栈实现队列

原文:https://www.cnblogs.com/soyo/p/8734402.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!