Solved

Stack Pop, Push, Peek...

Posted on 2003-11-04
4
938 Views
Last Modified: 2008-03-17
I just learnt how to use the Stack today, and I was wondering if anyone has a practical use for the stack.  I can only think of trivial uses for it like I wrote a program that reverses a string using the old PushPop method.  But can anyone give me some examples of how it could be used in the real world???

Tim
0
Comment
Question by:UberDeveloper
  • 2
4 Comments
 
LVL 4

Expert Comment

by:brother7
ID: 9677014
I'm currently writing a chess program.  In a chess program, it's necessary to traverse a tree of possible moves and apply a Minimax algorithm to assess the position scores at the terminal nodes.  Eventually, you  have to back those scores back up to the root node and choose the move that gives the highest score.

The list of moves made as you traverse the tree is stored in a stack.  The move is made on the board object and the move is pushed onto the stack.  This goes on for a while.  Eventually, you will want to unmake the moves to go back towards the starting position. To do this you pop the move off the stack and unmake that move on the board object.  So the stack represents the path or move list from the initial position to the current position being evaluated.

My particular application is a chess program, but I'm certain a stack is useful in other tree traversal applications.
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 55 total points
ID: 9678004
another real use of a STACK is to be able to UNDO a set of opertations.  As each operation is to be performed, that state of what it is to work on is PUSHED onto the Stack.  If you then wish to UNDO the last operation, then the stack is POPPED, and the last operation is un-done.  To undo a set of operations, the stack is POPPED multiple times, to undo each step in the reverse of the order in which they were originally done.

AW
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9678412
Stacks are an essential tool in many programming and operating system functions.  

When a program runs, and a routine is called, the current instruction location is PUSHED onto the stack.  After then routine ends, the old location is POPPED from the stack, and execution resumes from there.
0
 
LVL 4

Expert Comment

by:brother7
ID: 9694134
I guess my chess example wasn't a 'practical' use of a stack, at least not practical enough to earn an Assist :(  But it is one that I'm having to code in real life.  I'll leave it up to the VB.NET compiler to handle the command stack internally, thank you.  I hope to never have to fiddle with a command or operation stack, ever. (fingers crossed)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now