Question
find the run time complexity of the pop and push function from this code #include #include class Stack { private: std::queue q1; std::queue q2; public:
find the run time complexity of the pop and push function from this code
#include
#include
class Stack {
private:
std::queue
std::queue
public:
void push(int x) {
q1.push(x);
}
int pop() {
if (q1.empty()) return -1;
while (q1.size() > 1) {
int temp = q1.front();
q1.pop();
q2.push(temp);
}
int result = q1.front();
q1.pop();
std::swap(q1, q2);
return result;
}
};
int main() {
Stack s; // TEST CASES
s.push(1);
s.push(2);
s.push(3);
std::cout << s.pop() << std::endl;
std::cout << s.pop() << std::endl;
std::cout << s.pop() << std::endl;
std::cout << s.pop() << std::endl;
s.push(10);
s.push(20);
s.push(30);
std::cout << s.pop() << std::endl;
std::cout << s.pop() << std::endl;
std::cout << s.pop() << std::endl;
std::cout << s.pop() << std::endl;
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started