日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

快速得到栈、队列的最大值

發布時間:2025/3/20 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 快速得到栈、队列的最大值 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

特點

棧——先進后出

隊列——后進先出

思路

1. 快速得到最大值的棧

結構

  • 需要兩個數組:一個數組stackItem保存棧的元素,另一個數組link2NextMaxValueIndex保存下一個最大值的位置
  • 兩個指針:一個為stackTop指向棧頂,另一個為maxValueIndex指向最大值的下標
  • 操作

  • 插入時:比較插入元素與最大值的大小,如果比最大值還大呢,link2NextMaxValueIndex指向原來最大值的位置(即maxValueIndex),而maxValueIndex變為現在插入元素的位置;否則link2NextMaxValueIndex指向-1
  • 刪除時:刪除元素的位置出,如果maxValueIndex與當前位置相同,此時maxValueIndex為link2NextMaxValueIndex[satckTop]
  • 返回棧的最大元素
  • 圖示

    以入棧2 7 1,出棧為例:

    ?

    代碼

    #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;}elselink2NextMaxValueIndex[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];elsereturn 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;}elselink2NextMaxValueIndex[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];elsereturn -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; }

    結果

    ?

    ?




    本文轉自jihite博客園博客,原文鏈接:http://www.cnblogs.com/kaituorensheng/p/3529942.html,如需轉載請自行聯系原作者

    總結

    以上是生活随笔為你收集整理的快速得到栈、队列的最大值的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。