码迷,mamicode.com
首页 > 其他好文 > 详细

用两个队列实现一个栈

时间:2016-05-15 19:57:29      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:数据结构   栈 队列

本来画了图的,可是今天传上来的图片都看不了,只能默默敲下来了技术分享


思路:队列s1和队列s2

Push进来的元素压入非空队列

Pop操作之后的元素放在空队列

入栈:

(1)s1为空,s2为空

  把所有数据压入s1中

(2)s1为空,s2不为空

  数据压入s2

(3)s1不为空,s2为空

  数据压入s2

出栈:

(1)s2为空

  把s1中除了队头的元素全部压入s2中,然后弹出s1队头中的元素

(2)s1为空

  把s2中除了队头的元素全部压入s1中,然后弹出s1队头中的元素



#include<iostream>

#include<queue>

using namespace std;

template<typename T>

class Stack

{

public:

                Stack()

                { }

                Stack( const Stack & d)

                {

                                s1 = d.s1;

                                s2 = d.s2;

                }

                ~Stack()

                { }

                 void push(const T&x) //在栈顶增加元素

                {

                                 if (s1.size() > 0)    //如果s1不为空,则把数据插入s1中

                                {

                                                s1.push( x);

                                }

                                 else if (s2.size() > 0)   //如果s2不为空,则把数据插入s2中

                                {

                                                s2.push( x);

                                }

                                 else        //如果两个都为空,则把数据插入s1

                                {

                                                s1.push( x);

                                }

                }

                 void pop() // 移除栈顶元素

                {

                                 if (s1.size() == 0)

                                {

                                                 while (s2.size() != 1)

                                                {

                                                                s1.push(s2.front());

                                                                s2.pop();

                                                }

                                                s2.pop();

                                }

                                 else

                                {

                                                 while (s1.size() != 1)

                                                {

                                                                s2.push(s1.front());

                                                                s1.pop();

                                                }

                                                s1.pop();

                                }              

                }

                 T& top() //返回栈顶元素

                {

                                 if (s1.size() == 0)

                                {

                                                 return s2.front();

                                }

                                 else

                                {

                                                 return s1.front();

                                }

                }

                 bool empty() //堆栈为空则返回真

                {

                                 if (s1.size() == 0 && s2.size() == 0)

                                {

                                                 return true ;

                                }

                                 else

                                {

                                                 return false ;

                                }

                }

                 size_t size() //返回栈中元素数目

                {

                                 return s1.size() + s2.size();

                }

private:

                 queue<T > s1;

                 queue<T > s2;

};


void Test()

{

                 Stack<int > sta;

                sta.push(1);

                sta.push(2);

                sta.push(3);

                sta.push(4);


                sta.pop();

                sta.pop();


                sta.push(5);

                sta.push(6);


                 //int ret=sta.top();


                 int num=sta.size();

}


int main()

{

                Test();

                 return 0;

}


本文出自 “10911020” 博客,请务必保留此出处http://10921020.blog.51cto.com/10911020/1773632

用两个队列实现一个栈

标签:数据结构   栈 队列

原文地址:http://10921020.blog.51cto.com/10911020/1773632

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!