惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
Help Net Security
Help Net Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
A
Arctic Wolf
Cyberwarzone
Cyberwarzone
The Hacker News
The Hacker News
P
Privacy & Cybersecurity Law Blog
C
Cybersecurity and Infrastructure Security Agency CISA
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
L
LINUX DO - 最新话题
Hacker News: Ask HN
Hacker News: Ask HN
The Last Watchdog
The Last Watchdog
Forbes - Security
Forbes - Security
SecWiki News
SecWiki News
H
Heimdal Security Blog
Latest news
Latest news
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
H
Help Net Security
AWS News Blog
AWS News Blog
Cloudbric
Cloudbric
W
WeLiveSecurity
I
InfoQ
B
Blog RSS Feed
Webroot Blog
Webroot Blog
博客园 - 三生石上(FineUI控件)
T
Tor Project blog
Last Week in AI
Last Week in AI
Recent Announcements
Recent Announcements
GbyAI
GbyAI
S
Security Affairs
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
M
MIT News - Artificial intelligence
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
MongoDB | Blog
MongoDB | Blog
T
Threatpost
罗磊的独立博客
L
LINUX DO - 热门话题
C
CXSECURITY Database RSS Feed - CXSecurity.com
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hacker News - Newest:
Hacker News - Newest: "LLM"
Google DeepMind News
Google DeepMind News
Application and Cybersecurity Blog
Application and Cybersecurity Blog

博客园 - ZefengYao

Markdown 入门与 Word 使用指南 关于崩溃报告的日志以及dump文件 hdu 6223 Infinite Fraction Path 2017南宁现场赛E The Champion ACM-ICPC 2018 南京赛区网络预赛 Sum c语言几个字符串处理函数的简单实现 各种类型排序的实现及比较 随机洗牌算法Knuth Shuffle和错排公式 面试杂题 面试题——栈的压入、弹出顺序 C++ 智能指针的简单实现 openGL初学函数解释汇总 foj Problem 2107 Hua Rong Dao foj Problem 2282 Wand UVA-1400 Ray, Pass me the dishes! 《挑战程序设计竞赛》 利用后缀数组求最长回文串 Uva 11174 Stand in a Line UVA 11375 Matches poj 3729 Facer’s string
两个栈实现队列
ZefengYao · 2018-08-16 · via 博客园 - ZefengYao

两个栈实现队列

可以知道栈是先进后出的,把元素先压进第一个栈,之后再把元素抛出,压进第二栈,从第二个栈抛出的元素满足先进先出的原则。

实现queue的push操作:直接把元素都压入第一个栈即可。

实现queue的pop操作:1:判断第二个栈是否有元素?2:若第二个栈有元素,抛出第二个栈的栈顶元素即可。3:若第二个栈没元素,把第一个栈的所有元素都压进第二个栈,再抛出第二个栈的栈顶元素。

实现queue的front操作:原理同pop操作,只要返回第二个栈的栈顶元素即可。

实现queue的back操作:返回第一个栈的栈顶元素即可,若第一个栈无元素,需要先把第二个栈的所有元素压进第一个栈,再返回第一个栈的栈顶元素。

参考代码:

template<typename T>
class queue {
private:
    stack<T>st1, st2;
public:
    void push(T x);
    void pop();
    T front();
    T back();
    int size();
    bool empty();
};
template<typename T>
int queue<T>::size() {
    return st1.size() + st2.size();
}
template<typename T>
bool queue<T>::empty() {
    return st1.empty() && st2.empty();
}
template<typename T>
void queue<T>::push(T x) {
    st1.push(x);
}
template<typename T>
void queue<T>::pop() {
    if (st2.empty()) {
        while (!st1.empty()) {
            st2.push(st1.top());
            st1.pop();
        }
    }
    if (!st2.empty()) {
         st2.pop();
    }
}
template<typename T>
T queue<T>::front() {
    assert(!st1.empty() || !st2.empty());
    if (st2.empty()) {
        while (!st1.empty()) {
            st2.push(st1.top());
            st1.pop();
        }
    }
    return st2.top();
}
template<typename T>
T queue<T>::back() {
    assert(!st1.empty() || !st2.empty());
    if (st1.empty()) {
        while (!st2.empty()) {
            st1.push(st2.top());
            st2.pop();
        }
    }
     return st1.top();

}