Can We Able To Create Priority Queues Using Stack and Vice Versa?

Can You Give Some Logic For This

In priority queue, we assign priority to the elements that are being pushed. A stack requires elements to be processed in Last in First Out manner. The idea is to associate a count that determines when it was pushed. This count works as a key for the priority queue.

So the implementation of stack uses a priority queue of pairs, with the first element serving as the key.

for the other way around 2

You could use 2 stacks instead of a list and a stack for a very simple implementation.

1 Stack is just temporary. The other stack is the queue and the element you pop represents the next element in the queue.

Whenever you add an element you could pop the stack until you’ve found the correct place to push the new element. Every popped element goes onto the temporary stack. Once you’ve pushed the newly added element you start to pop from the temporary stack and push the elements back onto the real stack.

This approach works better for a priority queue than for a simple queue since the correct place to add the new item is not always the very end of the stack. There are probably far more efficient implementations though.