954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to implement a stack using two queues!

could anyone describe to me how to implement a stack using two queues. just cant get my head round it.thanks to anyone who helps.

whatsgoin'on
Newbie Poster
2 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

It takes about five minutes to figure out. Just take a few coins and use queue-like operations to simulate the pushing and popping of a stack. The nice thing about stacks and queues is that the concepts are so simple you can easily diagram them with minimal effort.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Use two stacks. Lets name that as addStack and removeStack. Call addStack.push() method in enequeue() and in dequeue(), check whether removeStack is empty. If it is empty, copy the elements off addStack in reverse order to removeStack and flush addStack(). So now in dequeue() invoke, removeStack.pop(). Simple.

krssanthosh
Newbie Poster
1 post since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You