91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

用兩個棧實現一個隊列

發布時間:2020-07-18 04:04:49 來源:網絡 閱讀:382 作者:淺唱逝流年 欄目:編程語言

思路:

棧s1和棧s2,s1用來Push數據,s2用來Pop數據

入隊:數據直接壓入棧s1中

出隊:若s2為空,則把s1中除過最后一個壓入的元素全部移入s2中,然后彈出s1的棧頂元素

            若s2不為空,直接彈出s2的棧頂元素

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>

#include<stack>

#include<assert.h>

using namespace std;


template<typename T>

class Queue

{

public:

                Queue()

                {

                                 //利用庫里面的stack的構造函構造s1和s2

                }

                Queue( const Queue & que)

                {

                                s1 = que.s1;

                                s2 = que.s2;

                }

                ~Queue()

                {

                                 //利用stack類來完成s1和s2的釋放

                }

                 void Push(const T&t)

                {

                                s1.push( t);

                }

                 void Pop()

                {

                                 assert(s1.size() != 0 || s2.size() != 0);

                                 if (s2.size() == 0)

                                {////把s1中除了棧底元素,其他全部push進s2中

                                                 while (s1.size() != 1)

                                                {

                                                                s2.push(s1.top());  

                                                                s1.pop();      

                                                }

                                                s1.pop();

                                }

                                 else

                                {

                                                s2.pop();

                                }

                }

                 T& Front()  //讀取隊列的隊首元素

                {

                                 assert(s1.size() != 0 || s2.size() != 0);

                                 if (s2.size() != 0)

                                {

                                                 return s2.top();

                                }

                                 else

                                {

                                                 while (s1.size() != 1)

                                                {

                                                                s2.push(s1.top());

                                                                s1.pop();

                                                }

                                                 return s1.top();

                                }              

                }

                 T& Back()  //讀取隊列的隊尾元素

                {

                                 assert(s1.size() != 0 || s2.size() != 0);

                                 if (s1.size() != 0)

                                {

                                                 return s1.top();

                                }

                                 else

                                {

                                                 while (s2.size() != 1)

                                                {

                                                                s1.push(s2.top());

                                                                s2.pop();

                                                }

                                                 return s2.top();

                                }

                } 

                 bool Empty()    //判斷空

                {

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

                                {

                                                 return true ;

                                }

                                 else

                                {

                                                 return false ;

                                }

                }

                 size_t Size()

                {

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

                }

              template<typename T> 

                 friend ostream &operator<<(ostream& _cout, const Queue <T>&que);

private:

                 stack<T > s1;

                 stack<T > s2;

                

};

template<typename T>

ostream& operator<<(ostream &_cout, const Queue <T>& que)

{

                 Queue<T >coutque(que);

                 _cout << "Queue:" ;

                 while (coutque.s2.size() > 0)

                {

                                 _cout << coutque.s2.top() << " ";

                                coutque.s2.pop();

                }

                 while (coutque.s1.size() > 0)

                {

                                coutque.s2.push(coutque.s1.top());

                                coutque.s1.pop();

                }

                 while (coutque.s2.size() > 0)

                {

                                 _cout << coutque.s2.top() << " ";

                                coutque.s2.pop();

                }

                 _cout << " " ;

                 return _cout ;

}

void Test()

{

                 Queue<int > que;

                que.Push(1);

                que.Push(2);

                que.Push(3);

                que.Push(4);

                que.Push(1);

                que.Push(2);

                que.Push(3);

                que.Push(4);

                cout << que << endl;


                 /*que.Pop();

                que.Pop();

                que.Pop();

                cout << que << endl;*/


                 /*int head1=que.Front();

                cout << head1<<endl;

                que.Pop();

                int head2 = que.Front();

                cout << head2<<endl;*/


                 /*int tail1 = que.Back();

                cout << tail1 << endl;*/


                 int ret=que.Size();

                cout << ret << endl;


}

int main()

{

                Test();

                 return 0;

}


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

阳泉市| 金湖县| 兴宁市| 沙河市| 缙云县| 康平县| 凤冈县| 岚皋县| 武汉市| 巩留县| 淮滨县| 玉林市| 宁陕县| 青河县| 扎囊县| 客服| 孝义市| 漳平市| 拉萨市| 临夏市| 沙湾县| 白玉县| 梅州市| 罗山县| 涡阳县| 鸡泽县| 务川| 大邑县| 栖霞市| 呼和浩特市| 石柱| 蓝山县| 鄂托克前旗| 牟定县| 靖江市| 平罗县| 石城县| 合山市| 九龙城区| 崇明县| 大邑县|