Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 q1;

std::queue q2;

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

4. Identify cultural variations in communication style.

Answered: 1 week ago

Question

9. Understand the phenomenon of code switching and interlanguage.

Answered: 1 week ago

Question

8. Explain the difference between translation and interpretation.

Answered: 1 week ago