Solved

Stack Pop, Push, Peek...

Posted on 2003-11-04
4
945 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

724 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