Stacks don’t have as much use in computers as a queue, however, when you need to use one, they are very beneficial.
Two of the most common uses of stacks are to help with Expression Conversion (Infix to Postfix, Postfix to Prefix, etc.) and especially with the parsing of data.
When parsing source files before they are compiled, interpreted, or displayed, it is important to make sure all of the tags that are opened are properly closed, or parentheses and quotes have matching pairs. In this way it is used to check to make sure your program follows the proper format.
Other examples of stacks include things like – handling multiple undos and backtracking. Undos are seen in all types of applications from coding, to writing in MS Word. Backtracking is similar but is used for determining maze solutions.
However, the most common use of a stack is for something that is hidden to most of us, despite it being used all the time. That is the call stack. The call stack is a stack which tracks what functions and methods have been called as the application runs. The first function added would be the main() function in a C/C++, C#, or Java application.
Each function call adds to the stack, so when you return, you pop off the last function call and return to the function that you called it from. This process of pushing new function calls and popping them as you return (whether from a return statement or just the end of the function) helps the program keep track of where it is as it not only tracks what function was called, but from what line so it knows where to return to. (Technically the call stack is using symbols, so it knows if there are multiple function calls on the same line.) When it gets to the end of the last function, it is popped off the stack, and then you have no more calls. This is how the program knows that it is time to exit the application.
This is one of the most common stacks, as every application uses it, however, it is hidden from us most of the time. The only time we might see it, is when an application crashes, we see which function called which function and on and on.
It should also be noted that the memory for your call stack is limited. So you can have too many functions calling functions. This rarely happens, unless you get stuck in a recursive infinite loop. Some applications allow you to specify the memory that your call stack can use, to allow more calls as necessary.
While they are not used as often as a queue, stacks are still very important. When used it is often because it is the only way to solve a problem.
Stacks in Computers was originally found on Access 2 Learn