棧的操作


 

1.獲取棧的最小值

定義棧的數據結構,要求添加一個min函數,能夠得到棧的最小元素。要求函數min、push以及pop的時間復雜度都是O(1)。

 

思考過程

對於push和pop操作來說,都很簡單,無論是數組實現棧,還是鏈表實現棧都很容易。但是唯獨min函數不好做。

首先對於棧這個數據結構來說,我們只能獲取第一個元素,也就是棧頂的元素。我們不能訪問到別的元素,所以我們不行也不可能去遍歷獲取棧的最小值。

那么我們肯定需要去保存棧的最小值,但是每當一個元素出棧之后,這個最小值需要變動,那么怎么變動呢?這又是一個問題。

還有就是如何去保存這個最小值,如果我們維護一個優先隊列,或者別的數據結構, 使得維護的結構是已經排序好的,那么當然可以,但是這對於空間和時間的開銷來說都不行。

 

既然是棧,那么肯定有棧自己的解決方式。

只要維護另一個棧,就能完成這個任務。

 

最小棧

1、定義一個棧這里我們稱為最小棧,原來的棧我們稱為數據棧。

2、最小棧和數據棧元素個數一定相同。最小棧的棧頂元素為數據棧的所有元素的最小值。

3、數據棧入棧一個元素A,最小棧需要拿這個元素與最小棧棧頂元素B比較,如果A小於B,則最小棧入棧A。否則最小棧入棧B。

4、當數據棧出棧一個元素時,最小棧也同時出棧一個元素。

 

圖形解釋其過程

image

image

 

 

代碼解釋其過程

- Hide code
#include <iostream>
#include <stack>
#include <cstdlib>
#include <cstdio>

using namespace std;

stack<int> dataStack;
stack<int> minStack;

int main() 
{
    int n;
    while(cin>>n)
    {
        if(!minStack.empty() && minStack.top() <= n)
        {
            dataStack.push(n);
            minStack.push(minStack.top());
        }
        else
        {
            dataStack.push(n);
            minStack.push(n);
        }
        
        printf("當前棧的最小元素為:%d\n",minStack.top());
    }
    return 0;
}


2.用兩個棧來實現一個隊列,完成隊列的Push和Pop操作。 隊列中的元素為int類型。

思路:push操作無所謂棧或隊列,都是往里面加入元素。而區別在於pop操作,隊列的pop操作取的是先push的元素,而棧pop的則是最后push的元素,怎樣通過兩個棧來實現隊列的pop操作,是主要問題所在。舉個例子,向stack1中逐個push進a,b,c,則stack1中元素{a,b,c},c位於棧頂,再將stack1中元素pop,並push進stack2中,則stack2元素{c,b,a},a位於棧頂,pop出的順序為a,b,c,符合先進先出的隊列。簡而言之,stack2中的元素是符合隊列pop順序的,可以直接pop,而stack1中的元素要想按照隊列的pop需先push到stack2中,即當stack2不為空時,可以直接pop,當stack2為空時,將stack1中的元素pop並push進stack2中,再pop。

說的有點亂,貼上代碼,就會更容易理解:

import java.util.Stack;

public class Solution { public static Stack<Integer> stack1 = new Stack<Integer>(); public static Stack<Integer> stack2 = new Stack<Integer>(); public static void push(int node) { stack1.push(node); } public static int pop() { if(stack2.empty()){ while(!stack1.empty()){ stack2.push(stack1.pop()); } } if(stack2.empty()){ System.out.println("queue is empty"); } return stack2.pop(); } public static void main(String[] args){ push(1); push(2); push(3); push(4); System.out.println(pop()); } }

3.用兩個隊列實現一個棧

思路:棧的push操作,當兩個隊列都為空時,不妨將元素push進queue1;或者push進非空隊列。棧的pop操作,根據隊列的先進先出原則,先插入的元素在隊首,后插入的在隊尾,而棧的操作要求后進先出,即隊尾的元素要先出,可以將非空隊列的元素逐個出隊並入隊到空隊列中,直到空隊列剩余一個元素,此時刪除這個元素即可。

貼出代碼

package com.su.biancheng; import java.util.LinkedList; import java.util.Queue; /** * @title StackWithTwoQueues.java * @author Shuai * @date 2016-4-15下午9:00:25 */ public class StackWithTwoQueues { public static Queue<Integer> queue1=new LinkedList<Integer>(); public static Queue<Integer> queue2=new LinkedList<Integer>(); public static void push(int e){ //插入非空隊列 if(queue2.isEmpty())
       //offer添加 queue1.offer(e); if(queue1.isEmpty()) queue2.offer(e); } public static int pop(){ //將非空隊列的元素逐個出隊並插入到空隊列 //poll,獲取並移除此隊列的頭,如果此隊列為空,則返回 null。 //remove,獲取並移除此隊列的頭。此方法與 poll 唯一的不同在於:此隊列為空時將拋出一個異常。 if(!queue1.isEmpty()){ while(queue1.size()>1){ queue2.offer(queue1.poll()); } return queue1.poll(); } else if(!queue2.isEmpty()){ while(queue2.size()>1){ queue1.offer(queue2.poll()); } return queue2.poll(); } return 0; } public static void main(String[] args){ push(1); push(2); push(3); push(4); push(5); System.out.println(pop()); } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM