Question

Show how to implement a stack using two queues. Analyze the running time of the stack...

Show how to implement a stack using two queues. Analyze the running time of the stack operations. Assume that two queues Q1 and Q2 are the only data structures you can use so you shouldn’t need any auxiliary arrays.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In order to implement stack using queues, we need to maintain two queues que1 and que2. Also we will keep top stack element in a constant memory.

#include <bits/stdc++.h>
using namespace std;
// we create a class stack to be implemented by two queue's.
class Stack{
queue<int>que1,que2;// these are the two queues we use to impelement the stack
int top_element;//stores the top element of the stack
int size_of_stack;//gives the size of stack at any given time
public:
Stack(){
size_of_stack=0;//constructor to initialise the current size=0
  
}
// gice the size of stack at any moment
int size(){
return size_of_stack;
}
//now we are gonna make 3 function to perform pop, push and top function of stack
void push(int k)
{
que1.push(k);// we always push the new element in que1
size_of_stack=size_of_stack+1;// we increase the size of stack
top_element=k;//it will be the top element;
  
}
int top()// returns top element of the stack
{
return top_element;
}
bool isempty()// return true if stack is empty
{
if(size_of_stack==0)
return true;
return false;
}
void pop(){
// since queues are based on FIFO(first in first out) while stack follow LIFO(last in first out)
// so when we need to pop an element from stack, we need the last element that we pushed in queue que1
//so we pop all elements from except the last one from que1 and push them in que2, and then pop the last element of que1
//now que2 contains all elements of que1(in the same order as they were in que1), so now que2 have become que1, so we swap names of que1 and que2
// below is the implementation of the idea
if(que1.empty())// if que1 is empty, stack is empty
return;
  
int k=que1.size();
//push all elemnts in que2 except the last element
while(k>1)
{
top_element=que1.front();
que2.push(top_element);
que1.pop();
k--;
}
//removing the last element
que1.pop();
//decrasing the size
size_of_stack=size_of_stack-1;
  
//swappinf the two queue
queue<int>temp=que2;
que2=que1;
que1=temp;
  
  
  
  
  
}

  
};

int main() {
   // your code goes here
   Stack s;// creating a stack
   s.push(1);
   s.push(2);
   s.push(21);
   s.push(46);
   cout<<s.top()<<endl;
   s.pop();
   cout<<s.top()<<endl;
   cout<<s.size()<<endl;
   cout<<s.isempty();
   return 0;
}

Sample output

46
21
3
0

Time complexity analysis

push():

O(1), since we simply push an element in que1

pop()

If n is size of stack, O(n), since we pop n elements from que1 and and push n-1 elements into que2, this gives us 2n-1 operatons

top()

O(1) since we always maintain the top element in sepearte variable

isempty()

O(1) we simply check if size_of_stack is zero or not

Please give a like if you liked the answer.Thank you!

Add a comment
Know the answer?
Add Answer to:
Show how to implement a stack using two queues. Analyze the running time of the stack...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Describe, in pseudocode, how you can implement all the functions of the stack ADT using two...

    Describe, in pseudocode, how you can implement all the functions of the stack ADT using two queues. What is the running time of the push and pop functions in this case?

  • 5. (34 pts) Stacks and Queues a. Stacks and queues are similar and different. For each of stack a...

    5. (34 pts) Stacks and Queues a. Stacks and queues are similar and different. For each of stack and queue, give a drawing showing how each would be stored in an array and a linked list. Use your first name (or usual nickname) as the data put into the container as an example for your pictures. Label the accessible element. (4 pictures; 16 pts) the text and lecture. (10 pts) You will not receive full credit without appropriate stack pictures....

  • Stacks and Java 1. Using Java design and implement a stack on an array. Implement the...

    Stacks and Java 1. Using Java design and implement a stack on an array. Implement the following operations: push, pop, top, size, isEmpty. Make sure that your program checks whether the stack is full in the push operation, and whether the stack is empty in the pop operation. None of the built-in classes/methods/functions of Java can be used and must be user implemented. Practical application 1: Arithmetic operations. (a) Design an algorithm that takes a string, which represents an arithmetic...

  • Use Java to implement a basic stack using an array of integers. For the stack, you...

    Use Java to implement a basic stack using an array of integers. For the stack, you will create an array of integers that holds 5 numbers. To make it easier, you can declare the array at the class level. That way you will be able to use the array in any method in your class without using parameters. Your input/output interface should look something like the following: What operation do you want to do? push What number do you want...

  • Goals This assignment is an individual assignment and you will work on it on your own....

    Goals This assignment is an individual assignment and you will work on it on your own. The goal of this assignment is to be able to use stacks and queues, and to master and have an in-depth understanding of such primitive ADTs. In this assignment you will build a more complex ADT using the primitive stack and queue ADTs. Moreover, an important goal of this assignment is to be able to analyze an ADT that you will build yourself using...

  • 1. Analyze the running time of the following algorithm and write it using ( notation. You...

    1. Analyze the running time of the following algorithm and write it using ( notation. You must show detailed calculation/derivation of your running time along with table to get marks. int sum = 0; for (int i=n; i)=1; i=i/2) { for (int j=1; j<=i; j*=2) { sum+=i*j; } }

  • Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles...

    Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles of good programming is to reuse existing code whenever practical. If you can reuse existing code, you don't need to spend the time to rewrite it. Code used previously has also been debugged, and will likely contain fewer errors. One of the easiest ways to create a container is to leverage an existing data type to build a new abstraction. In this lesson we...

  • You are going to create a Queue. (alternately you can create a list and simply implement...

    You are going to create a Queue. (alternately you can create a list and simply implement enqueue and dequeue functions in the List – that will technically make it a queue). You will fill the first list with numbers consecutively numbered from 2 to n where n is entered by the user (we will call this Q1). When creating your Queue object use the correct function names for enqueue and dequeue functions. Again – sorry, cannot use an Javascript array...

  • c program Here we see a Stack ADT implemented using array. We would like the stack...

    c program Here we see a Stack ADT implemented using array. We would like the stack to be usable for different max sizes though, so we need to use dynamic memory allocation for our array as well. #include <stdio.h> #include <stdlib.h> typedef struct { int *data;   // stack data, we assume integer for simplicity int top;     // top of the stack int maxSize; // max size of the stack } Stack; void StackInit(Stack* stack, int size) {     // this...

  • Describe the most time-efficient way to implement the operations listed below. Assume no duplicate values and...

    Describe the most time-efficient way to implement the operations listed below. Assume no duplicate values and that you can implement the operation as a member function of the class - with access to the underlying data structure. Then, give the tightest possible upper bound for the worst case running time for each operation in terms of N. (both implemented using an arm elements into a single binary min heap. Explanation:

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT