Solved

Stack Pop, Push, Peek...

Posted on 2003-11-04
4
939 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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:…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

22 Experts available now in Live!

Get 1:1 Help Now