Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Stack Pop, Push, Peek...

Posted on 2003-11-04
4
Medium Priority
?
951 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 220 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

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:…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

885 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