C++ STL 队列实现栈:高效数据结构操作
#include 'mystack.h'
mystack::mystack() {}
void mystack::push(int x) { q1.push(x); }
int mystack::pop() { if (empty()) return -1; while (q1.size() > 1) { q2.push(q1.front()); q1.pop(); } int res = q1.front(); q1.pop(); swap(q1, q2); return res; }
int mystack::top() { if (empty()) return -1; while (q1.size() > 1) { q2.push(q1.front()); q1.pop(); } int res = q1.front(); q2.push(q1.front()); q1.pop(); swap(q1, q2); return res; }
bool mystack::empty() { return q1.empty(); }
原文地址: https://www.cveoy.top/t/topic/owcO 著作权归作者所有。请勿转载和采集!