用栈实现队列(Leetcode第232题)+用队列实现栈(Leetcode第225题)
目錄
1.題目描述
2.思路
3.代碼展示
4.用隊(duì)列實(shí)現(xiàn)棧
4.1題目描述
4.2思路
4.3代碼實(shí)現(xiàn)
1.題目描述
2.思路
思路是很清晰的,棧是先進(jìn)后出,而隊(duì)列是先進(jìn)先出,所以要用棧實(shí)現(xiàn)隊(duì)列,就必須用到兩個(gè)棧,一個(gè)輸入棧,一個(gè)輸出棧
在push數(shù)據(jù)的時(shí)候,只要數(shù)據(jù)放進(jìn)輸入棧就好,「但在pop的時(shí)候,操作就復(fù)雜一些,輸出棧如果為空,就把進(jìn)棧數(shù)據(jù)全部導(dǎo)入進(jìn)來(注意是全部導(dǎo)入)」,再從出棧彈出數(shù)據(jù),如果輸出棧不為空,則直接從出棧彈出數(shù)據(jù)就可以了。
如果進(jìn)棧和出棧都為空的話,說明模擬的隊(duì)列為空了。
3.代碼展示
class MyQueue { public:stack<int> stIn;stack<int> stOut;/** Initialize your data structure here. */MyQueue() {}/** Push element x to the back of queue. */void push(int x) {stIn.push(x);}/** Removes the element from in front of queue and returns that element. */int pop() {//只有當(dāng)stOut為空的時(shí)候,再從stIn里導(dǎo)入數(shù)據(jù)(導(dǎo)入strIn全部數(shù)據(jù))if (stOut.empty()) {//從stIn導(dǎo)入數(shù)據(jù)直到stIn為空,即全部導(dǎo)入while (!stIn.empty()) {stOut.push(stIn.top());stIn.pop();}}int result = stOut.top();stOut.pop();return result;}/** Get the front element. */int peek() {int res = this->pop();//直接使用已有的pop函數(shù)stOut.push(res);//因?yàn)閜op函數(shù)彈出了元素res,所以再添加回去return res;}/** Returns whether the queue is empty. */bool empty() {return stIn.empty() && stOut.empty();} };/*** Your MyQueue object will be instantiated and called as such:* MyQueue* obj = new MyQueue();* obj->push(x);* int param_2 = obj->pop();* int param_3 = obj->peek();* bool param_4 = obj->empty();*/其實(shí)思路還是好理解的,實(shí)現(xiàn)起來也不復(fù)雜,建議以后大家寫文章粘貼代碼的時(shí)候附上運(yùn)行的截圖,這樣大家看起來方便,也有助于自己回顧。
4.用隊(duì)列實(shí)現(xiàn)棧
4.1題目描述
4.2思路
前面已經(jīng)介紹了棧和隊(duì)列兩者的特性了,用棧實(shí)現(xiàn)隊(duì)列就很簡單了,相反的,用隊(duì)列也能實(shí)現(xiàn)棧,也是需要兩個(gè)隊(duì)列,還是一個(gè)輸入隊(duì)列一個(gè)輸出隊(duì)列嗎?
當(dāng)然不是這樣的,隊(duì)列是先進(jìn)先出的規(guī)則,把一個(gè)隊(duì)列中的數(shù)據(jù)導(dǎo)入另一個(gè)隊(duì)列中,數(shù)據(jù)的順序并沒有變,并有變成先進(jìn)后出的順序。
所以用棧實(shí)現(xiàn)隊(duì)列, 和用隊(duì)列實(shí)現(xiàn)棧的思路還是不一樣的,這取決于這兩個(gè)數(shù)據(jù)結(jié)構(gòu)的性質(zhì)。
但是依然還是要用兩個(gè)隊(duì)列來模擬棧,只不過沒有輸入和輸出的關(guān)系,而是另一個(gè)隊(duì)列完全是用來備份的!
4.3代碼實(shí)現(xiàn)
class MyStack { public:queue<int> que1;queue<int> que2;//輔助隊(duì)列,用來備份的/** Initialize your data structure here. */MyStack() {}/** Push element x onto stack. */void push(int x) {que1.push(x);}/** Removes the element on top of the stack and returns that element. */int pop() {int size = que1.size();size--;while (size--) {//將que1導(dǎo)入que2,但留下最后一個(gè)元素que2.push(que1.front());que1.pop();}int result = que1.front();//留下最后一個(gè)元素就是要返回的值que1.pop();que1 = que2;//再將que2賦值給que1;while (!que2.empty()) {//清空que2que2.pop();}return result;}/** Get the top element. */int top() {return que1.back();}/** Returns whether the stack is empty. */bool empty() {return que1.empty();} };/*** Your MyStack object will be instantiated and called as such:* MyStack* obj = new MyStack();* obj->push(x);* int param_2 = obj->pop();* int param_3 = obj->top();* bool param_4 = obj->empty();*/總結(jié)
以上是生活随笔為你收集整理的用栈实现队列(Leetcode第232题)+用队列实现栈(Leetcode第225题)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 汇编指令长度计算方法
- 下一篇: flex页面中嵌入html页面