Wednesday 21 September 2011

stack applications

Stack



Applications of stacks
1.       Parsing
2.       Recursive functions
3.       Calling function
4.       Expression evaluation
5.       Expression conversion: 
      (i) Postfix Conversion
      (ii) Infix to post fix 
      (iii) postfix to Infix 
      (iv) prefix to Infix
Towers of hanoi
Back Tracking (game playing, finding paths, exhaustive searching)
Memory management, Run time management for nested language features
Undo Sequence in text editor
Reversing the data
Page visited history in web browser
Saving local variables when one function call another function, and this calls another, and so on


Backtracking:


A simple example of finding the correct path in a one area. There are a series of points, from the starting point to the destination. We start from one point. To reach the final destination, there are several paths. Suppose we choose a random path. After following a certain path, we realise that the path we have chosen is wrong. So we need to find a way by which we can return to the beginning of that path. This can be done with the use of stacks. With the help of stacks, we remember the point where we have reached. This is done by pushing that point into the stack. In case we end up on the wrong path, we can pop the last point from the stack and thus return to the last point and continue our quest to find the right path. This is called backtracking.

Runtime Memory management:

There are many programming languages are stack-based,  Many virtual machines are also stack-oriented, including the p-code machine and the Java Virtual Machine.
Almost all calling conventions – computer runtime memory environments – use a special stack (the "call stack") to hold information about procedure/function calling and nesting in order to switch to the context of the called function and restore to the caller function when the calling finishes. The functions follow a runtime protocol between caller and called to save arguments and return value on the stack. Stacks are an important way of supporting nested or recursive function calls. This type of stack is used implicitly by the compiler to support CALL and RETURN statements (or their equivalents) and is not manipulated directly by the programmer.
Some programming languages use the stack to store data that is local to a procedure. Space for local data items is allocated from the stack when the procedure is entered, and is deallocated when the procedure exits. The C programming language is typically implemented in this way. Using the same stack for both data and procedure calls has important security implications (see below) of which a programmer must be aware in order to avoid introducing serious security bugs into a program.
 (this Runtime memory management is gathered from: courtesy: wikipidia)

No comments:

Post a Comment