Write a program in C++ that uses two stacks to implement a queue. You will imple
ID: 647404 • Letter: W
Question
Write a program in C++ that uses two stacks to implement a queue. You will implement all of the basic functions of a queue container:
push: add an element onto the end of the queue (remember that a queue has a FIFO structure)
pop: remove the element at the front of the queue
front: look at the front element
back: look at the back element
size: check the queue's size
empty: check if the queue is empty
swap: given another queue, swap all elements with the other queue
You can use the std::stack container in order to implement your queue. The basic idea is that you will have one stack to take in your inputs, and another stack you will use to output values. Here is the basic pseudo code for enqueuing and dequeuing an element:
Enqueue: push element onto input stack
Dequeue: if output stack is not empty, pop element from output stack. Otherwise, pop elements from input stack and push them onto output stack. Then you can pop elements output stack
You should be able to run this main code with no issues:
int main(){
myQueue q1, q2, q3;
for(int i = 0; i < 10; ++i){
q1.push(i);
}
for(int i = 0; i < 7; ++i){
q2.push(i);
}
cout<< "Initial Queue:"< while(!q1.empty()){
cout << q1.front() << " " ;
q3.push(q1.front());
q1.pop();
}
cout<
The output for the above main program should be:
Initial Queue:
0 1 2 3 4 5 6 7 8 9
Size of Q3 (Q1 Reversed): 10
Back element of Q3: 9
Size of Q3 (after swap): 7
Size of Q2 (after swap): 10
Explanation / Answer
I can answer ,but I wan to get my ans rated.. pls let me knw if you are rating my ans
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.