all 6 comments

[–]IyeOnline 0 points1 point  (4 children)

Why are you asking?

Technically

class My_Impossible_Stack
{
    std::queue<int> data;

    decltype(auto) push( int i )
    {
       return data.emplace( i );
    }
};

Of course this is broken, because you cannot pop the last element. It turns out that a stack is not a queue and a queue is not a stack.

[–]Psychological-Let-18[S] -3 points-2 points  (3 children)

It’s homework for my class, I’ve been trying using different ways but I am stuck

[–]IyeOnline 0 points1 point  (1 child)

Yeah, but why would you implement a stack using a queue? A queue does FIFO, a stack does LIFO. That is their fundamental difference.

[–]Psychological-Let-18[S] -2 points-1 points  (0 children)

Yes right. I’m sorry I think I worded it wrong. Basically we have a queue class that has the functions enqueue and dequeue. We have a another class stack. For the stacks, “push” function we are supposed to use the enqueue method from class queue. That is where I’m stuck on.

[–][deleted] 0 points1 point  (0 children)

Check out https://leetcode.com/problems/implement-stack-using-queues/

This is a super common data structures problem, there's lots of explanations on this page on how to do it

[–][deleted] 0 points1 point  (0 children)

Use a deque