Implimenting queue using stack , code in description

#include
#include
#include
using namespace std;
template
class Queue
{
stack s1,s2;
public:
void push( T x)
{
s1.push(x);
}
void pop()
{
while(s1.size()>1)
{
T element = s1.top();
s2.push(element);
}
cout<<"\n Element poped : "<<s1.top();
s1.pop();
swap(s1,s2);
}
T front()
{
while(s1.size()>1)
{
T element = s1.top();
s2.push(element);
}
T element = s1.top();
swap(s1,s2);
return element;
}
bool empty()
{
return s1.size() == 0;
}

};

int main()
{
Queue q;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.push(5);
q.push(6);
q.push(7);
q.push(8);
while (!q.empty())
{
cout<<q.front()<<" ";
q.pop();
}

return 0;

}

Please share the link of Code

How to Share Link of Code??

  1. paste your code at https://ide.codingblocks.com/

  2. click->file->save

  3. after a link will be generated at top in place of url something like: https://ide.codingblocks.com/s/12345

  4. share this link

segmentation fault is because of infinite loop
because you forgot to pop the element from s1 while transfering
in pop() and front() function

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.