快速的到棧、隊列的最大值

特色ios

棧——先進後出數組

隊列——後進先出spa

思路指針

1. 快速獲得最大值的棧code

結構blog

  1. 須要兩個數組:一個數組stackItem保存棧的元素,另外一個數組link2NextMaxValueIndex保存下一個最大值的位置
  2. 兩個指針:一個爲stackTop指向棧頂,另外一個爲maxValueIndex指向最大值的下標

操做隊列

  1. 插入時:比較插入元素與最大值的大小,若是比最大值還大呢,link2NextMaxValueIndex指向原來最大值的位置(即maxValueIndex),而maxValueIndex變爲如今插入元素的位置;不然link2NextMaxValueIndex指向-1
  2. 刪除時:刪除元素的位置出,若是maxValueIndex與當前位置相同,此時maxValueIndex爲link2NextMaxValueIndex[satckTop]
  3. 返回棧的最大元素

圖示it

以入棧2 7 1,出棧爲例:io

 

代碼class

複製代碼
#include <iostream>
#include <climits>
#define MAX 100
using namespace std;

class stack
{
    public:
        stack() : stackTop(-1), maxValueIndex(-1) {}    
        void push(int val);
        int pop();
        int max();
        int size() { return stackTop + 1; }
        int empty() { return stackTop < 0 ? 1 : 0; }
    private:
        int stackItem[MAX];
        int link2NextMaxValueIndex[MAX];
        int stackTop;
        int maxValueIndex;
};

void stack::push(int val)
{
    ++stackTop;
    if(stackTop == MAX)
    {
        cout << "The stack has been full!" << endl;
        return;
    }
    else
    {
        stackItem[stackTop] = val;
        if(max() < val)
        {
            link2NextMaxValueIndex[stackTop] = maxValueIndex;
            maxValueIndex = stackTop;
        }
        else
            link2NextMaxValueIndex[stackTop] = -1;
    }
}
int stack::pop()
{
    int ret; 
    if(stackTop == -1)
    {
        cout << "The stack is empty!" << endl;
        return -1;
    }
    else
    {
        ret = stackItem[stackTop];
        if(stackTop == maxValueIndex)
            maxValueIndex = link2NextMaxValueIndex[stackTop];
        --stackTop;
        return ret;
    }
}
int stack::max()
{
    if(maxValueIndex >= 0)
        return stackItem[maxValueIndex];
    else
        return INT_MIN;
}


int main()
{
    stack s;
    cout << "s.empty():" << s.empty() << endl;
    s.push(3);
    s.push(4);
    cout << "s.top():" << s.pop() << endl;
    cout << "s.top():" << s.pop() << endl;
    cout << "s.top():" << s.pop() << endl;
    cout << "s.size():" << s.size() << endl;
    cout << "s.empty():" << s.empty() << endl;
}
複製代碼

結果

 

2. 快速獲得最大值的隊列

兩個棧能夠實現隊列(參考),就用剛纔的棧實現隊列

代碼

複製代碼
#include <iostream>
#include <climits>
#define MAX 100
using namespace std;

class stack
{
    public:
        stack() : stackTop(-1), maxValueIndex(-1) {}    
        void push(int val);
        int pop();
        int max();
        int size() { return stackTop + 1; }
        int empty() { return stackTop < 0 ? 1 : 0; }
    private:
        int stackItem[MAX];
        int link2NextMaxValueIndex[MAX];
        int stackTop;
        int maxValueIndex;
};
class queue
{
    public:
        void enQueue(int val);
        int deQueue();
        int size() { return stackIn.size() + stackOut.size(); }
    private:
        stack stackIn;
        stack stackOut;
};

void stack::push(int val)
{
    ++stackTop;
    if(stackTop == MAX)
    {
        cout << "The stack has been full!" << endl;
        return;
    }
    else
    {
        stackItem[stackTop] = val;
        if(max() < val)
        {
            link2NextMaxValueIndex[stackTop] = maxValueIndex;
            maxValueIndex = stackTop;
        }
        else
            link2NextMaxValueIndex[stackTop] = -1;
    }
}
int stack::pop()
{
    int ret; 
    if(stackTop == -1)
    {
        cout << "The stack is empty!" << endl;
        return -1;
    }
    else
    {
        ret = stackItem[stackTop];
        if(stackTop == maxValueIndex)
            maxValueIndex = link2NextMaxValueIndex[stackTop];
        --stackTop;
        return ret;
    }
}
int stack::max()
{
    if(maxValueIndex >= 0)
        return stackItem[maxValueIndex];
    else
        return -100;
}

void queue::enQueue(int val)
{
    stackIn.push(val);
}

int queue::deQueue()
{
    if(stackOut.empty() and !stackIn.empty())
    {
        while(!stackIn.empty())
            stackOut.push(stackIn.pop());
    }
    return stackOut.pop();
}

int main()
{
    stack s;
    cout << "s.empty():" << s.empty() << endl;
    s.push(3);
    s.push(4);
    cout << "s.top():" << s.pop() << endl;
    cout << "s.top():" << s.pop() << endl;
    cout << "s.top():" << s.pop() << endl;
    cout << "s.size():" << s.size() << endl;
    cout << "s.empty():" << s.empty() << endl;
    queue q;
    q.enQueue(1);
    q.enQueue(2);
    q.enQueue(3);
    cout << "q.size()" << q.size() << endl;
    q.deQueue();
    cout << "q.size()" << q.size() << endl;
}
複製代碼

結果

 

相關文章
相關標籤/搜索