Solved

Stack Pop, Push, Peek...

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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